Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENA-1537: Remove dependency on Apache Xerces. #413

Merged
merged 2 commits into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ PluggedIn Software under a BSD license.

This product includes software developed by Mort Bay Consulting Pty. Ltd.
Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.

Portions of this software are from Apache Xerces and were originally based on the following:
- software copyright (c) 1999, IBM Corporation., http://www.ibm.com.
- software copyright (c) 1999, Sun Microsystems., http://www.sun.com.
21 changes: 2 additions & 19 deletions apache-jena-osgi/jena-osgi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,6 @@
<version>3.8.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- xerces is needed - but this is marked as optional as some
OSGi frameworks like Karaf will include their own
xerces in their boot delegation.

See also pull request #134
https://github.com/apache/jena/pull/134#issuecomment-209474708
-->
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.xerces</artifactId>
<version>2.11.0_1</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.apache.jena</groupId>
Expand Down Expand Up @@ -256,14 +243,10 @@
<instructions>
<Export-Package>org.apache.jena.*,!org.apache.jena.ext.*</Export-Package>
<Embed-Dependency>artifactId=jena*;inline=true</Embed-Dependency>
<!--
<Embed-Dependency>artifactId=jena*;inline=true,artifactId=xercesImpl;inline=true,artifactId=xml-apis;inline=true</Embed-Dependency>
-->
<Embed-Transitive>true</Embed-Transitive>
<!-- Do not embed but import Xerces classes via OSGi -->
<Import-Package>org.osgi.framework.*,org.apache.xml.*,org.apache.xerces.*;version="2.11.0",!sun.io,!org.apache.avalon.framework.logger,!com.ibm.uvm.tools,!com.sun.jdmk.comm,!org.apache.log,!org.apache.jena.ext.*,sun.misc;resolution:=optional,com.google.errorprone.annotations.concurrent;resolution:=optional,*</Import-Package>
<Import-Package>org.osgi.framework.*,!sun.io,!org.apache.avalon.framework.logger,!com.ibm.uvm.tools,!com.sun.jdmk.comm,!org.apache.log,!org.apache.jena.ext.*,sun.misc;resolution:=optional,com.google.errorprone.annotations.concurrent;resolution:=optional,*</Import-Package>
<!--
<Import-Package>org.osgi.framework.*,!sun.io,!org.apache.avalon.framework.logger,!com.ibm.uvm.tools,!com.sun.jdmk.comm,!org.apache.log,!org.apache.xml.*,!org.apache.xerces.*,!org.apache.jena.ext.*,sun.misc;resolution:=optional,*</Import-Package>
<Import-Package>org.osgi.framework.*,!sun.io,!org.apache.avalon.framework.logger,!com.ibm.uvm.tools,!com.sun.jdmk.comm,!org.apache.log,!org.apache.xml.*,!org.apache.jena.ext.*,sun.misc;resolution:=optional,*</Import-Package>
-->
<Private-Package>org.apache.jena.ext.*</Private-Package>
<Bundle-Activator>org.apache.jena.osgi.Activator</Bundle-Activator>
Expand Down
70 changes: 3 additions & 67 deletions jena-arq/src/main/java/org/apache/jena/sparql/expr/NodeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@
import static org.apache.jena.datatypes.xsd.XSDDatatype.* ;
import static org.apache.jena.sparql.expr.ValueSpaceClassification.* ;

import java.io.File ;
import java.io.FileInputStream ;
import java.io.InputStream ;
import java.math.BigDecimal ;
import java.math.BigInteger ;
import java.util.Calendar ;
import java.util.Iterator ;
import java.util.Properties ;
import java.util.ServiceLoader ;

import javax.xml.datatype.DatatypeConfigurationException ;
import javax.xml.datatype.DatatypeFactory ;
import javax.xml.datatype.Duration ;
import javax.xml.datatype.XMLGregorianCalendar ;
Expand All @@ -45,6 +38,7 @@
import org.apache.jena.datatypes.RDFDatatype ;
import org.apache.jena.datatypes.TypeMapper ;
import org.apache.jena.datatypes.xsd.XSDDateTime ;
import org.apache.jena.ext.xerces.DatatypeFactoryInst;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.NodeFactory ;
import org.apache.jena.graph.impl.LiteralLabel ;
Expand Down Expand Up @@ -145,66 +139,8 @@ public abstract class NodeValue extends ExprNode

public static final String xsdNamespace = XSD+"#" ;

public static DatatypeFactory xmlDatatypeFactory = null ;
static
{
try { xmlDatatypeFactory = getDatatypeFactory() ; }
catch (DatatypeConfigurationException ex)
{ throw new ARQInternalErrorException("Can't create a javax.xml DatatypeFactory", ex) ; }
}

