poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kiwiwi...@apache.org
Subject svn commit: r1857518 [7/7] - in /xmlbeans/branches/xmlbeans-536: ./ src/jam/ src/jam/org/ src/jam/org/apache/ src/jam/org/apache/xmlbeans/ src/jam/org/apache/xmlbeans/impl/ src/jam/org/apache/xmlbeans/impl/jam/ src/jam/org/apache/xmlbeans/impl/jam/anno...
Date Sun, 14 Apr 2019 13:55:07 GMT
Added: xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/visitor/TraversingJVisitor.java
URL: http://svn.apache.org/viewvc/xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/visitor/TraversingJVisitor.java?rev=1857518&view=auto
==============================================================================
--- xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/visitor/TraversingJVisitor.java
(added)
+++ xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/visitor/TraversingJVisitor.java
Sun Apr 14 13:55:06 2019
@@ -0,0 +1,142 @@
+/*   Copyright 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.xmlbeans.impl.jam.visitor;
+
+import org.apache.xmlbeans.impl.jam.JAnnotatedElement;
+import org.apache.xmlbeans.impl.jam.JAnnotation;
+import org.apache.xmlbeans.impl.jam.JClass;
+import org.apache.xmlbeans.impl.jam.JComment;
+import org.apache.xmlbeans.impl.jam.JConstructor;
+import org.apache.xmlbeans.impl.jam.JField;
+import org.apache.xmlbeans.impl.jam.JInvokable;
+import org.apache.xmlbeans.impl.jam.JMethod;
+import org.apache.xmlbeans.impl.jam.JPackage;
+import org.apache.xmlbeans.impl.jam.JParameter;
+
+/**
+ * <p>An adaptor which helps another MVisitor visit a JElement and its
+ * children, recursively.  Note that inherited class or annotations members
+ * are never visited, nor are referenced classes (e.g. referenced via member
+ * types).  The following table lists each element and the child types
+ * which are traversed.</p>
+ *
+ * <table border='1'>
+ * <tr><td><b>Element</b></td><td><b>Sub-elements
traversal</b></td></tr>
+ * <tr><td>Package       </td><td>Classes, Annotations, Comments</td></tr>
+ * <tr><td>Class         </td><td>Fields, Constructors, Methods,
Annotations, Comments</td></tr>
+ * <tr><td>Field         </td><td>Annotations, Comments</td></tr>
+ * <tr><td>Constructor   </td><td>Parameters, Annotations, Comments</td></tr>
+ * <tr><td>Method        </td><td>Parameters, Annotations, Comments</td></tr>
+ * <tr><td>Parameter     </td><td>Annotations, Comments</td></tr>
+ * <tr><td>Annotation    </td><td>[none]</td></tr>
+ * <tr><td>Comment       </td><td>[none]</td></tr>
+ * </table>
+ *
+ * </p>
+ * @author Patrick Calahan &lt;email: pcal-at-bea-dot-com&gt;
+ */
+public class TraversingJVisitor extends JVisitor {
+
+  // ========================================================================
+  // Variables
+
+  private JVisitor mDelegate;
+
+  // ========================================================================
+  // Constructors
+
+  public TraversingJVisitor(JVisitor jv) {
+    if (jv == null) throw new IllegalArgumentException("null jv");
+    mDelegate = jv;
+  }
+
+  // ========================================================================
+  // JVisitor implementation
+
+  public void visit(JPackage pkg) {
+    pkg.accept(mDelegate);
+    JClass[] c = pkg.getClasses();
+    for(int i=0; i<c.length; i++) visit(c[i]);
+    visitAnnotations(pkg);
+    visitComment(pkg);
+  }
+
+  public void visit(JClass clazz) {
+    clazz.accept(mDelegate);
+    {
+      JField[] f = clazz.getDeclaredFields();
+      for(int i=0; i<f.length; i++) visit(f[i]);
+    }{
+      JConstructor[] c = clazz.getConstructors();
+      for(int i=0; i<c.length; i++) visit(c[i]);
+    }{
+      JMethod[] m = clazz.getMethods();
+      for(int i=0; i<m.length; i++) visit(m[i]);
+    }
+    visitAnnotations(clazz);
+    visitComment(clazz);
+  }
+
+  // ========================================================================
+  // JElement implementation
+
+  public void visit(JField field) {
+    field.accept(mDelegate);
+    visitAnnotations(field);
+    visitComment(field);
+  }
+
+  public void visit(JConstructor ctor) {
+    ctor.accept(mDelegate);
+    visitParameters(ctor);
+    visitAnnotations(ctor);
+    visitComment(ctor);
+  }
+
+  public void visit(JMethod method) {
+    method.accept(mDelegate);
+    visitParameters(method);
+    visitAnnotations(method);
+    visitComment(method);
+  }
+
+  public void visit(JParameter param) {
+    param.accept(mDelegate);
+    visitAnnotations(param);
+    visitComment(param);
+  }
+
+  public void visit(JAnnotation ann) { ann.accept(mDelegate); }
+
+  public void visit(JComment comment) { comment.accept(mDelegate); }
+
+  // ========================================================================
+  // Private methods
+
+  private void visitParameters(JInvokable iv) {
+    JParameter[] p = iv.getParameters();
+    for(int i=0; i<p.length; i++) visit(p[i]);
+  }
+
+  private void visitAnnotations(JAnnotatedElement ae) {
+    JAnnotation[] anns = ae.getAnnotations();
+    for(int i=0; i<anns.length; i++) visit(anns[i]);
+  }
+
+  private void visitComment(JAnnotatedElement e) {
+    JComment c = e.getComment();
+    if (c != null) visit(c);
+  }
+}
\ No newline at end of file

