[jboss-svn-commits] JBoss Common SVN: r3021 - in jbossxb/trunk/src: main/java/org/jboss/xb/binding/resolver and 4 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Mar 6 05:05:03 EST 2009
Author: alex.loubyansky at jboss.com
Date: 2009-03-06 05:05:02 -0500 (Fri, 06 Mar 2009)
New Revision: 3021
Added:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/
jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/AbstractMutableSchemaResolver.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/MultiClassSchemaResolver.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/MutableSchemaResolver.java
Removed:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/MultiClassSchemaResolver.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UriToClassMapping.java
Modified:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfig.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SingletonSchemaResolverFactory.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/XsdBinder.java
jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/AbstractBuilderTest.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/JBossXBTestDelegate.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/JbxbSchemaBindingAttributeUnitTestCase.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/SchemaBindingInitializerUnitTestCase.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/SchemaIncludeUnitTestCase.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardWrapperUnitTestCase.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnitTestCase.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/XercesBugTestCase.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/jbxb/minOccurs/Schema1UnitTestCase.java
Log:
JBXB-190
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/AbstractMutableSchemaResolver.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/AbstractMutableSchemaResolver.java (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/AbstractMutableSchemaResolver.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -0,0 +1,495 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.xb.binding.resolver;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.logging.Logger;
+import org.jboss.util.xml.JBossEntityResolver;
+import org.jboss.xb.binding.JBossXBRuntimeException;
+import org.jboss.xb.binding.sunday.unmarshalling.LSInputAdaptor;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingInitializer;
+import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
+import org.jboss.xb.builder.JBossXBBuilder;
+import org.w3c.dom.ls.LSInput;
+import org.xml.sax.InputSource;
+
+/**
+ * A AbstractMutableSchemaResolver.
+ *
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class AbstractMutableSchemaResolver implements MutableSchemaResolver
+{
+ private Logger log;
+ private String baseURI;
+ private JBossEntityResolver resolver;
+ private boolean cacheResolvedSchemas = true;
+ /** Namespace to SchemaBinding cache */
+ private Map<String, SchemaBinding> schemasByUri = Collections.emptyMap();
+ /** Namespace to processAnnotations flag used with the XsdBinder.bind call */
+ private Map<String, Boolean> schemaParseAnnotationsByUri = Collections.emptyMap();
+ private Map<String, SchemaBindingInitializer> schemaInitByUri = Collections.emptyMap();
+
+ protected AbstractMutableSchemaResolver(Logger log)
+ {
+ this(log, new JBossEntityResolver());
+ }
+
+ protected AbstractMutableSchemaResolver(Logger log, JBossEntityResolver resolver)
+ {
+ if(log == null)
+ throw new IllegalArgumentException("Logger is null!");
+ this.log = log;
+ this.resolver = resolver;
+ }
+
+ public boolean isCacheResolvedSchemas()
+ {
+ return cacheResolvedSchemas;
+ }
+
+ /**
+ * Passing in true will make the schema resolver to cache successfully resolved
+ * schemas (which is the default) with namespace URI being the identifier of a schema.
+ * False will flush the cache and make the schema resolver to resolve schemas
+ * on each request.
+ * @param cacheResolvedSchemas
+ */
+ public void setCacheResolvedSchemas(boolean cacheResolvedSchemas)
+ {
+ this.cacheResolvedSchemas = cacheResolvedSchemas;
+ if(!cacheResolvedSchemas)
+ {
+ schemasByUri = Collections.emptyMap();
+ }
+ }
+
+ /**
+ * Registers a location for the namespace URI.<p>
+ *
+ * This location is looked using the JBossEntityResolver, i.e. it is a classpath location
+ *
+ * @param nsUri the namespace location
+ * @param location the classpath location
+ */
+ public void mapSchemaLocation(String nsUri, String location)
+ {
+ resolver.registerLocalEntity(nsUri, location);
+ }
+
+
+ /**
+ * Removes a location for the namespace URI.
+ *
+ * @todo actually remove it rather than setting null
+ * @param nsUri the namespace location
+ */
+ public void removeSchemaLocation(String nsUri)
+ {
+ resolver.registerLocalEntity(nsUri, null);
+ }
+
+ /**
+ * Whether to parse annotations for this namespace.
+ *
+ * @param nsUri the namespace
+ * @param value the value of the option
+ */
+ public void setParseXSDAnnotations(String nsUri, boolean value)
+ {
+ if (nsUri == null)
+ throw new IllegalArgumentException("Null namespace uri");
+ switch(schemaParseAnnotationsByUri.size())
+ {
+ case 0:
+ schemaParseAnnotationsByUri = Collections.singletonMap(nsUri, value);
+ break;
+ case 1:
+ schemaParseAnnotationsByUri = new HashMap<String, Boolean>(schemaParseAnnotationsByUri);
+ default:
+ schemaParseAnnotationsByUri.put(nsUri, value);
+ }
+ }
+
+ public Boolean unsetParseXSDAnnotations(String nsURI)
+ {
+ if (nsURI == null)
+ throw new IllegalArgumentException("Null namespace uri");
+ return schemaParseAnnotationsByUri.remove(nsURI);
+ }
+
+ /**
+ * Registers a SchemaBindingInitializer for the namespace URI.
+ * When the schema binding that corresponds to the namespace URI
+ * is resolved, the init(SchemaBinding schema) method will be invoked on the
+ * instance of SchemaBindingInitializer with the SchemaBinding returned from the
+ * XsdBinder.bind() method.
+ *
+ * @param nsUri the namespace URI to register the schema initializer for
+ * @param sbiClassName the class name SchemaBindingInitializer
+ * @throws Exception for any error
+ */
+ public void mapSchemaInitializer(String nsUri, String sbiClassName) throws Exception
+ {
+ Class<?> clazz = loadReference(sbiClassName);
+ Object object = clazz.newInstance();
+ if (object instanceof SchemaBindingInitializer == false)
+ throw new IllegalArgumentException(clazz.getName() + " is not an instance of " + SchemaBindingInitializer.class.getName());
+ SchemaBindingInitializer sbi = (SchemaBindingInitializer) object;
+ mapSchemaInitializer(nsUri, sbi);
+ }
+
+ /**
+ * Registers an instance of SchemaBindingInitializer for the namespace URI.
+ * When the schema binding that corresponds to the namespace URI
+ * is resolved, the init(SchemaBinding schema) method will be invoked on the
+ * instance of SchemaBindingInitializer with the SchemaBinding returned from the
+ * XsdBinder.bind() method.
+ *
+ * @param nsUri the namespace URI to register the schema initializer for
+ * @param sbi an instance of SchemaBindingInitializer
+ */
+ public void mapSchemaInitializer(String nsUri, SchemaBindingInitializer sbi)
+ {
+ if (nsUri == null)
+ throw new IllegalArgumentException("Null namespace uri");
+ if (sbi == null)
+ throw new IllegalArgumentException("Null schema binding initializer");
+ switch(schemaInitByUri.size())
+ {
+ case 0:
+ schemaInitByUri = Collections.singletonMap(nsUri, sbi);
+ break;
+ case 1:
+ schemaInitByUri = new HashMap<String, SchemaBindingInitializer>(schemaInitByUri);
+ default:
+ schemaInitByUri.put(nsUri, sbi);
+ }
+ }
+
+ /**
+ * Unregisters and returns the SchemaBindingInitializer for the namespace URI.
+ * @param nsUri the namespace URI to unregister SchemaBindingInitializer for
+ * @return unregistered SchemaBindingInitializer for the namespace URI or null
+ * if there was no SchemaBindingInitialzer registered for the namespace URI
+ */
+ public SchemaBindingInitializer removeSchemaInitializer(String nsUri)
+ {
+ if (nsUri == null)
+ throw new IllegalArgumentException("Null namespace uri");
+ return schemaInitByUri.remove(nsUri);
+ }
+
+ public String getBaseURI()
+ {
+ return baseURI;
+ }
+
+ public void setBaseURI(String baseURI)
+ {
+ this.baseURI = baseURI;
+ }
+
+ /**
+ * Uses the JBossEntityResolver.resolveEntity by:
+ *
+ * 1. Using the nsUri as the systemID
+ * 2. Using the schemaLocation as the systemID
+ * 3. If that fails, the baseURI is not null, the xsd is located using URL(baseURL, schemaLocation)
+ * 4. If the baseURI is null, the xsd is located using URL(schemaLocation)
+ */
+ public SchemaBinding resolve(String nsURI, String baseURI, String schemaLocation)
+ {
+ boolean trace = log.isTraceEnabled();
+ // Was the schema binding based on the nsURI
+ boolean foundByNS = false;
+ SchemaBinding schema = schemasByUri.get(nsURI);
+ if(schema != null)
+ {
+ if(trace)
+ log.trace("resolved cached schema, nsURI="+nsURI+", schema: " + schema);
+ return schema;
+ }
+
+ // Look for a class binding by schemaLocation
+ Class<?>[] classes = resolveClassFromSchemaLocation(schemaLocation, trace);
+ if (classes == null)
+ {
+ // Next look by namespace
+ classes = getClassesForURI(nsURI);
+ if(classes != null)
+ foundByNS = true;
+ }
+
+ if (classes != null)
+ {
+ if( trace )
+ {
+ log.trace("found bindingClass, nsURI=" + nsURI +
+ ", baseURI=" + baseURI +
+ ", schemaLocation=" + schemaLocation +
+ ", classes=" + Arrays.asList(classes));
+ }
+ schema = JBossXBBuilder.build(classes);
+ }
+ else
+ {
+ // Parse the schema
+ InputSource is = getInputSource(nsURI, baseURI, schemaLocation);
+ if( trace )
+ {
+ String msg = (is == null ? "couldn't find" : "found") +
+ " schema InputSource, nsURI=" + nsURI +
+ ", baseURI=" + baseURI + ", schemaLocation=" +
+ schemaLocation;
+ log.trace(msg);
+ }
+
+ if (is != null)
+ {
+ if( baseURI == null )
+ baseURI = this.baseURI;
+
+ Boolean processAnnotationsBoolean = schemaParseAnnotationsByUri.get(nsURI);
+ boolean processAnnotations = (processAnnotationsBoolean == null) ? true : processAnnotationsBoolean.booleanValue();
+ try
+ {
+ schema = XsdBinder.bind(is.getByteStream(), null, baseURI, processAnnotations);
+ foundByNS = true;
+ }
+ catch(RuntimeException e)
+ {
+ String msg = "Failed to parse schema for nsURI="+nsURI
+ +", baseURI="+baseURI
+ +", schemaLocation="+schemaLocation;
+ throw new JBossXBRuntimeException(msg, e);
+ }
+ }
+ }
+
+ if(schema != null)
+ {
+ schema.setSchemaResolver(this);
+ SchemaBindingInitializer sbi = schemaInitByUri.get(nsURI);
+ if(sbi != null)
+ schema = sbi.init(schema);
+
+ if(schema != null && nsURI.length() > 0 && cacheResolvedSchemas && foundByNS)
+ {
+ if(schemasByUri.isEmpty())
+ schemasByUri = new HashMap<String, SchemaBinding>();
+ schemasByUri.put(nsURI, schema);
+ }
+ }
+
+ if(trace)
+ log.trace("resolved schema: " + schema);
+
+ return schema;
+ }
+
+ public void mapURIToClass(String nsUri, String reference) throws ClassNotFoundException
+ {
+ mapURIToClass(nsUri, loadReference(reference));
+ }
+
+ public void mapURIToClasses(String nsUri, String... reference) throws ClassNotFoundException
+ {
+ Class<?>[] classes = new Class<?>[reference.length];
+ int i = 0;
+ for(String ref : reference)
+ classes[i++] = loadReference(ref);
+ mapURIToClasses(nsUri, classes);
+ }
+
+ public void mapLocationToClass(String schemaLocation, String reference) throws ClassNotFoundException
+ {
+ mapLocationToClass(schemaLocation, loadReference(reference));
+ }
+
+ public void mapLocationToClasses(String schemaLocation, String... reference) throws ClassNotFoundException
+ {
+ Class<?>[] classes = new Class<?>[reference.length];
+ int i = 0;
+ for(String ref : reference)
+ classes[i++] = loadReference(ref);
+ mapLocationToClasses(schemaLocation, classes);
+ }
+
+ protected Class<?> loadReference(String sbiClassName) throws ClassNotFoundException
+ {
+ if (sbiClassName == null)
+ throw new IllegalArgumentException("Null class name");
+ return Thread.currentThread().getContextClassLoader().loadClass(sbiClassName);
+ }
+
+ /**
+ * Lookup a binding class by schemaLocation. This first uses the
+ * schemaLocation as is, then parses this as a URI to obtain the
+ * final path component. This allows registration of a binding class
+ * using jboss_5_0.dtd rather than http://www.jboss.org/j2ee/schema/jboss_5_0.xsd
+ *
+ * @param schemaLocation the schema location from the parser
+ * @param trace - logging trace flag
+ * @return the binding class if found.
+ */
+ protected Class<?>[] resolveClassFromSchemaLocation(String schemaLocation, boolean trace)
+ {
+ Class<?>[] classes = getClassesForSchemaLocation(schemaLocation);
+ if (classes == null && schemaLocation != null && schemaLocation.length() > 0)
+ {
+ // Parse the schemaLocation as a uri to get the final path component
+ try
+ {
+ URI url = new URI(schemaLocation);
+ String path = url.getPath();
+ if( path == null )
+ path = url.getSchemeSpecificPart();
+ int slash = path.lastIndexOf('/');
+ String filename;
+ if( slash >= 0 )
+ filename = path.substring(slash + 1);
+ else
+ filename = path;
+
+ if(path.length() == 0)
+ return null;
+
+ if (trace)
+ log.trace("Mapped schemaLocation to filename: " + filename);
+ classes = getClassesForSchemaLocation(filename);
+ }
+ catch (URISyntaxException e)
+ {
+ if (trace)
+ log.trace("schemaLocation: is not a URI, using systemId as resource", e);
+ }
+ }
+ return classes;
+ }
+
+ public LSInput resolveAsLSInput(String nsURI, String baseURI, String schemaLocation)
+ {
+ LSInput lsInput = null;
+ InputSource is = getInputSource(nsURI, baseURI, schemaLocation);
+ if (is != null)
+ {
+ String publicId = is.getPublicId();
+ String systemId = is.getSystemId();
+ lsInput = new LSInputAdaptor(publicId, systemId, baseURI);
+ lsInput.setCharacterStream(is.getCharacterStream());
+ lsInput.setByteStream(is.getByteStream());
+ lsInput.setEncoding(is.getEncoding());
+ }
+ return lsInput;
+ }
+
+ private InputSource getInputSource(String nsURI, String baseURI, String schemaLocation)
+ {
+ boolean trace = log.isTraceEnabled();
+ InputSource is = null;
+
+ if( trace )
+ log.trace("getInputSource, nsURI="+nsURI+", baseURI="+baseURI+", schemaLocation="+schemaLocation);
+
+ // First try what is requested
+ try
+ {
+ is = resolver.resolveEntity(nsURI, schemaLocation);
+ if (trace)
+ {
+ String msg = (is == null ? "Couldn't resolve" : "Resolved") +
+ " schema using namespace as publicId and schemaLocation as systemId";
+ log.trace(msg);
+ }
+ }
+ catch (Exception e)
+ {
+ if (trace)
+ log.trace("Failed to use nsUri/schemaLocation", e);
+ }
+
+ // Next, try to use the baseURI to resolve the schema location
+ if(baseURI == null)
+ {
+ baseURI = this.baseURI;
+ }
+
+ if (is == null && baseURI != null && schemaLocation != null)
+ {
+ try
+ {
+ URL url = new URL(baseURI);
+ url = new URL(url, schemaLocation);
+ String resolvedSchemaLocation = url.toString();
+ // No point if the schema location was already absolute
+ if (schemaLocation.equals(resolvedSchemaLocation) == false)
+ {
+ is = resolver.resolveEntity(null, url.toString());
+ if( trace && is != null )
+ log.trace("Resolved schema location using baseURI");
+ }
+ }
+ catch (Exception e)
+ {
+ if (trace)
+ log.trace("Failed to use schema location with baseURI", e);
+ }
+ }
+
+ // Finally, just try the namespace as the system id
+ if (is == null && nsURI != null)
+ {
+ try
+ {
+ is = resolver.resolveEntity(null, nsURI);
+ if( trace && is != null )
+ log.trace("Resolved namespace as system id");
+ }
+ catch (Exception e)
+ {
+ if (trace)
+ log.trace("Failed to use namespace as system id", e);
+ }
+ }
+ if( trace )
+ {
+ log.trace("getInputSource, nsURI="+nsURI+", baseURI="
+ +baseURI+", schemaLocation="+schemaLocation+", is="+is);
+ }
+ return is;
+ }
+
+ protected abstract Class<?>[] getClassesForURI(String uri);
+
+ protected abstract Class<?>[] getClassesForSchemaLocation(String uri);
+}
+
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/MultiClassSchemaResolver.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/MultiClassSchemaResolver.java (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/MultiClassSchemaResolver.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -0,0 +1,113 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.xb.binding.resolver;
+
+import java.util.WeakHashMap;
+
+import org.jboss.logging.Logger;
+import org.jboss.util.xml.JBossEntityResolver;
+
+/**
+ * A default SchemaBindingResolver that uses a JBossEntityResolver to locate
+ * the schema xsd.
+ *
+ * @author Scott.Stark at jboss.org
+ * @author alex at jboss.org
+ * @version $Revision: 2913 $
+ */
+public class MultiClassSchemaResolver extends AbstractMutableSchemaResolver
+{
+ private static Logger log = Logger.getLogger(MultiClassSchemaResolver.class);
+
+ /** Namespace to JBossXBBuilder binding class */
+ private WeakHashMap<String, Class<?>[]> uriToClass = new WeakHashMap<String, Class<?>[]>();
+ /** SchemaLocation to JBossXBBuilder binding class */
+ private WeakHashMap<String, Class<?>[]> schemaLocationToClass = new WeakHashMap<String, Class<?>[]>();
+
+ public MultiClassSchemaResolver()
+ {
+ super(log);
+ }
+
+ public MultiClassSchemaResolver(JBossEntityResolver resolver)
+ {
+ super(log, resolver);
+ }
+
+ @Override
+ protected Class<?>[] getClassesForSchemaLocation(String uri)
+ {
+ return schemaLocationToClass.get(uri);
+ }
+
+ @Override
+ protected Class<?>[] getClassesForURI(String uri)
+ {
+ return uriToClass.get(uri);
+ }
+
+ public void mapLocationToClass(String schemaLocation, Class<?> clazz)
+ {
+ if(schemaLocation == null)
+ throw new IllegalArgumentException("schemaLocation is null");
+ if(clazz == null)
+ throw new IllegalArgumentException("clazz is null");
+ schemaLocationToClass.put(schemaLocation, new Class[]{clazz});
+ }
+
+ public void mapLocationToClasses(String schemaLocation, Class<?>... classes)
+ {
+ if(schemaLocation == null)
+ throw new IllegalArgumentException("schemaLocation is null");
+ if(classes == null)
+ throw new IllegalArgumentException("classes is null");
+ schemaLocationToClass.put(schemaLocation, classes);
+ }
+
+ public void mapURIToClass(String nsUri, Class<?> clazz)
+ {
+ if(nsUri == null)
+ throw new IllegalArgumentException("nsUri is null");
+ if(clazz == null)
+ throw new IllegalArgumentException("clazz is null");
+ uriToClass.put(nsUri, new Class[]{clazz});
+ }
+
+ public void mapURIToClasses(String nsUri, Class<?>... classes)
+ {
+ if(nsUri == null)
+ throw new IllegalArgumentException("schemaLocation is null");
+ if(classes == null)
+ throw new IllegalArgumentException("classes is null");
+ uriToClass.put(nsUri, classes);
+ }
+
+ public Class<?>[] removeLocationToClassMapping(String schemaLocation)
+ {
+ return schemaLocationToClass.remove(schemaLocation);
+ }
+
+ public Class<?>[] removeURIToClassMapping(String nsUri)
+ {
+ return uriToClass.remove(nsUri);
+ }
+}
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/MutableSchemaResolver.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/MutableSchemaResolver.java (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/resolver/MutableSchemaResolver.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -0,0 +1,200 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.xb.binding.resolver;
+
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingInitializer;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
+
+/**
+ * A MutableSchemaResolver.
+ *
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public interface MutableSchemaResolver extends SchemaBindingResolver
+{
+ /**
+ * @return true if resolved SchemaBinding's are cached, false otherwise
+ */
+ boolean isCacheResolvedSchemas();
+
+ /**
+ * If the implementation supports caching of the resolved schema bindings then
+ * passing in true will enable caching.
+ * False will (possibly) flush the cache and make the schema resolver resolve schemas
+ * on each request.
+ * @param cacheResolvedSchemas
+ */
+ void setCacheResolvedSchemas(boolean cacheResolvedSchemas);
+
+ /**
+ * Registers a location for the namespace URI.<p>
+ *
+ * The location may be a classpath location if the implementation supports searching in the classpath
+ * (e.g. using JBossEntityResolver)
+ *
+ * @param nsUri the namespace URI
+ * @param location the schema location
+ */
+ void mapSchemaLocation(String nsUri, String location);
+
+ /**
+ * Removes a location for the namespace URI.
+ *
+ * @param nsUri the namespace location
+ */
+ void removeSchemaLocation(String nsUri);
+
+ /**
+ * Whether to parse XSD annotations for this namespace.
+ *
+ * @param nsUri the namespace
+ * @param value the value of the option
+ */
+ void setParseXSDAnnotations(String nsUri, boolean value);
+
+ /**
+ * Clears the flag to parse XSD annotations for the namespace URI.
+ * The default setting for parsing XSD annotations will be applied to this namespace URI after this method is called.
+ *
+ * @param nsURI the namespace URI
+ * @return the value previously set or null, if no value was set for this namespace URI
+ */
+ Boolean unsetParseXSDAnnotations(String nsURI);
+
+ /**
+ * Registers a SchemaBindingInitializer for the namespace URI.
+ * When the schema binding that corresponds to the namespace URI
+ * is resolved, the init(SchemaBinding schema) method will be invoked on the
+ * instance of SchemaBindingInitializer with the SchemaBinding returned from the
+ * XsdBinder.bind() method.
+ *
+ * @param nsUri the namespace URI to register the schema initializer for
+ * @param sbiClassName the class name SchemaBindingInitializer
+ * @throws Exception for any error
+ */
+ void mapSchemaInitializer(String nsUri, String sbiClassName) throws Exception;
+
+ /**
+ * Registers an instance of SchemaBindingInitializer for the namespace URI.
+ * When the schema binding that corresponds to the namespace URI
+ * is resolved, the init(SchemaBinding schema) method will be invoked on the
+ * instance of SchemaBindingInitializer with the SchemaBinding returned from the
+ * XsdBinder.bind() method.
+ *
+ * @param nsUri the namespace URI to register the schema initializer for
+ * @param sbi an instance of SchemaBindingInitializer
+ */
+ void mapSchemaInitializer(String nsUri, SchemaBindingInitializer sbi);
+
+ /**
+ * Unregisters and returns the SchemaBindingInitializer for the namespace URI.
+ * @param nsUri the namespace URI to unregister SchemaBindingInitializer for
+ * @return unregistered SchemaBindingInitializer for the namespace URI or null
+ * if there was no SchemaBindingInitialzer registered for the namespace URI
+ */
+ SchemaBindingInitializer removeSchemaInitializer(String nsUri);
+
+ /**
+ * Maps a namespace URI to a class which will be used as the base for the SchemaBinding.
+ *
+ * @param nsUri the namespace URI
+ * @param reference fully qualified class name to build the SchemaBinding from
+ * @throws ClassNotFoundException if the reference cannot be loaded
+ */
+ void mapURIToClass(String nsUri, String reference) throws ClassNotFoundException;
+
+ /**
+ * Maps a namespace URI to a class which will be used as the base for the SchemaBinding.
+ *
+ * @param nsUri the namespace URI
+ * @param clazz class to build the SchemaBinding from
+ */
+ void mapURIToClass(String nsUri, Class<?> clazz);
+
+ /**
+ * Maps a namespace URI to an array of classes that will be used as the base for the SchemaBinding.
+ *
+ * @param nsUri the namespace URI
+ * @param reference array of fully qualified class names to build the SchemaBinding from
+ * @throws ClassNotFoundException if at least one of the references cannot be loaded
+ */
+ void mapURIToClasses(String nsUri, String... reference) throws ClassNotFoundException;
+
+ /**
+ * Maps a namespace URI to an array of classes that will be used as the base for the SchemaBinding.
+ *
+ * @param nsUri the namespace URI
+ * @param reference array of classes to build the SchemaBinding from
+ */
+ void mapURIToClasses(String nsUri, Class<?>... clazz);
+
+ /**
+ * Removes namespace URI to class mapping
+ *
+ * @param nsUri the namespace URI to unmap
+ * @return classes mapped to the namespace URI or null if the URI was not mapped.
+ */
+ Class<?>[] removeURIToClassMapping(String nsUri);
+
+ /**
+ * Maps schema location to a class which should be used as the base for the SchemaBinding.
+ *
+ * @param schemaLocation the location of the schema
+ * @param reference the fully qualified class name to build the SchemaBinding from
+ * @throws ClassNotFoundException if the reference cannot be loaded
+ */
+ void mapLocationToClass(String schemaLocation, String reference) throws ClassNotFoundException;
+
+ /**
+ * Maps schema location to a class which should be used as the base for the SchemaBinding.
+ *
+ * @param schemaLocation the location of the schema
+ * @param clazz the class to build the SchemaBinding from
+ */
+ void mapLocationToClass(String schemaLocation, Class<?> clazz);
+
+ /**
+ * Maps schema location to an array of classes that should be used as the base for the SchemaBinding.
+ *
+ * @param schemaLocation the location of the schema
+ * @param reference the array of fully qualified class names to build the SchemaBinding from
+ * @throws ClassNotFoundException if at least one of the references cannot be loaded
+ */
+ void mapLocationToClasses(String schemaLocation, String... reference) throws ClassNotFoundException;
+
+ /**
+ * Maps schema location to an array of classes that should be used as the base for the SchemaBinding.
+ *
+ * @param schemaLocation the location of the schema
+ * @param classes the array of classes to build the SchemaBinding from
+ */
+ void mapLocationToClasses(String schemaLocation, Class<?>... classes);
+
+ /**
+ * Removes schema location to class mapping.
+ *
+ * @param schemaLocation the schema location
+ * @return the array of classes used to build the SchemaBinding or null, if the schema location wasn't mapped.
+ */
+ Class<?>[] removeLocationToClassMapping(String schemaLocation);
+}
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -21,20 +21,11 @@
*/
package org.jboss.xb.binding.sunday.unmarshalling;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
import java.util.WeakHashMap;
import org.jboss.logging.Logger;
import org.jboss.util.xml.JBossEntityResolver;
-import org.jboss.xb.binding.JBossXBRuntimeException;
-import org.jboss.xb.builder.JBossXBBuilder;
-import org.w3c.dom.ls.LSInput;
-import org.xml.sax.InputSource;
+import org.jboss.xb.binding.resolver.AbstractMutableSchemaResolver;
/**
* A default SchemaBindingResolver that uses a JBossEntityResolver to locate
@@ -43,60 +34,28 @@
* @author Scott.Stark at jboss.org
* @version $Revision$
*/
-public class DefaultSchemaResolver implements SchemaBindingResolver, UriToClassMapping
+public class DefaultSchemaResolver extends AbstractMutableSchemaResolver
{
private static Logger log = Logger.getLogger(DefaultSchemaResolver.class);
- private String baseURI;
- private JBossEntityResolver resolver;
- private boolean cacheResolvedSchemas = true;
- /** Namespace to SchemaBinding cache */
- private Map<String, SchemaBinding> schemasByUri = Collections.emptyMap();
/** Namespace to JBossXBBuilder binding class */
private WeakHashMap<String, Class<?>> uriToClass = new WeakHashMap<String, Class<?>>();
/** SchemaLocation to JBossXBBuilder binding class */
private WeakHashMap<String, Class<?>> schemaLocationToClass = new WeakHashMap<String, Class<?>>();
- /** Namespace to SchemaBindingInitializer */
- private Map<String, SchemaBindingInitializer> schemaInitByUri = Collections.emptyMap();
- /** Namespace to processAnnotations flag used with the XsdBinder.bind call */
- private Map<String, Boolean> schemaParseAnnotationsByUri = Collections.emptyMap();
public DefaultSchemaResolver()
{
- this(new JBossEntityResolver());
+ super(log);
}
public DefaultSchemaResolver(JBossEntityResolver resolver)
{
- this.resolver = resolver;
+ super(log, resolver);
}
/**
- * @return true if resolved SchemaBinding's are cached, false otherwise
- */
- public boolean isCacheResolvedSchemas()
- {
- return cacheResolvedSchemas;
- }
-
- /**
- * Passing in true will make the schema resolver to cache successfully resolved
- * schemas (which is the default) with namespace URI being the identifier of a schema.
- * False will flush the cache and make the schema resolver to resolve schemas
- * on each request.
- * @param cacheResolvedSchemas
- */
- public void setCacheResolvedSchemas(boolean cacheResolvedSchemas)
- {
- this.cacheResolvedSchemas = cacheResolvedSchemas;
- if(!cacheResolvedSchemas)
- {
- schemasByUri = Collections.emptyMap();
- }
- }
-
- /**
* Registers a location for the namespace URI.<p>
+ * This method delegates to mapSchemaLocation(nsUri, location).
*
* This location is looked using the JBossEntityResolver, i.e. it is a classpath location
*
@@ -105,56 +64,32 @@
*/
public void addSchemaLocation(String nsUri, String location)
{
- resolver.registerLocalEntity(nsUri, location);
+ super.mapSchemaLocation(nsUri, location);
}
/**
- * Removes a location for the namespace URI.
- *
- * @todo actually remove it rather than setting null
- * @param nsUri the namespace location
- */
- public void removeSchemaLocation(String nsUri)
- {
- resolver.registerLocalEntity(nsUri, null);
- }
-
- /**
* Whether to parse annotations for this namespace.
+ * This method delegates to parseXSDAnnotations(nsUri, value).
*
* @param nsUri the namespace
* @param value the value of the option
*/
public void addSchemaParseAnnotations(String nsUri, Boolean value)
{
- if (nsUri == null)
- throw new IllegalArgumentException("Null namespace uri");
- if (value == null)
- throw new IllegalArgumentException("Null value");
- switch(schemaParseAnnotationsByUri.size())
- {
- case 0:
- schemaParseAnnotationsByUri = Collections.singletonMap(nsUri, value);
- break;
- case 1:
- schemaParseAnnotationsByUri = new HashMap<String, Boolean>(schemaParseAnnotationsByUri);
- default:
- schemaParseAnnotationsByUri.put(nsUri, value);
- }
+ super.setParseXSDAnnotations(nsUri, value);
}
/**
- * Removes the parse annotation configuration for this namespace
+ * Removes the parse annotation configuration for this namespace.
+ * This method delegates to unsetParseXSDAnnotations(nsURI).
*
* @param nsUri the namespace
* @return the previous value
*/
public Boolean removeSchemaParseAnnotations(String nsUri)
{
- if (nsUri == null)
- throw new IllegalArgumentException("Null namespace uri");
- return schemaParseAnnotationsByUri.remove(nsUri);
+ return super.unsetParseXSDAnnotations(nsUri);
}
/**
@@ -163,6 +98,8 @@
* is resolved, the init(SchemaBinding schema) method will be invoked on the
* instance of SchemaBindingInitializer with the SchemaBinding returned from the
* XsdBinder.bind() method.
+ *
+ * This method delegates to mapSchemaInitializer(nsUri, sbiClassName).
*
* @param nsUri the namespace URI to register the schema initializer for
* @param sbiClassName the class name SchemaBindingInitializer
@@ -170,14 +107,7 @@
*/
public void addSchemaInitializer(String nsUri, String sbiClassName) throws Exception
{
- if (sbiClassName == null)
- throw new IllegalArgumentException("Null class name");
- Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(sbiClassName);
- Object object = clazz.newInstance();
- if (object instanceof SchemaBindingInitializer == false)
- throw new IllegalArgumentException(clazz.getName() + " is not an instance of " + SchemaBindingInitializer.class.getName());
- SchemaBindingInitializer sbi = (SchemaBindingInitializer) object;
- addSchemaInitializer(nsUri, sbi);
+ super.mapSchemaInitializer(nsUri, sbiClassName);
}
/**
@@ -186,42 +116,18 @@
* is resolved, the init(SchemaBinding schema) method will be invoked on the
* instance of SchemaBindingInitializer with the SchemaBinding returned from the
* XsdBinder.bind() method.
+ *
+ * This method delegates to mapSchemaInitializer(nsUri, sbi).
*
* @param nsUri the namespace URI to register the schema initializer for
* @param sbi an instance of SchemaBindingInitializer
*/
public void addSchemaInitializer(String nsUri, SchemaBindingInitializer sbi)
{
- if (nsUri == null)
- throw new IllegalArgumentException("Null namespace uri");
- if (sbi == null)
- throw new IllegalArgumentException("Null schema binding initializer");
- switch(schemaInitByUri.size())
- {
- case 0:
- schemaInitByUri = Collections.singletonMap(nsUri, sbi);
- break;
- case 1:
- schemaInitByUri = new HashMap<String, SchemaBindingInitializer>(schemaInitByUri);
- default:
- schemaInitByUri.put(nsUri, sbi);
- }
+ super.mapSchemaInitializer(nsUri, sbi);
}
/**
- * Unregisters and returns the SchemaBindingInitializer for the namespace URI.
- * @param nsUri the namespace URI to unregister SchemaBindingInitializer for
- * @return unregistered SchemaBindingInitializer for the namespace URI or null
- * if there was no SchemaBindingInitialzer registered for the namespace URI
- */
- public SchemaBindingInitializer removeSchemaInitializer(String nsUri)
- {
- if (nsUri == null)
- throw new IllegalArgumentException("Null namespace uri");
- return schemaInitByUri.remove(nsUri);
- }
-
- /**
* Add an in-memory schema.
*
* @param nsUri schema namespace
@@ -230,18 +136,14 @@
*/
public void addClassBinding(String nsUri, String reference) throws ClassNotFoundException
{
- if (reference == null)
- throw new IllegalArgumentException("Null reference class");
-
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- Class<?> clazz = cl.loadClass(reference);
- addClassBinding(nsUri, clazz);
+ super.mapURIToClass(nsUri, reference);
}
public void addClassBinding(String nsUri, Class<?> clazz)
{
uriToClass.put(nsUri, clazz);
}
+
public Class<?> removeClassBinding(String nsUri)
{
return uriToClass.remove(nsUri);
@@ -251,288 +153,60 @@
{
schemaLocationToClass.put(schemaLocation, clazz);
}
+
public Class<?> removeClassBindingForLocation(String schemaLocation)
{
return schemaLocationToClass.remove(schemaLocation);
}
- public String getBaseURI()
+ @Override
+ protected Class<?>[] getClassesForSchemaLocation(String uri)
{
- return baseURI;
+ Class<?> c = schemaLocationToClass.get(uri);
+ return c == null ? null : new Class<?>[]{c};
}
- public void setBaseURI(String baseURI)
+ @Override
+ protected Class<?>[] getClassesForURI(String uri)
{
- this.baseURI = baseURI;
+ Class<?> c = uriToClass.get(uri);
+ return c == null ? null : new Class<?>[]{c};
}
- /**
- * Uses the JBossEntityResolver.resolveEntity by:
- *
- * 1. Using the nsUri as the systemID
- * 2. Using the schemaLocation as the systemID
- * 3. If that fails, the baseURI is not null, the xsd is located using URL(baseURL, schemaLocation)
- * 4. If the baseURI is null, the xsd is located using URL(schemaLocation)
- */
- public SchemaBinding resolve(String nsURI, String baseURI, String schemaLocation)
+ public void mapLocationToClass(String schemaLocation, Class<?> clazz)
{
- boolean trace = log.isTraceEnabled();
- // Was the schema binding based on the nsURI
- boolean foundByNS = false;
- SchemaBinding schema = schemasByUri.get(nsURI);
- if(schema != null)
- {
- if(trace)
- log.trace("resolved cached schema, nsURI="+nsURI+", schema: " + schema);
- return schema;
- }
-
- // Look for a class binding by schemaLocation
- Class<?> bindingClass = resolveClassFromSchemaLocation(schemaLocation, trace);
- if (bindingClass == null)
- {
- // Next look by namespace
- bindingClass = uriToClass.get(nsURI);
- if(bindingClass != null)
- foundByNS = true;
- }
- if (bindingClass != null)
- {
- if( trace )
- {
- log.trace("found bindingClass, nsURI="+nsURI
- +", baseURI="+baseURI
- +", schemaLocation="+schemaLocation
- +", class="+bindingClass);
- }
- schema = JBossXBBuilder.build(bindingClass);
- }
- else
- {
- // Parse the schema
- InputSource is = getInputSource(nsURI, baseURI, schemaLocation);
- if( trace )
- {
- String msg = (is == null ? "couldn't find" : "found") +
- " schema InputSource, nsURI=" + nsURI +
- ", baseURI=" + baseURI + ", schemaLocation=" +
- schemaLocation;
- log.trace(msg);
- }
-
- if (is != null)
- {
- if( baseURI == null )
- baseURI = this.baseURI;
-
- Boolean processAnnotationsBoolean = schemaParseAnnotationsByUri.get(nsURI);
- boolean processAnnotations = (processAnnotationsBoolean == null) ? true : processAnnotationsBoolean.booleanValue();
- try
- {
- schema = XsdBinder.bind(is.getByteStream(), null, baseURI, processAnnotations);
- foundByNS = true;
- }
- catch(RuntimeException e)
- {
- String msg = "Failed to parse schema for nsURI="+nsURI
- +", baseURI="+baseURI
- +", schemaLocation="+schemaLocation;
- throw new JBossXBRuntimeException(msg, e);
- }
- }
- }
-
- if(schema != null)
- {
- schema.setSchemaResolver(this);
- SchemaBindingInitializer sbi = schemaInitByUri.get(nsURI);
- if(sbi != null)
- {
- schema = sbi.init(schema);
- }
-
- if(schema != null && nsURI.length() > 0 && cacheResolvedSchemas && foundByNS)
- {
- if(schemasByUri.isEmpty())
- {
- schemasByUri = new HashMap<String, SchemaBinding>();
- }
- schemasByUri.put(nsURI, schema);
- }
- }
-
- if(trace)
- {
- log.trace("resolved schema: " + schema);
- }
-
- return schema;
+ this.addClassBindingForLocation(schemaLocation, clazz);
}
- /**
- * Lookup a binding class by schemaLocation. This first uses the
- * schemaLocation as is, then parses this as a URI to obtain the
- * final path component. This allows registration of a binding class
- * using jboss_5_0.dtd rather than http://www.jboss.org/j2ee/schema/jboss_5_0.xsd
- *
- * @param schemaLocation the schema location from the parser
- * @param trace - logging trace flag
- * @return the binding class if found.
- */
- protected Class<?> resolveClassFromSchemaLocation(String schemaLocation,
- boolean trace)
+ public void mapLocationToClasses(String schemaLocation, Class<?>... classes)
{
- Class<?> bindingClass = schemaLocationToClass.get(schemaLocation);
- if (bindingClass == null && schemaLocation != null && schemaLocation.length() > 0)
- {
- // Parse the schemaLocation as a uri to get the final path component
- try
- {
- URI url = new URI(schemaLocation);
- String path = url.getPath();
- if( path == null )
- path = url.getSchemeSpecificPart();
- int slash = path.lastIndexOf('/');
- String filename;
- if( slash >= 0 )
- filename = path.substring(slash + 1);
- else
- filename = path;
-
- if(path.length() == 0)
- return null;
-
- if (trace)
- log.trace("Mapped schemaLocation to filename: " + filename);
- bindingClass = schemaLocationToClass.get(filename);
- }
- catch (URISyntaxException e)
- {
- if (trace)
- log.trace("schemaLocation: is not a URI, using systemId as resource", e);
- }
- }
- return bindingClass;
+ throw new UnsupportedOperationException("This implementation supports schema location mapping to a single class only.");
}
- public LSInput resolveAsLSInput(String nsURI, String baseURI, String schemaLocation)
+ public void mapURIToClass(String nsUri, Class<?> clazz)
{
- LSInput lsInput = null;
- InputSource is = getInputSource(nsURI, baseURI, schemaLocation);
- if (is != null)
- {
- String publicId = is.getPublicId();
- String systemId = is.getSystemId();
- lsInput = new LSInputAdaptor(publicId, systemId, baseURI);
- lsInput.setCharacterStream(is.getCharacterStream());
- lsInput.setByteStream(is.getByteStream());
- lsInput.setEncoding(is.getEncoding());
- }
- return lsInput;
+ this.addClassBinding(nsUri, clazz);
}
- private InputSource getInputSource(String nsURI, String baseURI, String schemaLocation)
+ public void mapURIToClasses(String nsUri, String... reference) throws ClassNotFoundException
{
- boolean trace = log.isTraceEnabled();
- InputSource is = null;
-
- if( trace )
- log.trace("getInputSource, nsURI="+nsURI+", baseURI="+baseURI+", schemaLocation="+schemaLocation);
-
- // First try what is requested
- try
- {
- is = resolver.resolveEntity(nsURI, schemaLocation);
- if (trace)
- {
- String msg = (is == null ? "Couldn't resolve" : "Resolved") +
- " schema using namespace as publicId and schemaLocation as systemId";
- log.trace(msg);
- }
- }
- catch (Exception e)
- {
- if (trace)
- log.trace("Failed to use nsUri/schemaLocation", e);
- }
-
- // Next, try to use the baseURI to resolve the schema location
- if(baseURI == null)
- {
- baseURI = this.baseURI;
- }
-
- if (is == null && baseURI != null && schemaLocation != null)
- {
- try
- {
- URL url = new URL(baseURI);
- url = new URL(url, schemaLocation);
- String resolvedSchemaLocation = url.toString();
- // No point if the schema location was already absolute
- if (schemaLocation.equals(resolvedSchemaLocation) == false)
- {
- is = resolver.resolveEntity(null, url.toString());
- if( trace && is != null )
- log.trace("Resolved schema location using baseURI");
- }
- }
- catch (Exception e)
- {
- if (trace)
- log.trace("Failed to use schema location with baseURI", e);
- }
- }
-
- // Finally, just try the namespace as the system id
- if (is == null && nsURI != null)
- {
- try
- {
- is = resolver.resolveEntity(null, nsURI);
- if( trace && is != null )
- log.trace("Resolved namespace as system id");
- }
- catch (Exception e)
- {
- if (trace)
- log.trace("Failed to use namespace as system id", e);
- }
- }
- if( trace )
- {
- log.trace("getInputSource, nsURI="+nsURI+", baseURI="
- +baseURI+", schemaLocation="+schemaLocation+", is="+is);
- }
- return is;
+ throw new UnsupportedOperationException("This implementation supports URI mapping to a single class only.");
}
- public void mapUriToClass(String nsUri, String reference) throws ClassNotFoundException
+ public void mapURIToClasses(String nsUri, Class<?>... clazz)
{
- addClassBinding(nsUri, reference);
+ throw new UnsupportedOperationException("This implementation supports URI mapping to a single class only.");
}
- public void mapUriToClass(String nsUri, Class<?> clazz)
+ public Class<?>[] removeLocationToClassMapping(String schemaLocation)
{
- addClassBinding(nsUri, clazz);
+ Class<?> c = removeClassBindingForLocation(schemaLocation);
+ return c == null ? null : new Class[]{c};
}
- public void mapUriToClasses(String nsUri, String... reference) throws ClassNotFoundException
+ public Class<?>[] removeURIToClassMapping(String nsUri)
{
- throw new UnsupportedOperationException("Namespace URI mapping to multiple classes is not supported by this implementation.");
+ Class<?> c = this.removeClassBinding(nsUri);
+ return c == null ? null : new Class<?>[]{c};
}
-
- public void mapUriToClasses(String nsUri, Class<?>... clazz)
- {
- throw new UnsupportedOperationException("Namespace URI mapping to multiple classes is not supported by this implementation.");
- }
-
- public Class<?>[] removeUriToClassMapping(String nsUri)
- {
- Class<?> clazz = removeClassBinding(nsUri);
- if(clazz != null)
- return new Class<?>[]{clazz};
- return null;
- }
-
}
Deleted: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/MultiClassSchemaResolver.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/MultiClassSchemaResolver.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/MultiClassSchemaResolver.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -1,539 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.xb.binding.sunday.unmarshalling;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.WeakHashMap;
-
-import org.jboss.logging.Logger;
-import org.jboss.util.xml.JBossEntityResolver;
-import org.jboss.xb.binding.JBossXBRuntimeException;
-import org.jboss.xb.builder.JBossXBBuilder;
-import org.w3c.dom.ls.LSInput;
-import org.xml.sax.InputSource;
-
-/**
- * A default SchemaBindingResolver that uses a JBossEntityResolver to locate
- * the schema xsd.
- *
- * @author Scott.Stark at jboss.org
- * @author alex at jboss.org
- * @version $Revision: 2913 $
- */
-public class MultiClassSchemaResolver implements SchemaBindingResolver, UriToClassMapping
-{
- private static Logger log = Logger.getLogger(MultiClassSchemaResolver.class);
-
- private String baseURI;
- private JBossEntityResolver resolver;
- private boolean cacheResolvedSchemas = true;
- /** Namespace to SchemaBinding cache */
- private Map<String, SchemaBinding> schemasByUri = Collections.emptyMap();
- /** Namespace to JBossXBBuilder binding class */
- private WeakHashMap<String, Class<?>[]> uriToClass = new WeakHashMap<String, Class<?>[]>();
- /** SchemaLocation to JBossXBBuilder binding class */
- private WeakHashMap<String, Class<?>[]> schemaLocationToClass = new WeakHashMap<String, Class<?>[]>();
- /** Namespace to SchemaBindingInitializer */
- private Map<String, SchemaBindingInitializer> schemaInitByUri = Collections.emptyMap();
- /** Namespace to processAnnotations flag used with the XsdBinder.bind call */
- private Map<String, Boolean> schemaParseAnnotationsByUri = Collections.emptyMap();
-
- public MultiClassSchemaResolver()
- {
- this(new JBossEntityResolver());
- }
-
- public MultiClassSchemaResolver(JBossEntityResolver resolver)
- {
- this.resolver = resolver;
- }
-
- /**
- * @return true if resolved SchemaBinding's are cached, false otherwise
- */
- public boolean isCacheResolvedSchemas()
- {
- return cacheResolvedSchemas;
- }
-
- /**
- * Passing in true will make the schema resolver to cache successfully resolved
- * schemas (which is the default) with namespace URI being the identifier of a schema.
- * False will flush the cache and make the schema resolver to resolve schemas
- * on each request.
- * @param cacheResolvedSchemas
- */
- public void setCacheResolvedSchemas(boolean cacheResolvedSchemas)
- {
- this.cacheResolvedSchemas = cacheResolvedSchemas;
- if(!cacheResolvedSchemas)
- {
- schemasByUri = Collections.emptyMap();
- }
- }
-
- /**
- * Registers a location for the namespace URI.<p>
- *
- * This location is looked using the JBossEntityResolver, i.e. it is a classpath location
- *
- * @param nsUri the namespace location
- * @param location the classpath location
- */
- public void addSchemaLocation(String nsUri, String location)
- {
- resolver.registerLocalEntity(nsUri, location);
- }
-
-
- /**
- * Removes a location for the namespace URI.
- *
- * @todo actually remove it rather than setting null
- * @param nsUri the namespace location
- */
- public void removeSchemaLocation(String nsUri)
- {
- resolver.registerLocalEntity(nsUri, null);
- }
-
- /**
- * Whether to parse annotations for this namespace.
- *
- * @param nsUri the namespace
- * @param value the value of the option
- */
- public void addSchemaParseAnnotations(String nsUri, Boolean value)
- {
- if (nsUri == null)
- throw new IllegalArgumentException("Null namespace uri");
- if (value == null)
- throw new IllegalArgumentException("Null value");
- switch(schemaParseAnnotationsByUri.size())
- {
- case 0:
- schemaParseAnnotationsByUri = Collections.singletonMap(nsUri, value);
- break;
- case 1:
- schemaParseAnnotationsByUri = new HashMap<String, Boolean>(schemaParseAnnotationsByUri);
- default:
- schemaParseAnnotationsByUri.put(nsUri, value);
- }
- }
-
- /**
- * Removes the parse annotation configuration for this namespace
- *
- * @param nsUri the namespace
- * @return the previous value
- */
- public Boolean removeSchemaParseAnnotations(String nsUri)
- {
- if (nsUri == null)
- throw new IllegalArgumentException("Null namespace uri");
- return schemaParseAnnotationsByUri.remove(nsUri);
- }
-
- /**
- * Registers a SchemaBindingInitializer for the namespace URI.
- * When the schema binding that corresponds to the namespace URI
- * is resolved, the init(SchemaBinding schema) method will be invoked on the
- * instance of SchemaBindingInitializer with the SchemaBinding returned from the
- * XsdBinder.bind() method.
- *
- * @param nsUri the namespace URI to register the schema initializer for
- * @param sbiClassName the class name SchemaBindingInitializer
- * @throws Exception for any error
- */
- public void addSchemaInitializer(String nsUri, String sbiClassName) throws Exception
- {
- if (sbiClassName == null)
- throw new IllegalArgumentException("Null class name");
- Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(sbiClassName);
- Object object = clazz.newInstance();
- if (object instanceof SchemaBindingInitializer == false)
- throw new IllegalArgumentException(clazz.getName() + " is not an instance of " + SchemaBindingInitializer.class.getName());
- SchemaBindingInitializer sbi = (SchemaBindingInitializer) object;
- addSchemaInitializer(nsUri, sbi);
- }
-
- /**
- * Registers an instance of SchemaBindingInitializer for the namespace URI.
- * When the schema binding that corresponds to the namespace URI
- * is resolved, the init(SchemaBinding schema) method will be invoked on the
- * instance of SchemaBindingInitializer with the SchemaBinding returned from the
- * XsdBinder.bind() method.
- *
- * @param nsUri the namespace URI to register the schema initializer for
- * @param sbi an instance of SchemaBindingInitializer
- */
- public void addSchemaInitializer(String nsUri, SchemaBindingInitializer sbi)
- {
- if (nsUri == null)
- throw new IllegalArgumentException("Null namespace uri");
- if (sbi == null)
- throw new IllegalArgumentException("Null schema binding initializer");
- switch(schemaInitByUri.size())
- {
- case 0:
- schemaInitByUri = Collections.singletonMap(nsUri, sbi);
- break;
- case 1:
- schemaInitByUri = new HashMap<String, SchemaBindingInitializer>(schemaInitByUri);
- default:
- schemaInitByUri.put(nsUri, sbi);
- }
- }
-
- /**
- * Unregisters and returns the SchemaBindingInitializer for the namespace URI.
- * @param nsUri the namespace URI to unregister SchemaBindingInitializer for
- * @return unregistered SchemaBindingInitializer for the namespace URI or null
- * if there was no SchemaBindingInitialzer registered for the namespace URI
- */
- public SchemaBindingInitializer removeSchemaInitializer(String nsUri)
- {
- if (nsUri == null)
- throw new IllegalArgumentException("Null namespace uri");
- return schemaInitByUri.remove(nsUri);
- }
-
- /**
- * Add an in-memory schema.
- *
- * @param nsUri schema namespace
- * @param reference the schema reference class name
- * @throws Exception for any error
- */
- public void addClassBinding(String nsUri, String... reference) throws ClassNotFoundException
- {
- if (reference == null)
- throw new IllegalArgumentException("Null reference class");
-
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- Class<?>[] classes = new Class<?>[reference.length];
- for(int i = 0; i < classes.length; ++i)
- classes[i] = cl.loadClass(reference[i]);
- addClassBinding(nsUri, classes);
- }
-
- public void addClassBinding(String nsUri, Class<?>... clazz)
- {
- uriToClass.put(nsUri, clazz);
- }
-
- public Class<?>[] removeClassBinding(String nsUri)
- {
- return uriToClass.remove(nsUri);
- }
-
- public void addClassBindingForLocation(String schemaLocation, Class<?>... clazz)
- {
- schemaLocationToClass.put(schemaLocation, clazz);
- }
-
- public Class<?>[] removeClassBindingForLocation(String schemaLocation)
- {
- return schemaLocationToClass.remove(schemaLocation);
- }
-
- public String getBaseURI()
- {
- return baseURI;
- }
-
- public void setBaseURI(String baseURI)
- {
- this.baseURI = baseURI;
- }
-
- /**
- * Uses the JBossEntityResolver.resolveEntity by:
- *
- * 1. Using the nsUri as the systemID
- * 2. Using the schemaLocation as the systemID
- * 3. If that fails, the baseURI is not null, the xsd is located using URL(baseURL, schemaLocation)
- * 4. If the baseURI is null, the xsd is located using URL(schemaLocation)
- */
- public SchemaBinding resolve(String nsURI, String baseURI, String schemaLocation)
- {
- boolean trace = log.isTraceEnabled();
- // Was the schema binding based on the nsURI
- boolean foundByNS = false;
- SchemaBinding schema = schemasByUri.get(nsURI);
- if(schema != null)
- {
- if(trace)
- log.trace("resolved cached schema, nsURI="+nsURI+", schema: " + schema);
- return schema;
- }
-
- // Look for a class binding by schemaLocation
- Class<?>[] classes = resolveClassFromSchemaLocation(schemaLocation, trace);
- if (classes == null)
- {
- // Next look by namespace
- classes = uriToClass.get(nsURI);
- if(classes != null)
- foundByNS = true;
- }
- if (classes != null)
- {
- if( trace )
- {
- log.trace("found bindingClass, nsURI="+nsURI
- +", baseURI="+baseURI
- +", schemaLocation="+schemaLocation
- +", classes="+Arrays.asList(classes));
- }
- schema = JBossXBBuilder.build(classes);
- }
- else
- {
- // Parse the schema
- InputSource is = getInputSource(nsURI, baseURI, schemaLocation);
- if( trace )
- {
- String msg = (is == null ? "couldn't find" : "found") +
- " schema InputSource, nsURI=" + nsURI +
- ", baseURI=" + baseURI + ", schemaLocation=" +
- schemaLocation;
- log.trace(msg);
- }
-
- if (is != null)
- {
- if( baseURI == null )
- baseURI = this.baseURI;
-
- Boolean processAnnotationsBoolean = schemaParseAnnotationsByUri.get(nsURI);
- boolean processAnnotations = (processAnnotationsBoolean == null) ? true : processAnnotationsBoolean.booleanValue();
- try
- {
- schema = XsdBinder.bind(is.getByteStream(), null, baseURI, processAnnotations);
- foundByNS = true;
- }
- catch(RuntimeException e)
- {
- String msg = "Failed to parse schema for nsURI="+nsURI
- +", baseURI="+baseURI
- +", schemaLocation="+schemaLocation;
- throw new JBossXBRuntimeException(msg, e);
- }
- }
- }
-
- if(schema != null)
- {
- schema.setSchemaResolver(this);
- SchemaBindingInitializer sbi = schemaInitByUri.get(nsURI);
- if(sbi != null)
- {
- schema = sbi.init(schema);
- }
-
- if(schema != null && nsURI.length() > 0 && cacheResolvedSchemas && foundByNS)
- {
- if(schemasByUri.isEmpty())
- {
- schemasByUri = new HashMap<String, SchemaBinding>();
- }
- schemasByUri.put(nsURI, schema);
- }
- }
-
- if(trace)
- {
- log.trace("resolved schema: " + schema);
- }
-
- return schema;
- }
-
- /**
- * Lookup a binding class by schemaLocation. This first uses the
- * schemaLocation as is, then parses this as a URI to obtain the
- * final path component. This allows registration of a binding class
- * using jboss_5_0.dtd rather than http://www.jboss.org/j2ee/schema/jboss_5_0.xsd
- *
- * @param schemaLocation the schema location from the parser
- * @param trace - logging trace flag
- * @return the binding class if found.
- */
- protected Class<?>[] resolveClassFromSchemaLocation(String schemaLocation, boolean trace)
- {
- Class<?>[] classes = schemaLocationToClass.get(schemaLocation);
- if (classes == null && schemaLocation != null && schemaLocation.length() > 0)
- {
- // Parse the schemaLocation as a uri to get the final path component
- try
- {
- URI url = new URI(schemaLocation);
- String path = url.getPath();
- if( path == null )
- path = url.getSchemeSpecificPart();
- int slash = path.lastIndexOf('/');
- String filename;
- if( slash >= 0 )
- filename = path.substring(slash + 1);
- else
- filename = path;
-
- if(path.length() == 0)
- return null;
-
- if (trace)
- log.trace("Mapped schemaLocation to filename: " + filename);
- classes = schemaLocationToClass.get(filename);
- }
- catch (URISyntaxException e)
- {
- if (trace)
- log.trace("schemaLocation: is not a URI, using systemId as resource", e);
- }
- }
- return classes;
- }
-
- public LSInput resolveAsLSInput(String nsURI, String baseURI, String schemaLocation)
- {
- LSInput lsInput = null;
- InputSource is = getInputSource(nsURI, baseURI, schemaLocation);
- if (is != null)
- {
- String publicId = is.getPublicId();
- String systemId = is.getSystemId();
- lsInput = new LSInputAdaptor(publicId, systemId, baseURI);
- lsInput.setCharacterStream(is.getCharacterStream());
- lsInput.setByteStream(is.getByteStream());
- lsInput.setEncoding(is.getEncoding());
- }
- return lsInput;
- }
-
- private InputSource getInputSource(String nsURI, String baseURI, String schemaLocation)
- {
- boolean trace = log.isTraceEnabled();
- InputSource is = null;
-
- if( trace )
- log.trace("getInputSource, nsURI="+nsURI+", baseURI="+baseURI+", schemaLocation="+schemaLocation);
-
- // First try what is requested
- try
- {
- is = resolver.resolveEntity(nsURI, schemaLocation);
- if (trace)
- {
- String msg = (is == null ? "Couldn't resolve" : "Resolved") +
- " schema using namespace as publicId and schemaLocation as systemId";
- log.trace(msg);
- }
- }
- catch (Exception e)
- {
- if (trace)
- log.trace("Failed to use nsUri/schemaLocation", e);
- }
-
- // Next, try to use the baseURI to resolve the schema location
- if(baseURI == null)
- {
- baseURI = this.baseURI;
- }
-
- if (is == null && baseURI != null && schemaLocation != null)
- {
- try
- {
- URL url = new URL(baseURI);
- url = new URL(url, schemaLocation);
- String resolvedSchemaLocation = url.toString();
- // No point if the schema location was already absolute
- if (schemaLocation.equals(resolvedSchemaLocation) == false)
- {
- is = resolver.resolveEntity(null, url.toString());
- if( trace && is != null )
- log.trace("Resolved schema location using baseURI");
- }
- }
- catch (Exception e)
- {
- if (trace)
- log.trace("Failed to use schema location with baseURI", e);
- }
- }
-
- // Finally, just try the namespace as the system id
- if (is == null && nsURI != null)
- {
- try
- {
- is = resolver.resolveEntity(null, nsURI);
- if( trace && is != null )
- log.trace("Resolved namespace as system id");
- }
- catch (Exception e)
- {
- if (trace)
- log.trace("Failed to use namespace as system id", e);
- }
- }
- if( trace )
- {
- log.trace("getInputSource, nsURI="+nsURI+", baseURI="
- +baseURI+", schemaLocation="+schemaLocation+", is="+is);
- }
- return is;
- }
-
- public void mapUriToClass(String nsUri, String reference) throws ClassNotFoundException
- {
- addClassBinding(nsUri, reference);
- }
-
- public void mapUriToClass(String nsUri, Class<?> clazz)
- {
- addClassBinding(nsUri, clazz);
- }
-
- public void mapUriToClasses(String nsUri, String... reference) throws ClassNotFoundException
- {
- addClassBinding(nsUri, reference);
- }
-
- public void mapUriToClasses(String nsUri, Class<?>... clazz)
- {
- addClassBinding(nsUri, clazz);
- }
-
- public Class<?>[] removeUriToClassMapping(String nsUri)
- {
- return this.removeClassBinding(nsUri);
- }
-}
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfig.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfig.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaResolverConfig.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -26,6 +26,7 @@
import java.util.Properties;
import org.jboss.logging.Logger;
+import org.jboss.xb.binding.resolver.MutableSchemaResolver;
/**
* SchemaResolverConfig.
@@ -40,7 +41,7 @@
private static final Logger log = Logger.getLogger(SchemaResolverConfig.class);
/** The singleton schema resolver */
- protected static MultiClassSchemaResolver resolver = (MultiClassSchemaResolver) SingletonSchemaResolverFactory.getInstance().getSchemaBindingResolver();
+ protected static MutableSchemaResolver resolver = SingletonSchemaResolverFactory.getInstance().getSchemaBindingResolver();
/** The initializers by namespace */
protected Properties schemaInitializers;
@@ -83,7 +84,7 @@
String initializer = (String) entry.getValue();
try
{
- resolver.addSchemaInitializer(namespace, initializer);
+ resolver.mapSchemaInitializer(namespace, initializer);
}
catch (Exception ignored)
{
@@ -108,7 +109,7 @@
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
String namespace = (String) entry.getKey();
String location = (String) entry.getValue();
- resolver.addSchemaLocation(namespace, location);
+ resolver.mapSchemaLocation(namespace, location);
}
}
}
@@ -128,8 +129,9 @@
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
String namespace = (String) entry.getKey();
String value = (String) entry.getValue();
- Boolean booleanValue = Boolean.valueOf(value);
- resolver.addSchemaParseAnnotations(namespace, booleanValue);
+ Boolean booleanValue = Boolean.valueOf(value);
+ if(booleanValue != null)
+ resolver.setParseXSDAnnotations(namespace, booleanValue);
}
}
}
@@ -153,7 +155,7 @@
try
{
Class<?> clazz = loader.loadClass(value);
- resolver.addClassBindingForLocation(schemaLocation, clazz);
+ resolver.mapLocationToClass(schemaLocation, clazz);
}
catch(ClassNotFoundException e)
{
@@ -182,7 +184,7 @@
try
{
Class<?> clazz = loader.loadClass(value);
- resolver.addClassBinding(namespace, clazz);
+ resolver.mapURIToClass(namespace, clazz);
}
catch(ClassNotFoundException e)
{
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SingletonSchemaResolverFactory.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SingletonSchemaResolverFactory.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SingletonSchemaResolverFactory.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -22,6 +22,8 @@
package org.jboss.xb.binding.sunday.unmarshalling;
import org.jboss.logging.Logger;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
+import org.jboss.xb.binding.resolver.MutableSchemaResolver;
/**
* SingletonSchemaResolverFactory.
@@ -39,7 +41,7 @@
private static final SingletonSchemaResolverFactory singleton = new SingletonSchemaResolverFactory();
/** The resolver */
- private final MultiClassSchemaResolver resolver = new MultiClassSchemaResolver();
+ private final MutableSchemaResolver resolver = new MultiClassSchemaResolver();
/**
* Get the factory instance
@@ -73,7 +75,7 @@
addJaxbSchema("urn:jboss:deployers:2.0", "org.jboss.deployers.vfs.plugins.xb.SchemaResolverDeployerMetaData");
}
- public SchemaBindingResolver getSchemaBindingResolver()
+ public MutableSchemaResolver getSchemaBindingResolver()
{
return resolver;
}
@@ -89,7 +91,7 @@
{
try
{
- resolver.addSchemaInitializer(namespace, initializer);
+ resolver.mapSchemaInitializer(namespace, initializer);
if (log.isTraceEnabled())
log.trace("Mapped initializer '" + namespace + "' to '" + initializer + "'");
return true;
@@ -132,7 +134,7 @@
return false;
try
{
- resolver.addSchemaLocation(namespace, location);
+ resolver.mapSchemaLocation(namespace, location);
if (log.isTraceEnabled())
log.trace("Mapped location '" + namespace + "' to '" + location + "'");
return true;
@@ -170,7 +172,10 @@
*/
protected void setParseAnnotations(String namespace, Boolean parseAnnotations)
{
- resolver.addSchemaParseAnnotations(namespace, parseAnnotations);
+ if(parseAnnotations != null)
+ resolver.setParseXSDAnnotations(namespace, parseAnnotations);
+ else
+ resolver.unsetParseXSDAnnotations(namespace);
if (log.isTraceEnabled())
log.trace("Parse annotations '" + namespace + "' set to '" + parseAnnotations + "'");
}
@@ -186,7 +191,7 @@
{
try
{
- resolver.addClassBinding(namespace, reference);
+ resolver.mapURIToClass(namespace, reference);
if (log.isTraceEnabled())
log.trace("Mapped '" + namespace + "' to '" + reference + "'");
return true;
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -47,6 +47,7 @@
import org.jboss.xb.binding.metadata.PropertyMetaData;
import org.jboss.xb.binding.metadata.ValueMetaData;
import org.jboss.xb.binding.parser.JBossXBParser;
+import org.jboss.xb.binding.resolver.MutableSchemaResolver;
import org.jboss.xb.binding.sunday.xop.XOPIncludeHandler;
import org.xml.sax.Attributes;
@@ -963,13 +964,13 @@
private Attributes preprocessAttributes(Attributes attrs)
{
SchemaBindingResolver resolver = schemaResolver == null ? schema.getSchemaResolver() : schemaResolver;
- if(resolver == null || !(resolver instanceof UriToClassMapping))
+ if(resolver == null || !(resolver instanceof MutableSchemaResolver))
return attrs;
int ind = attrs.getIndex(Constants.NS_JBXB, "schemabinding");
if (ind != -1)
{
- UriToClassMapping defaultResolver = (UriToClassMapping)resolver;
+ MutableSchemaResolver defaultResolver = (MutableSchemaResolver)resolver;
String value = attrs.getValue(ind);
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(value);
while(tokenizer.hasMoreTokens())
@@ -980,7 +981,7 @@
String cls = tokenizer.nextToken();
try
{
- defaultResolver.mapUriToClass(uri, cls);
+ defaultResolver.mapURIToClass(uri, cls);
}
catch (Exception e)
{
Deleted: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UriToClassMapping.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UriToClassMapping.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UriToClassMapping.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -1,41 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.xb.binding.sunday.unmarshalling;
-
-/**
- * A ClassToSchemaMapping.
- *
- * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
- * @version $Revision: 1.1 $
- */
-public interface UriToClassMapping
-{
- public void mapUriToClass(String nsUri, String reference) throws ClassNotFoundException;
-
- public void mapUriToClass(String nsUri, Class<?> clazz);
-
- public void mapUriToClasses(String nsUri, String... reference) throws ClassNotFoundException;
-
- public void mapUriToClasses(String nsUri, Class<?>... clazz);
-
- public Class<?>[] removeUriToClassMapping(String nsUri);
-}
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/XsdBinder.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/XsdBinder.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/XsdBinder.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -55,6 +55,7 @@
import org.jboss.xb.binding.Constants;
import org.jboss.xb.binding.JBossXBRuntimeException;
import org.jboss.xb.binding.Util;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.xop.XOPIncludeHandler;
import org.jboss.xb.binding.metadata.AddMethodMetaData;
import org.jboss.xb.binding.metadata.CharactersMetaData;
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/AbstractBuilderTest.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/AbstractBuilderTest.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/AbstractBuilderTest.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -27,8 +27,8 @@
import org.jboss.test.AbstractTestCaseWithSetup;
import org.jboss.test.AbstractTestDelegate;
import org.jboss.util.UnreachableStatementException;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.unmarshalling.ModelGroupBinding;
-import org.jboss.xb.binding.sunday.unmarshalling.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.unmarshalling.DefaultHandlers;
import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
import org.jboss.xb.binding.sunday.unmarshalling.ParticleHandler;
@@ -111,8 +111,8 @@
if (dot != -1)
name = name.substring(0, dot);
String testXsd = packageName + '/' + name + ".xsd";
- resolver.addSchemaLocation(nsURI, testXsd);
- resolver.addSchemaInitializer(nsURI, JBossXBBuilder.newInitializer(expected));
+ resolver.mapSchemaLocation(nsURI, testXsd);
+ resolver.mapSchemaInitializer(nsURI, JBossXBBuilder.newInitializer(expected));
String testXml = findTestXml();
Object o = unmarshal(testXml, expected, resolver);
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/JBossXBTestDelegate.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/JBossXBTestDelegate.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/JBossXBTestDelegate.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -28,7 +28,7 @@
import org.jboss.test.AbstractTestDelegate;
import org.jboss.xb.binding.Unmarshaller;
import org.jboss.xb.binding.UnmarshallerFactory;
-import org.jboss.xb.binding.sunday.unmarshalling.MultiClassSchemaResolver;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
import org.jboss.xb.binding.sunday.unmarshalling.SingletonSchemaResolverFactory;
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/JbxbSchemaBindingAttributeUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/JbxbSchemaBindingAttributeUnitTestCase.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/JbxbSchemaBindingAttributeUnitTestCase.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -24,7 +24,7 @@
import org.jboss.test.xb.builder.AbstractBuilderTest;
import org.jboss.test.xml.jbxb.schemabindingattribute.Ns2Root;
import org.jboss.test.xml.jbxb.schemabindingattribute.Root;
-import org.jboss.xb.binding.sunday.unmarshalling.MultiClassSchemaResolver;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
import org.jboss.xb.builder.JBossXBBuilder;
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/SchemaBindingInitializerUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/SchemaBindingInitializerUnitTestCase.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/SchemaBindingInitializerUnitTestCase.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -28,7 +28,7 @@
import org.jboss.test.xml.initializer.Simple;
import org.jboss.test.xml.initializer.SimpleInitializer;
import org.jboss.xb.binding.JBossXBException;
-import org.jboss.xb.binding.sunday.unmarshalling.MultiClassSchemaResolver;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
/**
@@ -43,12 +43,12 @@
{
Class<?> clazz = SchemaBindingInitializerUnitTestCase.class;
MultiClassSchemaResolver resolver = new MultiClassSchemaResolver();
- resolver.addSchemaInitializer(SimpleInitializer.NS, SimpleInitializer.class.getName());
+ resolver.mapSchemaInitializer(SimpleInitializer.NS, SimpleInitializer.class.getName());
String location = getSchemaLocation(clazz, "SchemaBindingInitializerUnitTestCaseSimple.xsd");
- resolver.addSchemaLocation(SimpleInitializer.NS, location);
- resolver.addSchemaInitializer(ContainerInitializer.NS, ContainerInitializer.class.getName());
+ resolver.mapSchemaLocation(SimpleInitializer.NS, location);
+ resolver.mapSchemaInitializer(ContainerInitializer.NS, ContainerInitializer.class.getName());
location = getSchemaLocation(clazz, "SchemaBindingInitializerUnitTestCaseContainer.xsd");
- resolver.addSchemaLocation(ContainerInitializer.NS, location);
+ resolver.mapSchemaLocation(ContainerInitializer.NS, location);
return resolver;
}
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/SchemaIncludeUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/SchemaIncludeUnitTestCase.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/SchemaIncludeUnitTestCase.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -25,7 +25,7 @@
import org.jboss.test.BaseTestCase;
import org.jboss.xb.binding.Util;
-import org.jboss.xb.binding.sunday.unmarshalling.MultiClassSchemaResolver;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
/**
* SchemaIncludeUnitTestCase.
@@ -48,7 +48,7 @@
MultiClassSchemaResolver resolver = new MultiClassSchemaResolver();
URL url = getClass().getResource("/xml/SchemaIncludeUnitTestCase.xsd");
assertNotNull(url);
- resolver.addSchemaLocation("urn:jbosstest:schemainclude", url.toString());
+ resolver.mapSchemaLocation("urn:jbosstest:schemainclude", url.toString());
Util.loadSchema(url.toString(), resolver);
}
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardWrapperUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardWrapperUnitTestCase.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardWrapperUnitTestCase.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -30,7 +30,7 @@
import org.jboss.test.xml.pojoserver.metadata.PropertyMetaData;
import org.jboss.test.xml.pojoserver.metadata.StringValueMetaData;
import org.jboss.test.xml.pojoserver.metadata.ValueMetaData;
-import org.jboss.xb.binding.sunday.unmarshalling.MultiClassSchemaResolver;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
/**
@@ -45,12 +45,12 @@
{
Class<?> clazz = WildcardWrapperUnitTestCase.class;
MultiClassSchemaResolver resolver = new MultiClassSchemaResolver();
- resolver.addSchemaInitializer(PropertyInitializer.NS, PropertyInitializer.class.getName());
+ resolver.mapSchemaInitializer(PropertyInitializer.NS, PropertyInitializer.class.getName());
String location = getSchemaLocation(clazz, "WildcardWrapperUnitTestCase.xsd");
- resolver.addSchemaLocation(PropertyInitializer.NS, location);
- resolver.addSchemaInitializer(SimpleInitializer.NS, SimpleInitializer.class.getName());
+ resolver.mapSchemaLocation(PropertyInitializer.NS, location);
+ resolver.mapSchemaInitializer(SimpleInitializer.NS, SimpleInitializer.class.getName());
location = getSchemaLocation(clazz, "SchemaBindingInitializerUnitTestCaseSimple.xsd");
- resolver.addSchemaLocation(SimpleInitializer.NS, location);
+ resolver.mapSchemaLocation(SimpleInitializer.NS, location);
return resolver;
}
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnitTestCase.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/XOPUnitTestCase.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -23,8 +23,8 @@
import org.jboss.xb.binding.Unmarshaller;
import org.jboss.xb.binding.UnmarshallerFactory;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.marshalling.MarshallerImpl;
-import org.jboss.xb.binding.sunday.unmarshalling.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
@@ -254,7 +254,7 @@
MultiClassSchemaResolver resolver = new MultiClassSchemaResolver();
String xmimeXsd = getSchemaLocation(getClass(), "xmlmime.xsd");
- resolver.addSchemaLocation("http://www.w3.org/2005/05/xmlmime", xmimeXsd);
+ resolver.mapSchemaLocation("http://www.w3.org/2005/05/xmlmime", xmimeXsd);
SCHEMA = XsdBinder.bind(testXsd, resolver);
SCHEMA.setIgnoreUnresolvedFieldOrClass(false);
@@ -610,7 +610,7 @@
MultiClassSchemaResolver resolver = new MultiClassSchemaResolver();
String xmimeXsd = getSchemaLocation(getClass(), "xmlmime.xsd");
- resolver.addSchemaLocation("http://www.w3.org/2005/05/xmlmime", xmimeXsd);
+ resolver.mapSchemaLocation("http://www.w3.org/2005/05/xmlmime", xmimeXsd);
SchemaBinding schema = XsdBinder.bind(new StringReader(xsd), null, resolver);
schema.setIgnoreUnresolvedFieldOrClass(false);
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/XercesBugTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/XercesBugTestCase.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/XercesBugTestCase.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -35,7 +35,7 @@
import org.jboss.test.BaseTestCase;
import org.jboss.xb.binding.Constants;
import org.jboss.xb.binding.Util;
-import org.jboss.xb.binding.sunday.unmarshalling.MultiClassSchemaResolver;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
import org.jboss.xb.binding.sunday.unmarshalling.XsdBinderTerminatingErrorHandler;
import org.w3c.dom.DOMConfiguration;
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/jbxb/minOccurs/Schema1UnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/jbxb/minOccurs/Schema1UnitTestCase.java 2009-03-05 21:28:52 UTC (rev 3020)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/jbxb/minOccurs/Schema1UnitTestCase.java 2009-03-06 10:05:02 UTC (rev 3021)
@@ -24,7 +24,7 @@
import java.net.URL;
-import org.jboss.xb.binding.sunday.unmarshalling.MultiClassSchemaResolver;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.Unmarshaller;
import org.jboss.xb.binding.UnmarshallerFactory;
import org.jboss.test.xml.AbstractJBossXBTest;
@@ -45,7 +45,7 @@
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
MultiClassSchemaResolver resolver = new MultiClassSchemaResolver();
- resolver.addSchemaLocation("urn:jboss:jbxb-minOccurs-schema1", "xml/jbxb/minOccurs/schema1.xsd");
+ resolver.mapSchemaLocation("urn:jboss:jbxb-minOccurs-schema1", "xml/jbxb/minOccurs/schema1.xsd");
Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
URL testSchema1 = loader.getResource("xml/jbxb/minOccurs/testSchema1.xml");
@@ -56,7 +56,7 @@
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
MultiClassSchemaResolver resolver = new MultiClassSchemaResolver();
- resolver.addSchemaLocation("urn:jboss:jbxb-minOccurs-schema1", "xml/jbxb/minOccurs/schema1v2.xsd");
+ resolver.mapSchemaLocation("urn:jboss:jbxb-minOccurs-schema1", "xml/jbxb/minOccurs/schema1v2.xsd");
Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
URL testSchema1 = loader.getResource("xml/jbxb/minOccurs/testSchema1.xml");
More information about the jboss-svn-commits
mailing list