[jboss-svn-commits] JBoss Common SVN: r2633 - jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Oct 11 11:44:40 EDT 2007
Author: scott.stark at jboss.org
Date: 2007-10-11 11:44:39 -0400 (Thu, 11 Oct 2007)
New Revision: 2633
Modified:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java
Log:
Add a removeClassBindingForLocation method, take the schemaLocation class binding ahead of a namespace binding, and parse the schema location to its final component when matching
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 2007-10-11 02:03:32 UTC (rev 2632)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/DefaultSchemaResolver.java 2007-10-11 15:44:39 UTC (rev 2633)
@@ -21,6 +21,8 @@
*/
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;
@@ -47,12 +49,15 @@
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()
@@ -219,13 +224,18 @@
{
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 removeClassBinding(String nsUri)
+ public Class removeClassBindingForLocation(String schemaLocation)
{
- return uriToClass.remove(nsUri);
+ return schemaLocationToClass.remove(schemaLocation);
}
public String getBaseURI()
@@ -248,26 +258,43 @@
*/
public SchemaBinding resolve(String nsURI, String baseURI, String schemaLocation)
{
+ boolean trace = log.isTraceEnabled();
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
- Class bindingClass = uriToClass.get(nsURI);
+ // Look for a class binding by schemaLocation
+ Class bindingClass = resolveClassFromSchemaLocation(schemaLocation, trace);
if (bindingClass == null)
- bindingClass = schemaLocationToClass.get(schemaLocation);
+ {
+ // Next look by namespace
+ bindingClass = uriToClass.get(nsURI);
+ }
if (bindingClass != null)
{
- if( log.isTraceEnabled() )
- log.trace("resolve, nsURI="+nsURI+", baseURI="+baseURI+", class="+bindingClass);
+ 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 )
+ {
+ log.trace("found schema InputSource, nsURI="+nsURI
+ +", baseURI="+baseURI
+ +", schemaLocation="+schemaLocation);
+ }
if (is != null)
{
@@ -289,7 +316,7 @@
schema = sbi.init(schema);
}
- if(schema != null && cacheResolvedSchemas)
+ if(schema != null && nsURI.length() > 0 && cacheResolvedSchemas)
{
if(schemasByUri == Collections.EMPTY_MAP)
{
@@ -299,7 +326,7 @@
}
}
- if(log.isTraceEnabled())
+ if(trace)
{
log.trace("resolved schema: " + schema);
}
@@ -307,6 +334,52 @@
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 bindingClass = schemaLocationToClass.get(schemaLocation);
+ if (bindingClass == null)
+ {
+ // 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;
+ }
+
public LSInput resolveAsLSInput(String nsURI, String baseURI, String schemaLocation)
{
LSInput lsInput = null;
@@ -394,4 +467,5 @@
}
return is;
}
+
}
More information about the jboss-svn-commits
mailing list