Repository: jena Updated Branches: refs/heads/master b4da74768 -> e076d42dd
JENA-846 : Add IRIFactoryI.create(URI) and IRIFactoryI.construct(URI) This closes #18 Project: http://git-wip-us.apache.org/repos/asf/jena/repo Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/e076d42d Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/e076d42d Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/e076d42d Branch: refs/heads/master Commit: e076d42dd4c4103fb473b4a882167193b37d7765 Parents: b4da747 Author: Andy Seaborne <[email protected]> Authored: Fri Jan 16 08:57:03 2015 +0000 Committer: Andy Seaborne <[email protected]> Committed: Fri Jan 16 08:57:03 2015 +0000 ---------------------------------------------------------------------- .../java/org/apache/jena/iri/IRIFactoryI.java | 152 +++++++++++-------- .../apache/jena/iri/impl/AbsIRIFactoryImpl.java | 26 ++-- .../test/java/org/apache/jena/iri/TS_IRI.java | 2 + .../org/apache/jena/iri/TestIRIFactory.java | 73 +++++++++ 4 files changed, 176 insertions(+), 77 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jena/blob/e076d42d/jena-iri/src/main/java/org/apache/jena/iri/IRIFactoryI.java ---------------------------------------------------------------------- diff --git a/jena-iri/src/main/java/org/apache/jena/iri/IRIFactoryI.java b/jena-iri/src/main/java/org/apache/jena/iri/IRIFactoryI.java index 506c1f2..c16983b 100644 --- a/jena-iri/src/main/java/org/apache/jena/iri/IRIFactoryI.java +++ b/jena-iri/src/main/java/org/apache/jena/iri/IRIFactoryI.java @@ -16,92 +16,112 @@ * limitations under the License. */ -package org.apache.jena.iri; +package org.apache.jena.iri ; + +import java.net.URI ; /** - * This interface is used for - * making new {@link IRI} objects. - * It is used for making IRIs in two ways: + * This interface is used for making new {@link IRI} objects. It is used for + * making IRIs in two ways: * <ol> - * <li>Without - * resolving against a base (by the class {@link IRIFactory}). - * <li>By resolving against a base (by the interface - * {@link IRI}). + * <li>Without resolving against a base (by the class {@link IRIFactory}). + * <li>By resolving against a base (by the interface {@link IRI}). * </ol> - * Which properties of the IRIs result in errors or - * warnings is determined by the - * current settings of the underlying {@link IRIFactory}, - * which is the factory object being used in the first - * case, or the factory object used to create the base - * IRI in the second case. + * Which properties of the IRIs result in errors or warnings is determined by + * the current settings of the underlying {@link IRIFactory}, which is the + * factory object being used in the first case, or the factory object used to + * create the base IRI in the second case. */ public interface IRIFactoryI { /** - * Make a new IRI object (possibly - * including IRI resolution), - * and check it for violations - * of the standards being enforced by the factory. - * This method both allows IRI resolution - * against a base, and for creating a new - * IRI using a different factory, - * with different conformance settings, - * implementing a different URI or IRI standard, - * or variant thereof. - * @param i The IRI to use. + * Make a new IRI object (possibly including IRI resolution), and check it + * for violations of the standards being enforced by the factory. This + * method both allows IRI resolution against a base, and for creating a new + * IRI using a different factory, with different conformance settings, + * implementing a different URI or IRI standard, or variant thereof. + * + * @param i + * The IRI to use. * @return A new IRI object. - * @throws IRIException If a violation of - * the standards being enforced by the factory - * has been detected, and this violation is - * classified by the factory as an error. + * @throws IRIException + * If a violation of the standards being enforced by the factory + * has been detected, and this violation is classified by the + * factory as an error. */ - IRI construct(IRI i) throws IRIException; + IRI construct(IRI i) throws IRIException ; + /** - * Make a new IRI object (possibly - * including IRI resolution), - * and check it for violations - * of the standards being enforced by the factory. - * @param s The IRI to use. + * Make a new IRI object (possibly including IRI resolution), and check it + * for violations of the standards being enforced by the factory. + * + * @param s + * The IRI to use. * @return A new IRI object. - * @throws IRIException If a violation of - * the standards being enforced by the factory - * has been detected, and this violation is - * classified by the factory as an error. + * @throws IRIException + * If a violation of the standards being enforced by the factory + * has been detected, and this violation is classified by the + * factory as an error. */ - IRI construct(String s) throws IRIException; + IRI construct(String s) throws IRIException ; + /** - * Make a new IRI object (possibly - * including IRI resolution), - * and check it for violations - * of the standards being enforced by the factory. - * This method both allows IRI resolution - * against a base, and for creating a new - * IRI using a different factory, with different - * conformance settings, - * implementing a different URI or IRI standard, - * or variant thereof. - * This method does not throw exceptions, but - * records all errors and warnings found - * to be queried later using {@link IRI#hasViolation(boolean)} - * and {@link IRI#violations(boolean)}. - * @param i The IRI to use. + * Make a new IRI object (possibly including IRI resolution), and check it + * for violations of the standards being enforced by the factory. + * + * @param uri + * The IRI string to use. + * @return A new IRI object. + * @throws IRIException + * If a violation of the standards being enforced by the factory + * has been detected, and this violation is classified by the + * factory as an error. + */ + IRI construct(URI uri) throws IRIException ; + + /** + * Make a new IRI object (possibly including IRI resolution), and check it + * for violations of the standards being enforced by the factory. This + * method both allows IRI resolution against a base, and for creating a new + * IRI using a different factory, with different conformance settings, + * implementing a different URI or IRI standard, or variant thereof. This + * method does not throw exceptions, but records all errors and warnings + * found to be queried later using {@link IRI#hasViolation(boolean)} and + * {@link IRI#violations(boolean)}. + * + * @param i + * The IRI to use. * @return A new IRI object. * */ - IRI create(IRI i); + IRI create(IRI i) ; + /** - * Make a new IRI object (possibly - * including IRI resolution), - * and check it for violations - * of the standards being enforced by the factory. - * This method does not throw exceptions, but - * records all errors and warnings found - * to be queried later using {@link IRI#hasViolation(boolean)} - * and {@link IRI#violations(boolean)}. - * @param s The IRI to use. + * Make a new IRI object (possibly including IRI resolution), and check it + * for violations of the standards being enforced by the factory. This + * method does not throw exceptions, but records all errors and warnings + * found to be queried later using {@link IRI#hasViolation(boolean)} and + * {@link IRI#violations(boolean)}. + * + * @param s + * The IRI to use. + * @return A new IRI object. + * + */ + IRI create(String s) ; + + /** + * Make a new IRI object (possibly including IRI resolution), and check it + * for violations of the standards being enforced by the factory. This + * method does not throw exceptions, but records all errors and warnings + * found to be queried later using {@link IRI#hasViolation(boolean)} and + * {@link IRI#violations(boolean)}. + * + * @param uri + * The URI to use (relative or absolute). * @return A new IRI object. * */ - IRI create(String s); + IRI create(URI uri) ; } http://git-wip-us.apache.org/repos/asf/jena/blob/e076d42d/jena-iri/src/main/java/org/apache/jena/iri/impl/AbsIRIFactoryImpl.java ---------------------------------------------------------------------- diff --git a/jena-iri/src/main/java/org/apache/jena/iri/impl/AbsIRIFactoryImpl.java b/jena-iri/src/main/java/org/apache/jena/iri/impl/AbsIRIFactoryImpl.java index 4a048bb..fe6fd1c 100644 --- a/jena-iri/src/main/java/org/apache/jena/iri/impl/AbsIRIFactoryImpl.java +++ b/jena-iri/src/main/java/org/apache/jena/iri/impl/AbsIRIFactoryImpl.java @@ -19,6 +19,8 @@ package org.apache.jena.iri.impl; +import java.net.URI; + import org.apache.jena.iri.IRI ; import org.apache.jena.iri.IRIException ; import org.apache.jena.iri.IRIFactoryI ; @@ -42,7 +44,12 @@ abstract public class AbsIRIFactoryImpl implements IRIFactoryI { ); } - //@Override + @Override + public IRI create(URI uri) { + return create(uri.toASCIIString()); + } + + @Override public IRI construct(String s) throws IRIException { return throwAnyErrors(create(s)); @@ -51,15 +58,14 @@ abstract public class AbsIRIFactoryImpl implements IRIFactoryI { //@Override @Override public IRI construct(IRI i) throws IRIException { - return throwAnyErrors(create(i)); + return throwAnyErrors(create(i)); + } -// -// try { -// return create(i,true); -// } catch (Violation e) { -// throw new IRIImplException(e); -// } + @Override + public IRI construct(URI uri) throws IRIException { + return throwAnyErrors(create(uri)); } + protected IRI throwAnyErrors(IRI rslt) throws IRIException { if (rslt.hasViolation(false)) { throw new IRIImplException(rslt.violations(false).next()); @@ -72,9 +78,7 @@ abstract public class AbsIRIFactoryImpl implements IRIFactoryI { } return rslt; } -// public IRI create(IRI i) { -// return create(i); -// } + @Override abstract public IRI create(IRI i); } http://git-wip-us.apache.org/repos/asf/jena/blob/e076d42d/jena-iri/src/test/java/org/apache/jena/iri/TS_IRI.java ---------------------------------------------------------------------- diff --git a/jena-iri/src/test/java/org/apache/jena/iri/TS_IRI.java b/jena-iri/src/test/java/org/apache/jena/iri/TS_IRI.java index 594cfda..03b69bd 100644 --- a/jena-iri/src/test/java/org/apache/jena/iri/TS_IRI.java +++ b/jena-iri/src/test/java/org/apache/jena/iri/TS_IRI.java @@ -31,6 +31,8 @@ public class TS_IRI extends TestCase{ rslt.addTest(TestMoreExamples.suite()); rslt.addTest(MoreTests.suite()); rslt.addTest(Additional.suite()); + rslt.addTest(TestIRIFactory.suite()); + return rslt; } } http://git-wip-us.apache.org/repos/asf/jena/blob/e076d42d/jena-iri/src/test/java/org/apache/jena/iri/TestIRIFactory.java ---------------------------------------------------------------------- diff --git a/jena-iri/src/test/java/org/apache/jena/iri/TestIRIFactory.java b/jena-iri/src/test/java/org/apache/jena/iri/TestIRIFactory.java new file mode 100644 index 0000000..4022b53 --- /dev/null +++ b/jena-iri/src/test/java/org/apache/jena/iri/TestIRIFactory.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.jena.iri; + +import static org.junit.Assert.assertEquals; + +import java.net.URI; + +import junit.framework.JUnit4TestAdapter; +import org.junit.Test; + +public class TestIRIFactory { + static public junit.framework.Test suite() { + return new JUnit4TestAdapter(TestIRIFactory.class); + } + + private static IRIFactory iriFactory = IRIFactory.jenaImplementation(); + + @Test + public void createFromURI() throws Exception { + URI uri = URI.create("http://johndoe:[email protected]:81/page?2#hash"); + IRI iri = iriFactory.create(uri); + assertEquals("http", iri.getScheme()); + assertEquals("johndoe:secret", iri.getRawUserinfo()); + assertEquals("example.com", iri.getRawHost()); + assertEquals(81, iri.getPort()); + assertEquals("/page", iri.getRawPath()); + assertEquals("2", iri.getRawQuery()); + assertEquals("hash", iri.getRawFragment()); + } + + @Test + public void createFromRelativeURI() throws Exception { + URI relative = URI.create("page/deeper.txt?q"); + IRI relativeIri = iriFactory.create(relative); + assertEquals("page/deeper.txt", relativeIri.getRawPath()); + + IRI base = iriFactory.create("http://example.com/relative/path?q=somethingelse"); + IRI absolute = base.create(relative); + assertEquals("http://example.com/relative/page/deeper.txt?q", absolute.toString()); + + URI other = URI.create("http://other.example.net/"); + IRI otherIri = base.create(other); + assertEquals("http://other.example.net/", otherIri.toASCIIString()); + } + + @Test(expected=IRIException.class) + public void constructFromDubiousURI() throws Exception { + // Legal URI by Java + URI relative = URI.create("unknown:abc"); + // Create a factory and set an error rule. + IRIFactory factory = new IRIFactory(); + factory.setIsError(ViolationCodes.UNREGISTERED_IANA_SCHEME,true); + // Expect exception + IRI relativeIri = IRIFactory.iriImplementation().construct(relative); + } + +}