/**
* Get a datatype factory using the correct classloader
*
* See JENA-328. DatatypeFactory.newInstance() clashes with OSGi
* This is clearly crazy, but DatatypeFactory is missing a very obvious
* method newInstance(Classloader). The method that was added is very
* hard to use correctly, as we shall see...
*/
private static DatatypeFactory getDatatypeFactory()
throws DatatypeConfigurationException {
// Step 1. Try the system property
String dtfClass = System.getProperty(DatatypeFactory.DATATYPEFACTORY_PROPERTY);

try {
File jaxpPropFile = new File(System.getProperty("java.home") +
File.separator + "lib" + File.separator + "jaxp.properties");
// Step 2. Otherwise, try property in jaxp.properties
if (dtfClass == null && jaxpPropFile.exists() && jaxpPropFile.canRead()) {
Properties jaxp = new Properties();
try(InputStream in = new FileInputStream(jaxpPropFile)) {
jaxp.load(in);
dtfClass = jaxp.getProperty(DatatypeFactory.DATATYPEFACTORY_PROPERTY);
} catch (Exception e) {
log.warn("Issue loading jaxp.properties", e);
}
}
}
// File.exists and File.canRead may throw SecurityException (probably AccessControlException)
catch (SecurityException ex) {
log.warn("Security exception try to get jaxp.properties: "+ex.getMessage()) ;
}

// Step 3. Otherwise try the service approach
// This is the normal initialization path, getting it from the Apach Xerces dependency
// and loading org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl
if (dtfClass == null) {
ClassLoader cl = NodeValue.class.getClassLoader();
Iterator<DatatypeFactory> factoryIterator =
ServiceLoader.load(DatatypeFactory.class, cl).iterator();
if (factoryIterator.hasNext()) return factoryIterator.next();
}

// Step 4. Use the default.
// Note: When Apache Xerces is on the classpath for Jena, javax.xml.datatype.DatatypeFactory is from that jar and
// DATATYPEFACTORY_IMPLEMENTATION_CLASS is "org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl"
// Without an explicit Xerces, we would get "com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl"
// from the JDK rt.jar version of javax.xml.datatype.DatatypeFactory.
if (dtfClass == null)
dtfClass = DatatypeFactory.DATATYPEFACTORY_IMPLEMENTATION_CLASS;
return DatatypeFactory.newInstance(dtfClass, NodeValue.class.getClassLoader()) ;
}

public static DatatypeFactory xmlDatatypeFactory = DatatypeFactoryInst.newDatatypeFactory();

private Node node = null ; // Null used when a value has not be turned into a Node.

// Don't create direct - the static builders manage the value/node relationship
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package org.apache.jena.sparql.expr;

import org.apache.xerces.impl.xpath.regex.ParseException ;
import org.apache.xerces.impl.xpath.regex.REUtil ;
import org.apache.xerces.impl.xpath.regex.RegularExpression ;
import org.apache.jena.ext.xerces.impl.xpath.regex.ParseException;
import org.apache.jena.ext.xerces.impl.xpath.regex.REUtil;
import org.apache.jena.ext.xerces.impl.xpath.regex.RegularExpression;

