Author: jochen
Date: Thu Aug 24 02:44:19 2006
New Revision: 434361

URL: http://svn.apache.org/viewvc?rev=434361&view=rev
Log:
External schema bindings are now supported recursively.

Modified:
    
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java
    webservices/jaxme/branches/MAVEN/status.xml
    
webservices/jaxme/branches/b0_5/src/jaxme/org/apache/ws/jaxme/generator/impl/Inliner.java
    webservices/jaxme/branches/b0_5/status.xml

Modified: 
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java
URL: 
http://svn.apache.org/viewvc/webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java?rev=434361&r1=434360&r2=434361&view=diff
==============================================================================
--- 
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java
 (original)
+++ 
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java
 Thu Aug 24 02:44:19 2006
@@ -15,24 +15,32 @@
  */
 package org.apache.ws.jaxme.generator.impl;
 
+import java.io.File;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
+import java.io.StringWriter;
 
+import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 
+import org.apache.ws.jaxme.logging.Logger;
+import org.apache.ws.jaxme.logging.LoggerAccess;
 import org.apache.ws.jaxme.util.DOMSerializer;
 import org.apache.ws.jaxme.xs.SchemaTransformer;
 import org.apache.ws.jaxme.xs.XSParser;
 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBParser;
+import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -49,12 +57,7 @@
  * http://wiki.apache.org/ws/JaxMe/ExternalSchemaBindings</a>.
  */
 public class Inliner implements SchemaTransformer {
-       private static final DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
-       static {
-               dbf.setNamespaceAware(true);
-               dbf.setValidating(false);
-       }
-
+       private static final Logger log = LoggerAccess.getLogger(Inliner.class);
        private final Document[] bindings;
        private XMLReader transformedXMLReader;
        private InputSource transformedInputSource;
@@ -65,54 +68,84 @@
                bindings = pBindings;
        }
 
+       private DocumentBuilder newDocumentBuilder() throws 
ParserConfigurationException {
+               final DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
+               dbf.setNamespaceAware(true);
+               dbf.setValidating(false);
+               return dbf.newDocumentBuilder();
+       }
+
        private Document read(InputSource pSource, XMLReader pReader)
                        throws SAXException, ParserConfigurationException, 
IOException {
-               DocumentBuilder db = dbf.newDocumentBuilder();
+               DocumentBuilder db = newDocumentBuilder();
                db.setEntityResolver(pReader.getEntityResolver());
                db.setErrorHandler(pReader.getErrorHandler());
                return db.parse(pSource);
        }
 