Added: xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/visitor/TraversingMVisitor.java
URL: http://svn.apache.org/viewvc/xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/visitor/TraversingMVisitor.java?rev=1857518&view=auto
==============================================================================
--- xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/visitor/TraversingMVisitor.java
(added)
+++ xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/visitor/TraversingMVisitor.java
Sun Apr 14 13:55:06 2019
@@ -0,0 +1,143 @@
+/*   Copyright 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.xmlbeans.impl.jam.visitor;
+
+import org.apache.xmlbeans.impl.jam.mutable.MAnnotatedElement;
+import org.apache.xmlbeans.impl.jam.mutable.MAnnotation;
+import org.apache.xmlbeans.impl.jam.mutable.MClass;
+import org.apache.xmlbeans.impl.jam.mutable.MComment;
+import org.apache.xmlbeans.impl.jam.mutable.MConstructor;
+import org.apache.xmlbeans.impl.jam.mutable.MField;
+import org.apache.xmlbeans.impl.jam.mutable.MInvokable;
+import org.apache.xmlbeans.impl.jam.mutable.MMethod;
+import org.apache.xmlbeans.impl.jam.mutable.MPackage;
+import org.apache.xmlbeans.impl.jam.mutable.MParameter;
+
+/**
+ * <p>An adaptor which helps another MVisitor visit a JElement and its
+ * children, recursively.  Note that inherited class or annotations members
+ * are never visited, nor are referenced classes (e.g. referenced via member
+ * types).  The following table lists each element and the child types
+ * which are traversed.</p>
+
+ *
+ * <table border='1'>
+ * <tr><td><b>Element</b></td><td><b>Sub-elements
traversal</b></td></tr>
+ * <tr><td>Package       </td><td>Classes, Annotations, Comments</td></tr>
+ * <tr><td>Class         </td><td>Fields, Constructors, Methods,
Annotations, Comments</td></tr>
+ * <tr><td>Field         </td><td>Annotations, Comments</td></tr>
+ * <tr><td>Constructor   </td><td>Parameters, Annotations, Comments</td></tr>
+ * <tr><td>Method        </td><td>Parameters, Annotations, Comments</td></tr>
+ * <tr><td>Parameter     </td><td>Annotations, Comments</td></tr>
+ * <tr><td>Annotation    </td><td>[none]</td></tr>
+ * <tr><td>Comment       </td><td>[none]</td></tr>
+ * </table>
+ *
+ * </p>
+ * @author Patrick Calahan &lt;email: pcal-at-bea-dot-com&gt;
+ */
+public class TraversingMVisitor extends MVisitor {
+
+  // ========================================================================
+  // Variables
+
+  private MVisitor mDelegate;
+
+  // ========================================================================
+  // Constructors
+
+  public TraversingMVisitor(MVisitor jv) {
+    if (jv == null) throw new IllegalArgumentException("null jv");
+    mDelegate = jv;
+  }
+
+  // ========================================================================
+  // JVisitor implementation
+
+  public void visit(MPackage pkg) {
+    pkg.accept(mDelegate);
+    MClass[] c = pkg.getMutableClasses();
+    for(int i=0; i<c.length; i++) visit(c[i]);
+    visitAnnotations(pkg);
+    visitComment(pkg);
+  }
+
+  public void visit(MClass clazz) {
+    clazz.accept(mDelegate);
+    {
+      MField[] f = clazz.getMutableFields();
+      for(int i=0; i<f.length; i++) visit(f[i]);
+    }{
+      MConstructor[] c = clazz.getMutableConstructors();
+      for(int i=0; i<c.length; i++) visit(c[i]);
+    }{
+      MMethod[] m = clazz.getMutableMethods();
+      for(int i=0; i<m.length; i++) visit(m[i]);
+    }
+    visitAnnotations(clazz);
+    visitComment(clazz);
+  }
+
+  // ========================================================================
+  // MElement implementation
+
+  public void visit(MField field) {
+    field.accept(mDelegate);
+    visitAnnotations(field);
+    visitComment(field);
+  }
+
+  public void visit(MConstructor ctor) {
+    ctor.accept(mDelegate);
+    visitParameters(ctor);
+    visitAnnotations(ctor);
+    visitComment(ctor);
+  }
+
+  public void visit(MMethod method) {
+    method.accept(mDelegate);
+    visitParameters(method);
+    visitAnnotations(method);
+    visitComment(method);
+  }
+
+  public void visit(MParameter param) {
+    param.accept(mDelegate);
+    visitAnnotations(param);
+    visitComment(param);
+  }
+
+  public void visit(MAnnotation ann) { ann.accept(mDelegate); }
+
+  public void visit(MComment comment) { comment.accept(mDelegate); }
+
+  // ========================================================================
+  // Private methods
+
+  private void visitParameters(MInvokable iv) {
+    MParameter[] p = iv.getMutableParameters();
+    for(int i=0; i<p.length; i++) visit(p[i]);
+  }
+
+  private void visitAnnotations(MAnnotatedElement ae) {
+    MAnnotation[] anns = ae.getMutableAnnotations();
+    for(int i=0; i<anns.length; i++) visit(anns[i]);
+  }
+
+  private void visitComment(MAnnotatedElement e) {
+    MComment c = e.getMutableComment();
+    if (c != null) visit(c);
+  }
+}
\ No newline at end of file

