Skip to content

Commit

Permalink
Merge pull request #294 from jvasiljevich/accept-url-sources
Browse files Browse the repository at this point in the history
Accept URL sources
  • Loading branch information
joelittlejohn committed Mar 5, 2015
2 parents 5656940 + 37b1c72 commit 7f0eaee
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.Jsonschema2Pojo;
import org.jsonschema2pojo.NoopAnnotator;
import org.jsonschema2pojo.URLProtocol;
import org.jsonschema2pojo.SourceType;
import org.jsonschema2pojo.rules.RuleFactory;
import org.jsonschema2pojo.util.URLUtil;

/**
* When invoked, this task reads one or more <a
Expand All @@ -63,7 +65,7 @@ public class Jsonschema2PojoTask extends Task implements GenerationConfig {

private boolean usePrimitives;

private File source;
private String source;

private File targetDirectory;

Expand Down Expand Up @@ -130,11 +132,24 @@ public void execute() throws BuildException {
return;
}

if (!source.exists()) {
log(source.getAbsolutePath() + " cannot be found");
// attempt to parse the url
URL sourceURL;
try {
sourceURL = URLUtil.parseURL(source);
} catch (IllegalArgumentException e) {
log(String.format("Invalid schema source provided: %s", source));
return;
}

// if url is a file, ensure it exists
if (URLUtil.parseProtocol(sourceURL.toString()) == URLProtocol.FILE) {
File sourceFile = new File(sourceURL.getFile());
if (!sourceFile.exists()) {
log(sourceFile.getAbsolutePath() + " cannot be found");
return;
}
}

if (targetDirectory == null) {
log("targetDirectory attribute is required but was not set");
return;
Expand All @@ -146,7 +161,7 @@ public void execute() throws BuildException {
try {
Jsonschema2Pojo.generate(this);
} catch (IOException e) {
throw new BuildException("Error generating classes from JSON Schema file(s) " + source.getPath(), e);
throw new BuildException("Error generating classes from JSON Schema file(s) " + source, e);
}
}

Expand Down Expand Up @@ -257,7 +272,7 @@ public void setUseDoubleNumbers(boolean useDoubleNumbers) {
* Location of the JSON Schema file(s). Note: this may refer to a
* single file or a directory of files.
*/
public void setSource(File source) {
public void setSource(String source) {
this.source = source;
}

Expand Down Expand Up @@ -469,8 +484,8 @@ public boolean isUsePrimitives() {
}

@Override
public Iterator<File> getSource() {
return Collections.singleton(source).iterator();
public Iterator<URL> getSource() {
return Collections.singleton(URLUtil.parseURL(source)).iterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.File;
import java.io.FileFilter;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

Expand All @@ -34,7 +35,6 @@
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.converters.FileConverter;

/**
* Describes and parses the command line arguments supported by the
Expand All @@ -51,8 +51,8 @@ public class Arguments implements GenerationConfig {
@Parameter(names = { "-t", "--target" }, description = "The target directory into which generated types will be written", required = true)
private File targetDirectory;

@Parameter(names = { "-s", "--source" }, description = "The source file(s) or directory(ies) from which JSON Schema will be read", required = true, converter = FileConverter.class)
private List<File> sourcePaths;
@Parameter(names = { "-s", "--source" }, description = "The source file(s) or directory(ies) from which JSON Schema will be read", required = true, converter = UrlConverter.class)
private List<URL> sourcePaths;

@Parameter(names = { "-b", "--generate-builders" }, description = "Generate builder-style methods as well as setters")
private boolean generateBuilderMethods = false;
Expand Down Expand Up @@ -158,7 +158,7 @@ public Arguments parse(String[] args) {
}

@Override
public Iterator<File> getSource() {
public Iterator<URL> getSource() {
return sourcePaths.iterator();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private Jsonschema2PojoCLI() {
* if the application is unable to read data from the paths
* specified
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
public static void main(String[] args) throws IOException {

Arguments arguments = new Arguments().parse(args);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright © 2010-2014 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.cli;

import com.beust.jcommander.ParameterException;
import com.beust.jcommander.converters.BaseConverter;
import org.jsonschema2pojo.util.URLUtil;

import java.net.URL;

import static org.apache.commons.lang3.StringUtils.isBlank;


/**
* Convert a string into a url.
*
* @author jvasiljevich
*/
public class UrlConverter extends BaseConverter<URL> {

public UrlConverter(String optionName) {
super(optionName);
}

public URL convert(String value) {
if (isBlank(value)) {
throw new ParameterException(getErrorString("a blank value", "a valid URL"));
}

try {
return URLUtil.parseURL(value);
} catch (IllegalArgumentException e) {
throw new ParameterException(getErrorString(value, "a valid URL"));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsonschema2pojo.util.URLUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -55,7 +58,7 @@ public void parseRecognisesValidArguments() {
});

assertThat(args.didExit(), is(false));
assertThat(args.getSource().next(), is(theFile("/home/source")));
assertThat(args.getSource().next().getFile(), is("/home/source"));
assertThat(args.getTargetDirectory(), is(theFile("/home/target")));
assertThat(args.getTargetPackage(), is("mypackage"));
assertThat(args.isGenerateBuilders(), is(true));
Expand All @@ -71,7 +74,7 @@ public void parseRecognisesShorthandArguments() {
});

assertThat(args.didExit(), is(false));
assertThat(args.getSource().next(), is(theFile("/home/source")));
assertThat(args.getSource().next().getFile(), is("/home/source"));
assertThat(args.getTargetDirectory(), is(theFile("/home/target")));
assertThat(args.getTargetPackage(), is("mypackage"));
assertThat(args.isGenerateBuilders(), is(true));
Expand All @@ -96,7 +99,7 @@ public void allOptionalArgsCanBeOmittedAndDefaultsPrevail() {
});

assertThat(args.didExit(), is(false));
assertThat(args.getSource().next(), is(theFile("/home/source")));
assertThat(args.getSource().next().getFile(), is("/home/source"));
assertThat(args.getTargetDirectory(), is(theFile("/home/target")));
assertThat(args.getTargetPackage(), is(nullValue()));
assertThat(args.isGenerateBuilders(), is(false));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright © 2010-2014 Nokia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jsonschema2pojo.cli;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import org.junit.Test;

import com.beust.jcommander.ParameterException;
import org.jsonschema2pojo.Annotator;

import java.net.URL;

public class UrlConverterTest {

private UrlConverter converter = new UrlConverter("--source");

@Test
public void urlIsCreatedFromFilePath() {
URL url = converter.convert("/path/to/something");

assertThat(url.getPath(), is("/path/to/something"));
}

@Test
public void urlIsCreatedFromFileUrl() {
URL url = converter.convert("file:/path/to/something");

assertThat(url.toString(), is("file:/path/to/something"));
}

@Test(expected = ParameterException.class)
public void invalidUrlThrowsParameterException() {
converter.convert("http:total nonsense");
}

@Test(expected = ParameterException.class)
public void nullValueThrowsParameterException() {
converter.convert(null);
}

}
4 changes: 4 additions & 0 deletions jsonschema2pojo-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.File;
import java.io.FileFilter;
import java.net.URL;
import java.util.Iterator;

/**
Expand Down Expand Up @@ -47,7 +48,7 @@ public boolean isUsePrimitives() {
* Unsupported since no default source is possible.
*/
@Override
public Iterator<File> getSource() {
public Iterator<URL> getSource() {
throw new UnsupportedOperationException("No default source available");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.io.FileFilter;
import java.net.URL;
import java.util.Iterator;
import org.jsonschema2pojo.rules.RuleFactory;

Expand Down Expand Up @@ -55,7 +56,7 @@ public interface GenerationConfig {
* @return The source file(s) or directory(ies) from which JSON Schema will
* be read.
*/
Iterator<File> getSource();
Iterator<URL> getSource();

/**
* Gets the 'targetDirectory' configuration option.
Expand Down
Loading

0 comments on commit 7f0eaee

Please # to comment.