-       private void apply(Document pSchema, Document pBindings, String pURI) 
throws XPathExpressionException {
-               for (Iterator iter = getBindingElements(pBindings, pURI);  
iter.hasNext();  ) {
-                       Element e = (Element) iter.next();
-                       String xpathQuery = e.getAttribute("node");
-                       XPathFactory xpathFactory = XPathFactory.newInstance();
-                       XPath xpath = xpathFactory.newXPath();
-                       NodeList nodes = (NodeList) xpath.evaluate(xpathQuery, 
e, XPathConstants.NODESET);
+       private boolean isURIMatching(String pURI, Element pBindings) {
+               final Attr schemaLocationAttr = 
pBindings.getAttributeNodeNS(null, "schemaLocation");
+               if (schemaLocationAttr == null) {
+                       return true;
+               }
+               String value = schemaLocationAttr.getValue();
+               if (value.equals(pURI)) {
+                       return true;
+               }
+               /* I'd prefer not to use the following. However, fact is that 
we do see the operating
+                * systems absolute path or a path relative to a projects base 
directory or stuff like
+                * that in most cases and not the URI, which the user specifies 
in its project.
+                */
+               if (pURI == null) {
+                       return false;
+               }
+               if (pURI.endsWith("/" + value)) {
+                       return true;
+               }
+               if (!File.separator.equals("/")  &&  
pURI.endsWith(File.separator + value)) {
+                       return true;
+               }
+               return false;
+       }
+
+       private void apply(Element pSchema, Element pBindings, String pURI) 
throws XPathExpressionException {
+               final Attr nodeAttr = pBindings.getAttributeNodeNS(null, 
"node");
+               if (!isURIMatching(pURI, pBindings)) {
+                       return;
+               }
+               if (nodeAttr == null) {
+                       importChilds(pSchema, pBindings, pURI);
+               } else {
+                       final XPath xpath = 
XPathFactory.newInstance().newXPath();
+                       final NodeList nodes = (NodeList) 
xpath.evaluate(nodeAttr.getValue(), pSchema, XPathConstants.NODESET);
                        for (int i = 0;  i < nodes.getLength();  i++) {
-                               Node node = nodes.item(i);
-                               if (node.getNodeType() != Node.ELEMENT_NODE) {
-                                       continue;
-                               }
-                               Element match = (Element) node;
-                               if 
(!XSParser.XML_SCHEMA_URI.equals(match.getNamespaceURI())) {
-                                       continue;
-                               }
-                               String prefix = match.getPrefix();
-                               Element annotationElement = getChild(match, 
prefix, "annotation");
-                               Element appInfoElement = 
getChild(annotationElement, prefix, "appInfo");
-                               for (Node child = e.getFirstChild();  child != 
null;  child = child.getNextSibling()) {
-                                       
appInfoElement.appendChild(pSchema.importNode(child, true));
+                               final Node node = nodes.item(i);
+                               if (node.getNodeType() == Node.ELEMENT_NODE
+                                       &&  
XSParser.XML_SCHEMA_URI.equals(node.getNamespaceURI())) {
+                                       importChilds((Element) node, pBindings, 
pURI);
                                }
                        }
                }
        }
 
-       private Iterator getBindingElements(Document pBindings, String pURI) {
-               Element root = pBindings.getDocumentElement();
-               List result = new ArrayList();
-               for (Node child = root.getFirstChild();  child != null;  child 
= child.getNextSibling()) {
-                       if (child.getNodeType() == Node.ELEMENT_NODE) {
-                               Element e = (Element) child;
-                               if 
(JAXBParser.JAXB_SCHEMA_URI.equals(e.getNamespaceURI())  &&
-                                       "bindings".equals(e.getLocalName())  &&
-                                       
pURI.equals(e.getAttribute("schemaLocation"))) {
-                                       result.add(e);
-                               }
+       private void importChilds(Element pTarget, Node pSource, String pURI) 
throws XPathExpressionException {
+               final Document doc = pTarget.getOwnerDocument();
+               DocumentFragment df = doc.createDocumentFragment();
+               for (Node child = pSource.getFirstChild();  child != null;  
child = child.getNextSibling()) {
+                       if (child.getNodeType() == Node.ELEMENT_NODE
+                               &&  
JAXBParser.JAXB_SCHEMA_URI.equals(child.getNamespaceURI())
+                               &&  "bindings".equals(child.getLocalName())) {
+                               apply(pTarget, (Element) child, pURI);
+                       } else {
+                               df.appendChild(doc.importNode(child, true));
                        }
                }
-               return result.iterator();
+               if (df.hasChildNodes()) {
+                       String prefix = pTarget.getPrefix();
+                       Element annotationElement = getChild(pTarget, prefix, 
"annotation");
+                       Element appInfoElement = getChild(annotationElement, 
prefix, "appInfo");
+                       appInfoElement.appendChild(df);
+               }
        }
 
        private Element getChild(Element pParent, String pPrefix, String pName) 
{
@@ -131,17 +164,35 @@
                return e;
        }
 
+       private String asString(Document pDocument) throws SAXException {
+               final StringWriter sw = new StringWriter();
+               try {
+                       
TransformerFactory.newInstance().newTransformer().transform(new 
DOMSource(pDocument), new StreamResult(sw));
+               } catch (TransformerException e) {
+                       throw new SAXException(e);
+               }
+               return sw.toString();
+       }
+
        public void parse(InputSource pSource, XMLReader pReader) throws 
ParserConfigurationException, SAXException, IOException {
+               final String mName = "parse";
+               final String uri = pSource.getSystemId();
+               log.entering(mName, uri);
                final Document schema = read(pSource, pReader);
-               String uri = pSource.getSystemId();
-               if (uri != null) {
-                       try {
-                               for (int i = 0;  i < bindings.length;  i++) {
-                                       apply(schema, bindings[i], uri);
+               try {
+                       for (int i = 0;  i < bindings.length;  i++) {
+                               Element bindingsElement = 
bindings[i].getDocumentElement();
+                               if 
(!JAXBParser.JAXB_SCHEMA_URI.equals(bindingsElement.getNamespaceURI())
+                                               ||  
!"bindings".equals(bindingsElement.getLocalName())) {
+                                       throw new SAXException("Expected " +
+                                                       new 
QName(JAXBParser.JAXB_SCHEMA_URI, "bindings") +
+                                                       " as a binding files 
root element, got " +
+                                                       new 
QName(bindingsElement.getNamespaceURI(), bindingsElement.getLocalName()));
                                }
-                       } catch (XPathExpressionException e) {
-                               throw new SAXException(e);
+                               apply(schema.getDocumentElement(), 
bindingsElement, uri);
                        }
+               } catch (XPathExpressionException e) {
+                       throw new SAXException(e);
                }
                transformedInputSource = new InputSource();
                transformedXMLReader = new XMLFilterImpl(){

Modified: webservices/jaxme/branches/MAVEN/status.xml
URL: 
http://svn.apache.org/viewvc/webservices/jaxme/branches/MAVEN/status.xml?rev=434361&r1=434360&r2=434361&view=diff
==============================================================================
--- webservices/jaxme/branches/MAVEN/status.xml (original)
+++ webservices/jaxme/branches/MAVEN/status.xml Thu Aug 24 02:44:19 2006
@@ -34,7 +34,7 @@
       <action dev="JW" type="fix" context="generator">
         The IDREF type has been implemented as string and not
         as a reference to another object, as it should be.
-        (JAXME-58, Fredrik Vraalsen <fredrik at vraalsen.no>)
+        (JAXME-58, Fredrik Vraalsen, fredrik at vraalsen.no)
       </action>
       <action dev="JW" type="enhancement" context="generator">
         Added support for xs:pattern.
@@ -57,6 +57,9 @@
       <action dev="JW" type="fix" context="jm">
         Fixed a serious performance problem, if mixed content elements
         had large embedded text segments.
+      </action>
+      <action dev="JW" type="add" context="jm">
+        External schema bindings are now supported recursively.
       </action>
     </release>
        <release version="0.5.1" date="Not yet published">

Modified: 
webservices/jaxme/branches/b0_5/src/jaxme/org/apache/ws/jaxme/generator/impl/Inliner.java
URL: 
http://svn.apache.org/viewvc/webservices/jaxme/branches/b0_5/src/jaxme/org/apache/ws/jaxme/generator/impl/Inliner.java?rev=434361&r1=434360&r2=434361&view=diff
==============================================================================
--- 
webservices/jaxme/branches/b0_5/src/jaxme/org/apache/ws/jaxme/generator/impl/Inliner.java
 (original)
+++ 
webservices/jaxme/branches/b0_5/src/jaxme/org/apache/ws/jaxme/generator/impl/Inliner.java
 Thu Aug 24 02:44:19 2006
@@ -15,24 +15,32 @@
  */
 package org.apache.ws.jaxme.generator.impl;
 
+import java.io.File;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
+import java.io.StringWriter;
 
+import javax.xml.namespace.QName;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
 
+import org.apache.ws.jaxme.logging.Logger;
+import org.apache.ws.jaxme.logging.LoggerAccess;
 import org.apache.ws.jaxme.util.DOMSerializer;
 import org.apache.ws.jaxme.xs.SchemaTransformer;
 import org.apache.ws.jaxme.xs.XSParser;
 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBParser;
+import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
+import org.w3c.dom.DocumentFragment;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -49,12 +57,7 @@
  * http://wiki.apache.org/ws/JaxMe/ExternalSchemaBindings</a>.
  */
 public class Inliner implements SchemaTransformer {
-       private static final DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
-       static {
-               dbf.setNamespaceAware(true);
-               dbf.setValidating(false);
-       }
-
+       private static final Logger log = LoggerAccess.getLogger(Inliner.class);
        private final Document[] bindings;
        private XMLReader transformedXMLReader;
        private InputSource transformedInputSource;
@@ -65,54 +68,84 @@
                bindings = pBindings;
        }
 
+       private DocumentBuilder newDocumentBuilder() throws 
ParserConfigurationException {
+               final DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
+               dbf.setNamespaceAware(true);
+               dbf.setValidating(false);
+               return dbf.newDocumentBuilder();
+       }
+
        private Document read(InputSource pSource, XMLReader pReader)
                        throws SAXException, ParserConfigurationException, 
IOException {
-               DocumentBuilder db = dbf.newDocumentBuilder();
+               DocumentBuilder db = newDocumentBuilder();
                db.setEntityResolver(pReader.getEntityResolver());
                db.setErrorHandler(pReader.getErrorHandler());
                return db.parse(pSource);
        }
 
-       private void apply(Document pSchema, Document pBindings, String pURI) 
throws XPathExpressionException {
-               for (Iterator iter = getBindingElements(pBindings, pURI);  
iter.hasNext();  ) {
-                       Element e = (Element) iter.next();
-                       String xpathQuery = e.getAttribute("node");
-                       XPathFactory xpathFactory = XPathFactory.newInstance();
-                       XPath xpath = xpathFactory.newXPath();
-                       NodeList nodes = (NodeList) xpath.evaluate(xpathQuery, 
e, XPathConstants.NODESET);
+       private boolean isURIMatching(String pURI, Element pBindings) {
+               final Attr schemaLocationAttr = 
pBindings.getAttributeNodeNS(null, "schemaLocation");
+               if (schemaLocationAttr == null) {
+                       return true;
+               }
+               String value = schemaLocationAttr.getValue();
+               if (value.equals(pURI)) {
+                       return true;
+               }
+               /* I'd prefer not to use the following. However, fact is that 
we do see the operating
+                * systems absolute path or a path relative to a projects base 
directory or stuff like
+                * that in most cases and not the URI, which the user specifies 
in its project.
+                */
+               if (pURI == null) {
+                       return false;
+               }
+               if (pURI.endsWith("/" + value)) {
+                       return true;
+               }
+               if (!File.separator.equals("/")  &&  
pURI.endsWith(File.separator + value)) {
+                       return true;
+               }
+               return false;
+       }
+
+       private void apply(Element pSchema, Element pBindings, String pURI) 
throws XPathExpressionException {
+               final Attr nodeAttr = pBindings.getAttributeNodeNS(null, 
"node");
+               if (!isURIMatching(pURI, pBindings)) {
+                       return;
+               }
+               if (nodeAttr == null) {
+                       importChilds(pSchema, pBindings, pURI);
+               } else {
+                       final XPath xpath = 
XPathFactory.newInstance().newXPath();
+                       final NodeList nodes = (NodeList) 
xpath.evaluate(nodeAttr.getValue(), pSchema, XPathConstants.NODESET);
                        for (int i = 0;  i < nodes.getLength();  i++) {
-                               Node node = nodes.item(i);
-                               if (node.getNodeType() != Node.ELEMENT_NODE) {
-                                       continue;
-                               }
-                               Element match = (Element) node;
-                               if 
(!XSParser.XML_SCHEMA_URI.equals(match.getNamespaceURI())) {
-                                       continue;
-                               }
-                               String prefix = match.getPrefix();
-                               Element annotationElement = getChild(match, 
prefix, "annotation");
-                               Element appInfoElement = 
getChild(annotationElement, prefix, "appInfo");
-                               for (Node child = e.getFirstChild();  child != 
null;  child = child.getNextSibling()) {
-                                       
appInfoElement.appendChild(pSchema.importNode(child, true));
+                               final Node node = nodes.item(i);
+                               if (node.getNodeType() == Node.ELEMENT_NODE
+                                       &&  
XSParser.XML_SCHEMA_URI.equals(node.getNamespaceURI())) {
+                                       importChilds((Element) node, pBindings, 
pURI);
                                }
                        }
                }
        }
 
-       private Iterator getBindingElements(Document pBindings, String pURI) {
-               Element root = pBindings.getDocumentElement();
-               List result = new ArrayList();
-               for (Node child = root.getFirstChild();  child != null;  child 
= child.getNextSibling()) {
-                       if (child.getNodeType() == Node.ELEMENT_NODE) {
-                               Element e = (Element) child;
-                               if 
(JAXBParser.JAXB_SCHEMA_URI.equals(e.getNamespaceURI())  &&
-                                       "bindings".equals(e.getLocalName())  &&
-                                       
pURI.equals(e.getAttribute("schemaLocation"))) {
-                                       result.add(e);
-                               }
+       private void importChilds(Element pTarget, Node pSource, String pURI) 
throws XPathExpressionException {
+               final Document doc = pTarget.getOwnerDocument();
+               DocumentFragment df = doc.createDocumentFragment();
+               for (Node child = pSource.getFirstChild();  child != null;  
child = child.getNextSibling()) {
+                       if (child.getNodeType() == Node.ELEMENT_NODE
+                               &&  
JAXBParser.JAXB_SCHEMA_URI.equals(child.getNamespaceURI())
+                               &&  "bindings".equals(child.getLocalName())) {
+                               apply(pTarget, (Element) child, pURI);
+                       } else {
+                               df.appendChild(doc.importNode(child, true));
                        }
                }
-               return result.iterator();
+               if (df.hasChildNodes()) {
+                       String prefix = pTarget.getPrefix();
+                       Element annotationElement = getChild(pTarget, prefix, 
"annotation");
+                       Element appInfoElement = getChild(annotationElement, 
prefix, "appInfo");
+                       appInfoElement.appendChild(df);
+               }
        }
 
        private Element getChild(Element pParent, String pPrefix, String pName) 
{
@@ -131,17 +164,35 @@
                return e;
        }
 
+       private String asString(Document pDocument) throws SAXException {
+               final StringWriter sw = new StringWriter();
+               try {
+                       
TransformerFactory.newInstance().newTransformer().transform(new 
DOMSource(pDocument), new StreamResult(sw));
+               } catch (TransformerException e) {
+                       throw new SAXException(e);
+               }
+               return sw.toString();
+       }
+
        public void parse(InputSource pSource, XMLReader pReader) throws 
ParserConfigurationException, SAXException, IOException {
+               final String mName = "parse";
+               final String uri = pSource.getSystemId();
+               log.entering(mName, uri);
                final Document schema = read(pSource, pReader);
-               String uri = pSource.getSystemId();
-               if (uri != null) {
-                       try {
-                               for (int i = 0;  i < bindings.length;  i++) {
-                                       apply(schema, bindings[i], uri);
+               try {
+                       for (int i = 0;  i < bindings.length;  i++) {
+                               Element bindingsElement = 
bindings[i].getDocumentElement();
+                               if 
(!JAXBParser.JAXB_SCHEMA_URI.equals(bindingsElement.getNamespaceURI())
+                                               ||  
!"bindings".equals(bindingsElement.getLocalName())) {
+                                       throw new SAXException("Expected " +
+                                                       new 
QName(JAXBParser.JAXB_SCHEMA_URI, "bindings") +
+                                                       " as a binding files 
root element, got " +
+                                                       new 
QName(bindingsElement.getNamespaceURI(), bindingsElement.getLocalName()));
                                }
-                       } catch (XPathExpressionException e) {
-                               throw new SAXException(e);
+                               apply(schema.getDocumentElement(), 
bindingsElement, uri);
                        }
+               } catch (XPathExpressionException e) {
+                       throw new SAXException(e);
                }
                transformedInputSource = new InputSource();
                transformedXMLReader = new XMLFilterImpl(){

Modified: webservices/jaxme/branches/b0_5/status.xml
URL: 
http://svn.apache.org/viewvc/webservices/jaxme/branches/b0_5/status.xml?rev=434361&r1=434360&r2=434361&view=diff
==============================================================================
--- webservices/jaxme/branches/b0_5/status.xml (original)
+++ webservices/jaxme/branches/b0_5/status.xml Thu Aug 24 02:44:19 2006
@@ -54,6 +54,9 @@
         Fixed a build problem with hsqldb, when the database was already in 
use.
         (Boris Gruschke, boris at gruschke.de, JAXME-81)
       </action>
+      <action dev="JW" type="add" context="jm">
+        External schema bindings are now supported recursively.
+      </action>
     </release>
     <release version="0.5.1" date="06-Jan-2006">
       <action dev="JW" type="enhancement" context="js">



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to