Added: xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlElements.java
URL: http://svn.apache.org/viewvc/xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlElements.java?rev=1857518&view=auto
==============================================================================
--- xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlElements.java
(added)
+++ xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlElements.java
Sun Apr 14 13:55:06 2019
@@ -0,0 +1,49 @@
+/*   Copyright 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.xmlbeans.impl.jam.xml;
+
+/**
+ * @author Patrick Calahan &lt;email: pcal-at-bea-dot-com&gt;
+ */
+public interface JamXmlElements {
+
+  public static final String JAMSERVICE = "jam-service";
+
+  public static final String PACKAGE = "package";
+  public static final String CLASS = "class";
+  public static final String NAME = "name";
+
+  public static final String CLASS_NAME = "name";
+  public static final String PACKAGE_NAME = "package";
+
+  public static final String ISINTERFACE = "is-interface";
+  public static final String INTERFACE = "interface";
+  public static final String SUPERCLASS = "superclass";
+  public static final String MODIFIERS = "modifiers";
+  public static final String PARAMETER = "parameter";
+  public static final String TYPE = "type";
+  public static final String CONSTRUCTOR = "constructor";
+  public static final String METHOD = "method";
+  public static final String FIELD = "field";
+  public static final String RETURNTYPE = "return-type";
+  public static final String COMMENT = "comment";
+  public static final String SOURCEPOSITION = "source-position";
+  public static final String LINE = "line";
+  public static final String COLUMN = "column";
+  public static final String SOURCEURI = "source-uri";
+  public static final String VALUE = "value";
+  public static final String ANNOTATION = "annotation";
+  public static final String ANNOTATIONVALUE = "annotation-value";
+}