public class RegexXerces implements RegexEngine
{
Expand All @@ -29,7 +29,6 @@ public class RegexXerces implements RegexEngine
public RegexXerces(String pattern, String flags)
{
if ( flags.contains("q") )
// Nest we can do.
pattern = REUtil.quoteMeta(pattern);
regexPattern = makePattern(pattern, flags) ;
}
Expand Down
2 changes: 1 addition & 1 deletion jena-cmds/src/main/java/jena/schemagen.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.regex.Pattern ;
import java.util.regex.PatternSyntaxException ;

import org.apache.jena.ext.xerces.util.XMLChar;
import org.apache.jena.ontology.Individual ;
import org.apache.jena.ontology.OntModel ;
import org.apache.jena.ontology.OntModelSpec ;
Expand All @@ -46,7 +47,6 @@
import org.apache.jena.vocabulary.RDF ;
import org.apache.jena.vocabulary.RDFS ;
import org.apache.jena.vocabulary.XSD ;
import org.apache.xerces.util.XMLChar ;



Expand Down
4 changes: 4 additions & 0 deletions jena-core/NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ These have been licensed to the Apache Software Foundation under a software gran

This product includes software developed by
PluggedIn Software under a BSD license.

Portions of this software are from Apache Xerces and were originally based on the following:
- software copyright (c) 1999, IBM Corporation., http://www.ibm.com.
- software copyright (c) 1999, Sun Microsystems., http://www.sun.com.
5 changes: 0 additions & 5 deletions jena-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@
<version>3.8.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</dependency>

<dependency>
<artifactId>commons-cli</artifactId>
<groupId>commons-cli</groupId>
Expand Down
173 changes: 85 additions & 88 deletions jena-core/src/main/java/org/apache/jena/datatypes/xsd/XSDDatatype.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,25 @@

package org.apache.jena.datatypes.xsd;

import java.io.Reader ;
import java.math.BigDecimal ;
import java.math.BigInteger ;
import java.net.URI ;
import java.util.ArrayList ;
import java.util.List ;

import org.apache.jena.datatypes.BaseDatatype ;
import org.apache.jena.datatypes.DatatypeFormatException ;
import org.apache.jena.datatypes.RDFDatatype ;
import org.apache.jena.datatypes.TypeMapper ;
import org.apache.jena.datatypes.xsd.impl.* ;
import org.apache.jena.ext.xerces.impl.dv.*;
import org.apache.jena.ext.xerces.impl.dv.util.Base64;
import org.apache.jena.ext.xerces.impl.dv.util.HexBin;
import org.apache.jena.ext.xerces.impl.dv.xs.DecimalDV;
import org.apache.jena.ext.xerces.impl.dv.xs.XSSimpleTypeDecl;
import org.apache.jena.ext.xerces.impl.validation.ValidationState;
import org.apache.jena.ext.xerces.util.SymbolHash;
import org.apache.jena.ext.xerces.xs.XSConstants;
import org.apache.jena.ext.xerces.xs.XSTypeDefinition;
import org.apache.jena.graph.impl.LiteralLabel ;
import org.apache.xerces.impl.dv.* ;
import org.apache.xerces.impl.dv.util.Base64 ;
import org.apache.xerces.impl.dv.util.HexBin ;
import org.apache.xerces.impl.dv.xs.DecimalDV ;
import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl ;
import org.apache.xerces.impl.validation.ValidationState ;
import org.apache.xerces.parsers.XMLGrammarPreparser ;
import org.apache.xerces.util.SymbolHash ;
import org.apache.xerces.xni.grammars.XMLGrammarDescription ;
import org.apache.xerces.xni.grammars.XSGrammar ;
import org.apache.xerces.xni.parser.XMLInputSource ;
import org.apache.xerces.xs.XSConstants ;
import org.apache.xerces.xs.XSNamedMap ;
import org.apache.xerces.xs.XSTypeDefinition ;

/**
* Representation of an XSD datatype based on the Xerces-2
Expand Down Expand Up @@ -315,77 +307,82 @@ public Object extendedTypeDefinition() {
return typeDeclaration;
}

/**
* Create and register a set of types specified in a user schema file.
* We use the (illegal) DAML+OIL approach that the uriref of the type
* is the url of the schema file with fragment ID corresponding the
* the name of the type.
*
* @param uri the absolute uri of the schema file to be loaded
* @param reader the Reader stream onto the file (useful if you wish to load a cached copy of the schema file)
* @param encoding the encoding of the source file (can be null)
* @param tm the type mapper into which to load the definitions
* @return a List of strings giving the uri's of the newly defined datatypes
* @throws DatatypeFormatException if there is a problem during load (not that we use Xerces
* in default mode for load which may provide diagnostic output direct to stderr)
*/
public static List<String> loadUserDefined(String uri, Reader reader, String encoding, TypeMapper tm) throws DatatypeFormatException {
return loadUserDefined(new XMLInputSource(null, uri, uri, reader, encoding), tm);
}

/**
* Create and register a set of types specified in a user schema file.
* We use the (illegal) DAML+OIL approach that the uriref of the type
* is the url of the schema file with fragment ID corresponding the
* the name of the type.
*
* @param uri the absolute uri of the schema file to be loaded, this should be a resolvable URL
* @param encoding the encoding of the source file (can be null)
* @param tm the type mapper into which to load the definitions
* @return a List of strings giving the uri's of the newly defined datatypes
* @throws DatatypeFormatException if there is a problem during load (not that we use Xerces
* in default mode for load which may provide diagnostic output direct to stderr)
*/
public static List<String> loadUserDefined(String uri, String encoding, TypeMapper tm) throws DatatypeFormatException {
return loadUserDefined(new XMLInputSource(null, uri, uri), tm);
}

/**
* Internal implementation of loadUserDefined
*
* @param uri the absolute uri of the schema file to be loaded
* @param reader the Reader stream onto the file (useful if you wish to load a cached copy of the schema file)
* @param encoding the encoding of the source file (can be null)
* @param tm the type mapper into which to load the definitions
* @return a List of strings giving the uri's of the newly defined datatypes
* @throws DatatypeFormatException if there is a problem during load (not that we use Xerces
* in default mode for load which may provide diagnostic output direct to stderr)
*/
private static List<String> loadUserDefined(XMLInputSource source, TypeMapper tm) throws DatatypeFormatException {
XMLGrammarPreparser parser = new XMLGrammarPreparser();
parser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
try {
XSGrammar xsg = (XSGrammar) parser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, source);
org.apache.xerces.xs.XSModel xsm = xsg.toXSModel();
XSNamedMap map = xsm.getComponents(XSTypeDefinition.SIMPLE_TYPE);
int numDefs = map.getLength();
ArrayList<String> names = new ArrayList<>(numDefs);
for (int i = 0; i < numDefs; i++) {
XSSimpleType xstype = (XSSimpleType) map.item(i);
// Filter built in types - only needed for 2.6.0
if ( ! XSD.equals(xstype.getNamespace()) ) {
//xstype.derivedFrom()
XSDDatatype definedType = new XSDGenericType(xstype, source.getSystemId());
tm.registerDatatype(definedType);
names.add(definedType.getURI());
}
}
return names;
} catch (Exception e) {
e.printStackTrace(); // Temp
throw new DatatypeFormatException(e.toString());
}
}
// The following code parses an external XSD file for XSD datatype definitions.
// See an example "testing/xsd/daml+oil-ex-dt.xsd" and TestTypeLiterals.testUserDefined.
// It calls into Xerces internal XM parsing, which is problematic if switching to the
// JDK built-in XML parser in Java9 and later.

