Author: jochen
Date: Sat Dec 24 16:49:45 2005
New Revision: 358959

URL: http://svn.apache.org/viewcvs?rev=358959&view=rev
Log:
Added support for external binding files, based on a suggestion from
Ortwin Glueck.

Added:
    
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java
    
webservices/jaxme/branches/MAVEN/projects/xs/src/main/java/org/apache/ws/jaxme/xs/SchemaTransformer.java
Modified:
    
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/sg/impl/JAXBSGFactory.java

Added: 
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java
URL: 
http://svn.apache.org/viewcvs/webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java?rev=358959&view=auto
==============================================================================
--- 
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java
 (added)
+++ 
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/impl/Inliner.java
 Sat Dec 24 16:49:45 2005
@@ -0,0 +1,165 @@
+/*
+ * Copyright 2003, 2004  The Apache Software Foundation
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ws.jaxme.generator.impl;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+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.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.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLFilterImpl;
+
+
+/** Performs the modifications on an XML schema file, as
+ * specified by an external binding file. This is based
+ * on a suggestion from Ortwin Glück, see
+ * <a href="http://wiki.apache.org/ws/JaxMe/ExternalSchemaBindings";>
+ * 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 final Document[] bindings;
+       private XMLReader transformedXMLReader;
+       private InputSource transformedInputSource;
+
+       /** Creates a new instance with the given bindings.
+        */
+       public Inliner(Document[] pBindings) {
+               bindings = pBindings;
+       }
+
+       private Document read(InputSource pSource, XMLReader pReader)
+                       throws SAXException, ParserConfigurationException, 
IOException {
+               DocumentBuilder db = dbf.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);
+                       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));
+                               }
+                       }
+               }
+       }
+
+       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);
+                               }
+                       }
+               }
+               return result.iterator();
+       }
+
+       private Element getChild(Element pParent, String pPrefix, String pName) 
{
+               for (Node child = pParent.getFirstChild();  child != null;  
child = child.getNextSibling()) {
+                       if (child.getNodeType() == Node.ELEMENT_NODE) {
+                               Element e = (Element) child;
+                               if (pName.equals(e.getLocalName())  &&  
XSParser.XML_SCHEMA_URI.equals(e.getNamespaceURI())) {
+                                       return e;
+                               }
+                       }
+               }
+
+               String qName = (pPrefix == null || pPrefix.length() == 0) ? 
pName : pPrefix + ":" + pName;
+               Element e = 
pParent.getOwnerDocument().createElementNS(XSParser.XML_SCHEMA_URI, qName);
+               pParent.insertBefore(e, pParent.getFirstChild());
+               return e;
+       }
+
+       public void parse(InputSource pSource, XMLReader pReader) throws 
ParserConfigurationException, SAXException, IOException {
+               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);
+                               }
+                       } catch (XPathExpressionException e) {
+                               throw new SAXException(e);
+                       }
+               }
+               transformedInputSource = new InputSource();
+               transformedXMLReader = new XMLFilterImpl(){
+                       public void parse(InputSource pInput) throws 
SAXException, IOException {
+                               new DOMSerializer().serialize(schema, this);
+                       }
+
+                       public void parse(String pSystemId) throws 
SAXException, IOException {
+                               throw new IllegalStateException("Not 
implemented");
+                       }
+               };
+       }
+
+       public InputSource getTransformedInputSource() {
+               return transformedInputSource;
+       }
+
+       public XMLReader getTransformedXMLReader() {
+               return transformedXMLReader;
+       }
+}

Modified: 
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/sg/impl/JAXBSGFactory.java
URL: 
http://svn.apache.org/viewcvs/webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/sg/impl/JAXBSGFactory.java?rev=358959&r1=358958&r2=358959&view=diff
==============================================================================
--- 
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/sg/impl/JAXBSGFactory.java
 (original)
+++ 
webservices/jaxme/branches/MAVEN/projects/jm/src/main/java/org/apache/ws/jaxme/generator/sg/impl/JAXBSGFactory.java
 Sat Dec 24 16:49:45 2005
@@ -24,7 +24,7 @@
 import javax.xml.parsers.ParserConfigurationException;
 
 import org.apache.ws.jaxme.generator.Generator;
-import org.apache.ws.jaxme.generator.Inliner;
+import org.apache.ws.jaxme.generator.impl.Inliner;
 import org.apache.ws.jaxme.generator.sg.Context;
 import org.apache.ws.jaxme.generator.sg.Facet;
 import org.apache.ws.jaxme.generator.sg.GroupSG;

Added: 
webservices/jaxme/branches/MAVEN/projects/xs/src/main/java/org/apache/ws/jaxme/xs/SchemaTransformer.java
URL: 
http://svn.apache.org/viewcvs/webservices/jaxme/branches/MAVEN/projects/xs/src/main/java/org/apache/ws/jaxme/xs/SchemaTransformer.java?rev=358959&view=auto
==============================================================================
--- 
webservices/jaxme/branches/MAVEN/projects/xs/src/main/java/org/apache/ws/jaxme/xs/SchemaTransformer.java
 (added)
+++ 
webservices/jaxme/branches/MAVEN/projects/xs/src/main/java/org/apache/ws/jaxme/xs/SchemaTransformer.java
 Sat Dec 24 16:49:45 2005
@@ -0,0 +1,31 @@
+package org.apache.ws.jaxme.xs;
+
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+
+
+/** A schema transformer is able to modify a schema, which
+ * is being read. This is used, for example, to implement
+ * the external JAXB binding files.
+ */
+public interface SchemaTransformer {
+       /** Reads the given input source.
+        */
+       public void parse(InputSource pSource, XMLReader pReader)
+                       throws ParserConfigurationException, SAXException, 
IOException;
+
+       /** Returns the new input source. Called after
+        * [EMAIL PROTECTED] #parse(InputSource, XMLReader)}.
+        */
+       public InputSource getTransformedInputSource();
+
+       /** Returns the new XML reader. Called after
+        * [EMAIL PROTECTED] #parse(InputSource, XMLReader)}.
+        */
+       public XMLReader getTransformedXMLReader();
+}



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

Reply via email to