Added: xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlReader.java
URL: http://svn.apache.org/viewvc/xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlReader.java?rev=1857518&view=auto
==============================================================================
--- xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlReader.java
(added)
+++ xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlReader.java
Sun Apr 14 13:55:06 2019
@@ -0,0 +1,316 @@
+/*   Copyright 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.xmlbeans.impl.jam.xml;
+
+import org.apache.xmlbeans.impl.jam.JClass;
+import org.apache.xmlbeans.impl.jam.internal.CachedClassBuilder;
+import org.apache.xmlbeans.impl.jam.internal.elements.ClassImpl;
+import org.apache.xmlbeans.impl.jam.internal.elements.ElementContext;
+import org.apache.xmlbeans.impl.jam.mutable.MAnnotatedElement;
+import org.apache.xmlbeans.impl.jam.mutable.MAnnotation;
+import org.apache.xmlbeans.impl.jam.mutable.MClass;
+import org.apache.xmlbeans.impl.jam.mutable.MConstructor;
+import org.apache.xmlbeans.impl.jam.mutable.MField;
+import org.apache.xmlbeans.impl.jam.mutable.MInvokable;
+import org.apache.xmlbeans.impl.jam.mutable.MMethod;
+import org.apache.xmlbeans.impl.jam.mutable.MParameter;
+import org.apache.xmlbeans.impl.jam.mutable.MSourcePosition;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * @author Patrick Calahan &lt;email: pcal-at-bea-dot-com&gt;
+ */
+/*package*/ class JamXmlReader implements JamXmlElements {
+
+  // ========================================================================
+  // Variables
+
+  private XMLStreamReader mIn;
+  private CachedClassBuilder mCache;
+  private ElementContext mContext;
+
+  // ========================================================================
+  // Constructors
+
+  public JamXmlReader(CachedClassBuilder cache, 
+                      InputStream in, 
+                      ElementContext ctx)
+    throws XMLStreamException
+  {
+    this(cache,XMLInputFactory.newInstance().createXMLStreamReader(in),ctx);
+  }
+
+  public JamXmlReader(CachedClassBuilder cache,
+                      Reader in,
+                      ElementContext ctx)
+    throws XMLStreamException
+  {
+    this(cache,XMLInputFactory.newInstance().createXMLStreamReader(in),ctx);
+  }
+
+  public JamXmlReader(CachedClassBuilder cache,
+                      XMLStreamReader in,
+                      ElementContext ctx)
+  {
+    if (cache == null) throw new IllegalArgumentException("null cache");
+    if (in == null) throw new IllegalArgumentException("null cache");
+    if (ctx == null) throw new IllegalArgumentException("null ctx");
+    mIn = in;
+    mCache = cache;
+    mContext = ctx;
+  }
+
+  // ========================================================================
+  // Public methods
+
+  public void read() throws XMLStreamException {
+    nextElement();
+    assertStart(JAMSERVICE);
+    nextElement();
+    while(CLASS.equals(getElementName())) readClass();
+    assertEnd(JAMSERVICE);
+  }
+
+  // ========================================================================
+  // Private methods
+
+  private void readClass() throws XMLStreamException {
+    assertStart(CLASS);
+    nextElement();
+    String clazzName = assertCurrentString(CLASS_NAME);
+    int dot = clazzName.lastIndexOf('.');
+    String pkgName = "";
+    if (dot != -1) {
+      pkgName = clazzName.substring(0,dot);
+      clazzName = clazzName.substring(dot+1);
+    }
+    MClass clazz = mCache.createClassToBuild(pkgName,clazzName,null);
+    //
+    clazz.setIsInterface(assertCurrentBoolean(ISINTERFACE));
+    clazz.setModifiers(assertCurrentInt(MODIFIERS));
+    String supername = checkCurrentString(SUPERCLASS);
+    if (supername != null) clazz.setSuperclass(supername);
+    while((supername = checkCurrentString(INTERFACE)) != null) {
+      clazz.addInterface(supername);
+    }
+    while(FIELD.equals(getElementName())) readField(clazz);
+    while(CONSTRUCTOR.equals(getElementName())) readConstructor(clazz);
+    while(METHOD.equals(getElementName())) readMethod(clazz);
+    readAnnotatedElement(clazz);
+    assertEnd(CLASS);
+    ((ClassImpl)clazz).setState(ClassImpl.LOADED);
+    nextElement();
+  }
+
+  private void readField(MClass clazz) throws XMLStreamException {
+    assertStart(FIELD);
+    MField field = clazz.addNewField();
+    nextElement();
+    field.setSimpleName(assertCurrentString(NAME));
+    field.setModifiers(assertCurrentInt(MODIFIERS));
+    field.setType(assertCurrentString(TYPE));
+    readAnnotatedElement(field);
+    assertEnd(FIELD);
+    nextElement();
+  }
+
+  private void readConstructor(MClass clazz) throws XMLStreamException {
+    assertStart(CONSTRUCTOR);
+    MConstructor ctor = clazz.addNewConstructor();
+    nextElement();
+    readInvokableContents(ctor);
+    assertEnd(CONSTRUCTOR);
+    nextElement();
+  }
+
+  private void readMethod(MClass clazz) throws XMLStreamException {
+    assertStart(METHOD);
+    MMethod method = clazz.addNewMethod();
+    nextElement();
+    method.setSimpleName(assertCurrentString(NAME));
+    method.setReturnType(assertCurrentString(RETURNTYPE));
+    readInvokableContents(method);
+    assertEnd(METHOD);
+    nextElement();
+  }
+
+  private void readSourcePosition(MAnnotatedElement element)
+    throws XMLStreamException
+  {
+    assertStart(SOURCEPOSITION);
+    MSourcePosition pos = element.createSourcePosition();
+    nextElement();
+    if (LINE.equals(getElementName())) {
+      pos.setLine(assertCurrentInt(LINE));
+    }
+    if (COLUMN.equals(getElementName())) {
+      pos.setColumn(assertCurrentInt(COLUMN));
+    }
+    if (SOURCEURI.equals(getElementName())) {
+      try {
+        pos.setSourceURI(new URI(assertCurrentString(SOURCEURI)));
+      } catch(URISyntaxException use) {
+        throw new XMLStreamException(use);
+      }
+    }
+    assertEnd(SOURCEPOSITION);
+    nextElement();
+  }
+
+  private void readInvokableContents(MInvokable out)
+    throws XMLStreamException
+  {
+    out.setModifiers(assertCurrentInt(MODIFIERS));
+    while(PARAMETER.equals(getElementName())) {
+      nextElement();
+      MParameter param = out.addNewParameter();
+      param.setSimpleName(assertCurrentString(NAME));
+      param.setType(assertCurrentString(TYPE));
+      readAnnotatedElement(param);
+      assertEnd(PARAMETER);
+      nextElement();
+    }
+    readAnnotatedElement(out);
+  }
+
+
+  private void readAnnotatedElement(MAnnotatedElement element)
+    throws XMLStreamException
+  {
+    while(ANNOTATION.equals(getElementName())) {
+      nextElement();
+      //REVIEW creating ann for tag is not the right thing to do here.
+      //we may need to store more info about exactly what the annotation was
+      MAnnotation ann = element.addLiteralAnnotation(assertCurrentString(NAME));
+      while(ANNOTATIONVALUE.equals(getElementName())) {
+        nextElement();
+        String name = assertCurrentString(NAME);
+        String type = assertCurrentString(TYPE);
+        JClass jclass = mContext.getClassLoader().loadClass(type);
+        if (jclass.isArrayType()) {
+          Collection list = new ArrayList();
+          while(VALUE.equals(getElementName())) {
+            String value = assertCurrentString(VALUE);
+            list.add(value);
+          }
+          String[] vals = new String[list.size()];
+          list.toArray(vals);
+          ann.setSimpleValue(name,vals,jclass);
+        } else {
+          String value = assertCurrentString(VALUE);
+          ann.setSimpleValue(name,value, jclass);
+        }
+        assertEnd(ANNOTATIONVALUE);
+        nextElement();
+      }
+      assertEnd(ANNOTATION);
+      nextElement();
+    }
+    if (COMMENT.equals(getElementName())) {
+      element.createComment().setText(mIn.getElementText());
+      assertEnd(COMMENT);
+      nextElement();
+    }
+    if (SOURCEPOSITION.equals(getElementName())) {
+      readSourcePosition(element);
+    }
+  }
+
+
+  private void assertStart(String named) throws XMLStreamException {
+    if (!mIn.isStartElement() || !named.equals(getElementName())) {
+      error("expected to get a <"+named+">, ");
+    }
+  }
+
+  private void assertEnd(String named) throws XMLStreamException {
+    if (!mIn.isEndElement() || !named.equals(getElementName())) {
+      error("expected to get a </"+named+">, ");
+    }
+  }
+
+
+
+  private String checkCurrentString(String named) throws XMLStreamException {
+    if (named.equals(getElementName())) {
+      String val = mIn.getElementText();
+      assertEnd(named);
+      nextElement();
+      return val;
+    }
+    return null;
+  }
+
+
+  private String assertCurrentString(String named) throws XMLStreamException {
+    assertStart(named);
+    String val = mIn.getElementText();
+    assertEnd(named);
+    nextElement();
+    return val;
+  }
+
+  private int assertCurrentInt(String named) throws XMLStreamException {
+    assertStart(named);
+    String val = mIn.getElementText();
+    assertEnd(named);
+    nextElement();
+    return Integer.valueOf(val).intValue();
+  }
+
+  private boolean assertCurrentBoolean(String named) throws XMLStreamException {
+    assertStart(named);
+    String val = mIn.getElementText();
+    assertEnd(named);
+    nextElement();
+    return Boolean.valueOf(val).booleanValue();
+  }
+
+
+  private void error(String message) throws XMLStreamException {
+    StringWriter out = new StringWriter();
+    out.write("<");
+    out.write(mIn.getLocalName());
+    out.write("> line:");
+    out.write(""+mIn.getLocation().getLineNumber());
+    out.write(" col:");
+    out.write(""+mIn.getLocation().getColumnNumber());
+    out.write("]");
+    throw new XMLStreamException(message+":\n "+out.toString());
+  }
+
+  private void nextElement() throws XMLStreamException {
+    do {
+      if (mIn.next() == -1) {
+        throw new XMLStreamException("Unexpected end of file");
+      }
+    } while(!mIn.isEndElement() && !mIn.isStartElement());
+  }
+
+  private String getElementName() {
+    return mIn.getLocalName();
+  }
+
+}