// /**
// * Create and register a set of types specified in a user schema file.
// * We use the (illegal) DAML+OIL approach that the uriref of the type
// * is the url of the schema file with fragment ID corresponding the
// * the name of the type.
// *
// * @param uri the absolute uri of the schema file to be loaded
// * @param reader the Reader stream onto the file (useful if you wish to load a cached copy of the schema file)
// * @param encoding the encoding of the source file (can be null)
// * @param tm the type mapper into which to load the definitions
// * @return a List of strings giving the uri's of the newly defined datatypes
// * @throws DatatypeFormatException if there is a problem during load (not that we use Xerces
// * in default mode for load which may provide diagnostic output direct to stderr)
// */
// public static List<String> loadUserDefined(String uri, Reader reader, String encoding, TypeMapper tm) throws DatatypeFormatException {
// return loadUserDefined(new XMLInputSource(null, uri, uri, reader, encoding), tm);
// }
//
// /**
// * Create and register a set of types specified in a user schema file.
// * We use the (illegal) DAML+OIL approach that the uriref of the type
// * is the url of the schema file with fragment ID corresponding the
// * the name of the type.
// *
// * @param uri the absolute uri of the schema file to be loaded, this should be a resolvable URL
// * @param encoding the encoding of the source file (can be null)
// * @param tm the type mapper into which to load the definitions
// * @return a List of strings giving the uri's of the newly defined datatypes
// * @throws DatatypeFormatException if there is a problem during load (not that we use Xerces
// * in default mode for load which may provide diagnostic output direct to stderr)
// */
// public static List<String> loadUserDefined(String uri, String encoding, TypeMapper tm) throws DatatypeFormatException {
// return loadUserDefined(new XMLInputSource(null, uri, uri), tm);
// }
//
// /**
// * Internal implementation of loadUserDefined
// *
// * @param uri the absolute uri of the schema file to be loaded
// * @param reader the Reader stream onto the file (useful if you wish to load a cached copy of the schema file)
// * @param encoding the encoding of the source file (can be null)
// * @param tm the type mapper into which to load the definitions
// * @return a List of strings giving the uri's of the newly defined datatypes
// * @throws DatatypeFormatException if there is a problem during load (not that we use Xerces
// * in default mode for load which may provide diagnostic output direct to stderr)
// */
// private static List<String> loadUserDefined(XMLInputSource source, TypeMapper tm) throws DatatypeFormatException {
// XMLGrammarPreparser parser = new XMLGrammarPreparser();
// parser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null);
// try {
// XSGrammar xsg = (XSGrammar) parser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, source);
// org.apache.xerces.xs.XSModel xsm = xsg.toXSModel();
// XSNamedMap map = xsm.getComponents(XSTypeDefinition.SIMPLE_TYPE);
// int numDefs = map.getLength();
// ArrayList<String> names = new ArrayList<>(numDefs);
// for (int i = 0; i < numDefs; i++) {
// XSSimpleType xstype = (XSSimpleType) map.item(i);
// // Filter built in types - only needed for 2.6.0
// if ( ! XSD.equals(xstype.getNamespace()) ) {
// //xstype.derivedFrom()
// XSDDatatype definedType = new XSDGenericType(xstype, source.getSystemId());
// tm.registerDatatype(definedType);
// names.add(definedType.getURI());
// }
// }
// return names;
// } catch (Exception e) {
// e.printStackTrace(); // Temp
// throw new DatatypeFormatException(e.toString());
// }
// }

/**
* Convert a validated xerces data value into the corresponding java data value.
Expand Down
Loading