Added: xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlUtils.java
URL: http://svn.apache.org/viewvc/xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlUtils.java?rev=1857518&view=auto
==============================================================================
--- xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlUtils.java
(added)
+++ xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlUtils.java
Sun Apr 14 13:55:06 2019
@@ -0,0 +1,85 @@
+/*   Copyright 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.xmlbeans.impl.jam.xml;
+
+import org.apache.xmlbeans.impl.jam.JClass;
+import org.apache.xmlbeans.impl.jam.JamService;
+import org.apache.xmlbeans.impl.jam.JamServiceFactory;
+import org.apache.xmlbeans.impl.jam.JamServiceParams;
+import org.apache.xmlbeans.impl.jam.internal.CachedClassBuilder;
+import org.apache.xmlbeans.impl.jam.internal.JamServiceImpl;
+import org.apache.xmlbeans.impl.jam.internal.elements.ElementContext;
+
+import javax.xml.stream.XMLStreamException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * @author Patrick Calahan &lt;email: pcal-at-bea-dot-com&gt;
+ */
+public class JamXmlUtils {
+
+  // ========================================================================
+  // Singleton
+
+  public static final JamXmlUtils getInstance() { return INSTANCE; }
+
+  private static final JamXmlUtils INSTANCE = new JamXmlUtils();
+
+  private JamXmlUtils() {}
+
+  // ========================================================================
+  // Public methods
+
+  public JamService createService(InputStream in)
+    throws IOException, XMLStreamException
+  {
+    if (in == null) throw new IllegalArgumentException("null stream");
+    JamServiceFactory jsf = JamServiceFactory.getInstance();
+    JamServiceParams params = jsf.createServiceParams();
+    CachedClassBuilder cache = new CachedClassBuilder();
+    // finish initalizing the params and create the service
+    params.addClassBuilder(cache);
+    JamService out = jsf.createService(params);
+    // now go view the xml.  we have to do this afterwards so that the
+    // classloader has been created and is available for linking.
+    JamXmlReader reader = new JamXmlReader(cache,in,(ElementContext)params);
+    reader.read();
+    {
+      // slightly gross hack to get the class names into the service
+      List classNames = Arrays.asList(cache.getClassNames());
+      classNames.addAll(Arrays.asList(out.getClassNames()));
+      String[] nameArray = new String[classNames.size()];
+      classNames.toArray(nameArray);
+      ((JamServiceImpl)out).setClassNames(nameArray);
+    }
+    return out;
+  }
+
+  public void toXml(JClass[] clazzes, Writer writer)
+    throws IOException, XMLStreamException
+  {
+    if (clazzes == null) throw new IllegalArgumentException("null classes");
+    if (writer == null) throw new IllegalArgumentException("null writer");
+    JamXmlWriter out = new JamXmlWriter(writer);
+    out.begin();
+    for(int i=0; i<clazzes.length; i++) out.write(clazzes[i]);
+    out.end();
+  }
+
+}

Added: xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlWriter.java
URL: http://svn.apache.org/viewvc/xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlWriter.java?rev=1857518&view=auto
==============================================================================
--- xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlWriter.java
(added)
+++ xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/JamXmlWriter.java
Sun Apr 14 13:55:06 2019
@@ -0,0 +1,269 @@
+/*   Copyright 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.xmlbeans.impl.jam.xml;
+
+import org.apache.xmlbeans.impl.jam.JAnnotatedElement;
+import org.apache.xmlbeans.impl.jam.JAnnotation;
+import org.apache.xmlbeans.impl.jam.JAnnotationValue;
+import org.apache.xmlbeans.impl.jam.JClass;
+import org.apache.xmlbeans.impl.jam.JComment;
+import org.apache.xmlbeans.impl.jam.JConstructor;
+import org.apache.xmlbeans.impl.jam.JField;
+import org.apache.xmlbeans.impl.jam.JInvokable;
+import org.apache.xmlbeans.impl.jam.JMethod;
+import org.apache.xmlbeans.impl.jam.JParameter;
+import org.apache.xmlbeans.impl.jam.JSourcePosition;
+
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import java.io.Writer;
+
+/**
+ * @author Patrick Calahan &lt;email: pcal-at-bea-dot-com&gt;
+ */
+/*package*/ class JamXmlWriter implements JamXmlElements {
+
+  // ========================================================================
+  // Variables
+
+  private XMLStreamWriter mOut;
+  private boolean mInBody = false;
+  private boolean mWriteSourceURI = false;
+
+  // ========================================================================
+  // Constructors
+
+  public JamXmlWriter(Writer out) throws XMLStreamException  {
+    if (out == null) throw new IllegalArgumentException("null out");
+    mOut = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
+  }
+
+  public JamXmlWriter(XMLStreamWriter out) {
+    if (out == null) throw new IllegalArgumentException("null out");
+    mOut = out;
+  }
+
+  // ========================================================================
+  // Public methods
+
+
+  public void begin() throws XMLStreamException {
+    if (mInBody) throw new XMLStreamException("begin() already called");
+    mOut.writeStartElement(JAMSERVICE);
+    mInBody = true;
+  }
+
+  public void end() throws XMLStreamException {
+    if (!mInBody) throw new XMLStreamException("begin() never called");
+    mOut.writeEndElement();
+    mInBody = false;
+  }
+
+
+  /*
+  public void write(JPackage pkg) throws XMLStreamException {
+    assertStarted();
+    mOut.writeStartElement(PACKAGE);
+    JClass[] c = pkg.getClasses();
+    for(int i=0; i<c.length; i++) write(c[i]);
+    writeAnnotatedElement(pkg);
+    mOut.writeEndElement();
+  }*/
+
+  public void write(JClass clazz) throws XMLStreamException {
+    assertStarted();
+    mOut.writeStartElement(CLASS);
+    writeValueElement(CLASS_NAME,clazz.getFieldDescriptor());
+    writeValueElement(ISINTERFACE,clazz.isInterface());
+    writeModifiers(clazz.getModifiers());
+    JClass sc = clazz.getSuperclass();
+    if (sc != null) writeValueElement(SUPERCLASS,sc.getFieldDescriptor());
+    writeClassList(INTERFACE,clazz.getInterfaces());
+    {
+      JField[] f = clazz.getDeclaredFields();
+      for(int i=0; i<f.length; i++) write(f[i]);
+    }{
+      JConstructor[] c = clazz.getConstructors();
+      for(int i=0; i<c.length; i++) write(c[i]);
+    }{
+      JMethod[] m = clazz.getDeclaredMethods();
+      for(int i=0; i<m.length; i++) write(m[i]);
+    }
+    //FIXME inner classes?
+    writeAnnotatedElement(clazz);
+    mOut.writeEndElement();
+  }
+
+
+  // ========================================================================
+  // Private methods
+
+  private void write(JMethod method) throws XMLStreamException {
+    mOut.writeStartElement(METHOD);
+    writeValueElement(NAME,method.getSimpleName());
+    writeValueElement(RETURNTYPE,
+                      method.getReturnType().getFieldDescriptor());
+    writeInvokable(method);
+    mOut.writeEndElement();
+  }
+
+  private void write(JConstructor ctor) throws XMLStreamException {
+    mOut.writeStartElement(CONSTRUCTOR);
+    writeInvokable(ctor);
+    mOut.writeEndElement();
+  }
+
+  private void write(JField field) throws XMLStreamException {
+    mOut.writeStartElement(FIELD);
+    writeValueElement(NAME,field.getSimpleName());
+    writeModifiers(field.getModifiers());
+    writeValueElement(TYPE,field.getType().getFieldDescriptor());
+    writeAnnotatedElement(field);
+    mOut.writeEndElement();
+  }
+
+  private void writeInvokable(JInvokable ji) throws XMLStreamException {
+    writeModifiers(ji.getModifiers());
+    JParameter[] params = ji.getParameters();
+    for(int i=0; i<params.length; i++) {
+      mOut.writeStartElement(PARAMETER);
+      writeValueElement(NAME,params[i].getSimpleName());
+      writeValueElement(TYPE,params[i].getType().getFieldDescriptor());
+      writeAnnotatedElement(params[i]);
+      mOut.writeEndElement();
+    }
+    writeAnnotatedElement(ji);
+  }
+
+  private void writeClassList(String elementName, JClass[] clazzes)
+    throws XMLStreamException
+  {
+    for(int i=0; i<clazzes.length; i++) {
+      mOut.writeStartElement(elementName);
+      mOut.writeCharacters(clazzes[i].getFieldDescriptor());
+      mOut.writeEndElement();
+    }
+  }
+
+  private void writeModifiers(int mods) throws XMLStreamException {
+    mOut.writeStartElement(MODIFIERS);
+    mOut.writeCharacters(String.valueOf(mods));
+    mOut.writeEndElement();
+  }
+
+  private void writeValueElement(String elementName, boolean b)
+    throws XMLStreamException
+  {
+    mOut.writeStartElement(elementName);
+    mOut.writeCharacters(String.valueOf(b));
+    mOut.writeEndElement();
+  }
+
+  private void writeValueElement(String elementName, int x)
+    throws XMLStreamException
+  {
+    mOut.writeStartElement(elementName);
+    mOut.writeCharacters(String.valueOf(x));
+    mOut.writeEndElement();
+  }
+
+  private void writeValueElement(String elementName, String val)
+    throws XMLStreamException
+  {
+    mOut.writeStartElement(elementName);
+    mOut.writeCharacters(val);
+    mOut.writeEndElement();
+  }
+
+  private void writeValueElement(String elementName, String[] vals)
+    throws XMLStreamException
+  {
+    for(int i=0; i<vals.length; i++) writeValueElement(elementName,vals[i]);
+  }
+
+
+  private void writeAnnotatedElement(JAnnotatedElement ae)
+    throws XMLStreamException
+  {
+    JAnnotation[] anns = ae.getAnnotations();
+    for(int i=0; i<anns.length; i++) {
+      writeAnnotation(anns[i]);
+    }
+    JComment jc = ae.getComment();
+    if (jc != null) {
+      String text = jc.getText();
+      if (text != null) {
+        text = text.trim();
+        if (text.length() > 0) {
+          mOut.writeStartElement(COMMENT);
+          mOut.writeCData(jc.getText());
+          mOut.writeEndElement();
+        }
+      }
+    }
+    JSourcePosition pos = ae.getSourcePosition();
+    if (pos != null) {
+      mOut.writeStartElement(SOURCEPOSITION);
+      if (pos.getLine() != -1) {
+        writeValueElement(LINE,pos.getLine());
+      }
+      if (pos.getColumn() != -1) {
+        writeValueElement(COLUMN,pos.getColumn());
+      }
+      if (mWriteSourceURI && pos.getSourceURI() != null)
+        writeValueElement(SOURCEURI,pos.getSourceURI().toString());
+      mOut.writeEndElement();
+    }
+  }
+
+  private void writeAnnotation(JAnnotation ann) throws XMLStreamException {
+    mOut.writeStartElement(ANNOTATION);
+    writeValueElement(NAME,ann.getQualifiedName());
+    JAnnotationValue[] values = ann.getValues();
+    for(int i=0; i<values.length; i++) {
+      writeAnnotationValue(values[i]);
+    }
+    mOut.writeEndElement();
+  }
+
+  private void writeAnnotationValue(JAnnotationValue val)
+    throws XMLStreamException
+  {
+    mOut.writeStartElement(ANNOTATIONVALUE);
+    writeValueElement(NAME,val.getName());
+    writeValueElement(TYPE,val.getType().getFieldDescriptor());
+    if (val.getType().isArrayType()) {
+      writeValueElement(VALUE,val.asStringArray());
+    } else {
+      writeValueElement(VALUE,val.asString());
+    }
+
+    mOut.writeEndElement();
+    //FIXME what about asAnnotationArray?
+/*    JAnnotation nestedAnn = val.asAnnotation();
+    if (nestedAnn != null) {
+      writeAnnotation(nestedAnn);
+    } else {
+      writeValueElement(VALUE,val.asString());
+    }
+    */
+  }
+
+  private void assertStarted() throws XMLStreamException {
+    if (!mInBody) throw new XMLStreamException("begin() not called");
+  }
+
+}
\ No newline at end of file

Added: xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/TunnelledException.java
URL: http://svn.apache.org/viewvc/xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/TunnelledException.java?rev=1857518&view=auto
==============================================================================
--- xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/TunnelledException.java
(added)
+++ xmlbeans/branches/xmlbeans-536/src/jam/org/apache/xmlbeans/impl/jam/xml/TunnelledException.java
Sun Apr 14 13:55:06 2019
@@ -0,0 +1,32 @@
+/*   Copyright 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.xmlbeans.impl.jam.xml;
+
+import javax.xml.stream.XMLStreamException;
+
+/**
+ * @author Patrick Calahan &lt;email: pcal-at-bea-dot-com&gt;
+ */
+public class TunnelledException extends RuntimeException {
+
+  private XMLStreamException mXSE = null;
+
+  public TunnelledException(XMLStreamException xse) {
+    mXSE = xse;
+  }
+
+  public XMLStreamException getXMLStreamException() { return mXSE; }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message