You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by pc...@apache.org on 2003/12/20 03:03:43 UTC

cvs commit: xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust Annotation.java AnnotationImpl.java JavaOutputStream.java SourceJavaOutputStream.java ValidatingJavaOutputStream.java

pcal        2003/12/19 18:03:43

  Modified:    v2/src/binding/org/apache/xmlbeans/impl/binding/joust
                        JavaOutputStream.java SourceJavaOutputStream.java
                        ValidatingJavaOutputStream.java
  Added:       v2/src/binding/org/apache/xmlbeans/impl/binding/joust
                        Annotation.java AnnotationImpl.java
  Log:
  add writeAnnotation to JavaOutputStream
  
  Revision  Changes    Path
  1.3       +38 -7     xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/JavaOutputStream.java
  
  Index: JavaOutputStream.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/JavaOutputStream.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JavaOutputStream.java	13 Dec 2003 07:56:28 -0000	1.2
  +++ JavaOutputStream.java	20 Dec 2003 02:03:43 -0000	1.3
  @@ -237,22 +237,54 @@
             throws IOException;
   
     /**
  -   * Writes out a source-code comment in the current class.  The comment
  +   * <p>Writes out a source-code comment in the current class.  The comment
      * will usually be interpreted as applying to whatever is written next
      * in the stream, i.e. to write comments about a given class, you should
  -   * first call writeComment and then call writeClass.  The precise
  -   * formatting of the comments will be implementation-dependendent, and
  -   * some implementations may ignore comments altogether.
  +   * first call writeComment and then call writeClass.</p>
  +   *
  +   * <p>Line breaks in the comment will be respected, but it should NOT
  +   * include any formatting tokens such as '/*' or '//' - the implementation
  +   * will properly format the comments based on context.  Also note that
  +   * you should not use writeComment to add javadoc tags; code metadata
  +   * should be added with writeAnnotation.</p>
      *
      * @param comment The text of the comment.
      *
      * @throws IllegalStateException if the current stream state does not allow
  -   *         a field declaration (e.g. if startClass has not been called).
  +   *         writing a comment (e.g. if startFile has not been called).
      *         writeComment should be a valid operation at all other times.
      */
     public void writeComment(String comment) throws IOException;
   
     /**
  +   * <p>Writes out an annotation in the current class.  The annotation will
  +   * apply to whatever is written next in the stream, i.e. to an annotation
  +   * for a method, call wrteAnnotation just before writing the method with
  +   * writeMethod.  The way in which the annotations are
  +   * implementation-dependent, but this typically will result in either
  +   * javadoc tags or JSR175 annotations being produced.</p>
  +   *
  +   * <p>Note that the caller is free to re-use the Annotation parameter object
  +   * after making a call to this method.</p>
  +   *
  +   * @param ann the annotation to write to the stream.
  +   *
  +   * @throws IllegalStateException if the current stream state does not allow
  +   *         writing an annotation (e.g. if startFile has not been called).
  +   *         writeComment should be a valid operation at most other times.
  +   */
  +  public void writeAnnotation(Annotation ann) throws IOException;
  +
  +  /**
  +   * Returns an Annotation object of the given type that can be populated
  +   * with some values (setValue) and then passed to the writeAnnotation
  +   * method.
  +   *
  +   * @return An ExpressionFactory.  Must never return null.
  +   */
  +  public Annotation createAnnotation(String type);
  +
  +  /**
      * Writes out a return statement for the current method that returns
      * the given expression.
      *
  @@ -310,8 +342,6 @@
      */
     public void endFile() throws IOException;
   
  -
  -
     /**
      * Closes the JavaOutputStream.  This should be called exactly once and
      * only when you are completely finished with the stream.  Note that in
  @@ -327,6 +357,7 @@
      * @return An ExpressionFactory.  Must never return null.
      */
     public ExpressionFactory getExpressionFactory();
  +
   
   
   }
  
  
  
  1.4       +83 -25    xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/SourceJavaOutputStream.java
  
  Index: SourceJavaOutputStream.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/SourceJavaOutputStream.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SourceJavaOutputStream.java	13 Dec 2003 07:56:28 -0000	1.3
  +++ SourceJavaOutputStream.java	20 Dec 2003 02:03:43 -0000	1.4
  @@ -58,10 +58,10 @@
   import java.io.IOException;
   import java.io.PrintWriter;
   import java.io.Writer;
  -import java.io.File;
  +import java.io.StringWriter;
   import java.lang.reflect.Modifier;
  +import java.util.Iterator;
   import java.util.StringTokenizer;
  -import java.util.Collection;
   
   /**
    * <p>Implementation of JavaOutputStream which outputs Java source code.</p>
  @@ -131,9 +131,10 @@
     private String mPackageName = null;
     private String mClassOrInterfaceName = null;
     private WriterFactory mWriterFactory;
  +  private StringWriter mCommentBuffer = null;
  +  private PrintWriter mCommentPrinter = null;
     protected boolean mVerbose = false;
   
  -
     // ========================================================================
     // Constructors
   
  @@ -157,7 +158,7 @@
     // JavaOutputStream implementation
   
     public void startFile(String packageName,
  -                           String classOrInterfaceName) throws IOException {
  +                        String classOrInterfaceName) throws IOException {
       if (packageName == null) {
         throw new IllegalArgumentException("null package");
       }
  @@ -180,6 +181,7 @@
                            String[] interfaceNames)
             throws IOException {
       checkStateForWrite();
  +    printCommentsIfNeeded();
       if (mVerbose) verbose("startClass "+mPackageName+"."+mClassOrInterfaceName);
       extendsClassName = makeI18nSafe(extendsClassName);
       mOut.println("package " + mPackageName + ";");
  @@ -210,9 +212,8 @@
             throws IOException {
       if (mVerbose) verbose("startInterface "+mPackageName+"."+mClassOrInterfaceName);
       checkStateForWrite();
  -    mClassOrInterfaceName = makeI18nSafe(mClassOrInterfaceName);
  +    printCommentsIfNeeded();
       mPackageName = makeI18nSafe(mPackageName);
  -    this.mClassOrInterfaceName = null;
       mOut.println("package " + mPackageName + ";");
       // We need to write up the actual class declaration and save it until
       // after the imports have been written
  @@ -237,6 +238,7 @@
                                Expression defaultValue) throws IOException {
       if (mVerbose) verbose("writeField "+typeName+" "+fieldName);
       checkStateForWrite();
  +    printCommentsIfNeeded();
       printIndents();
       typeName = makeI18nSafe(typeName);
       fieldName = makeI18nSafe(fieldName);
  @@ -259,7 +261,7 @@
                                        String[] paramNames,
                                        String[] exceptionClassNames)
             throws IOException {
  -    return startMethod(modifiers, mClassOrInterfaceName, null,
  +    return startMethod(modifiers, null, mClassOrInterfaceName,
                          paramTypeNames, paramNames, exceptionClassNames);
     }
   
  @@ -272,6 +274,7 @@
             throws IOException {
       if (mVerbose) verbose("startMethod "+methodName);
       checkStateForWrite();
  +    printCommentsIfNeeded();
       methodName = makeI18nSafe(methodName);
       returnTypeName = makeI18nSafe(returnTypeName);
       printIndents();
  @@ -312,23 +315,31 @@
   
     public void writeComment(String comment) throws IOException {
       if (mVerbose) verbose("comment");
  -    checkStateForWrite();
  -    printIndents();
  -    mOut.println("/**");
  -    StringTokenizer st = new StringTokenizer(makeI18nSafe(comment),
  -                                             COMMENT_LINE_DELIMITERS);
  -    while (st.hasMoreTokens()) {
  -      printIndents();
  -      mOut.print(" * ");
  -      mOut.println(st.nextToken());
  +    getCommentPrinter().println(comment);
  +  }
  +
  +  public void writeAnnotation(Annotation ann) throws IOException {
  +    //FIXME haven't really thought much about how to write annotations
  +    //as javadoc - this is more just proof-of-concept at this point.
  +    //FIXME Eventually, will also need a switch for writing out jsr175
  +    PrintWriter out = getCommentPrinter();
  +    Iterator i = ((AnnotationImpl)ann).getPropertyNames();
  +    while(i.hasNext()) {
  +      String n = i.next().toString();
  +      out.print('@');
  +      out.print(((AnnotationImpl)ann).getType());
  +      out.print('.');
  +      out.print(n);
  +      out.print(" = ");
  +      out.print(((AnnotationImpl)ann).getValueDeclaration(n));
  +      out.println();
       }
  -    printIndents();
  -    mOut.println(" */");
     }
   
     public void writeReturnStatement(Expression expression) throws IOException {
       if (mVerbose) verbose("return");
       checkStateForWrite();
  +    printCommentsIfNeeded();
       printIndents();
       mOut.print("return ");
       mOut.print(((String) expression.getMemento()));
  @@ -339,6 +350,7 @@
             throws IOException {
       if (mVerbose) verbose("assignment");
       checkStateForWrite();
  +    printCommentsIfNeeded();
       printIndents();
       mOut.print(((String) left.getMemento()));
       mOut.print(" = ");
  @@ -349,6 +361,7 @@
     public void endMethodOrConstructor() throws IOException {
       if (mVerbose) verbose("endMethodOrConstructor");
       checkStateForWrite();
  +    printCommentsIfNeeded();
       reduceIndent();
       printIndents();
       mOut.println('}');
  @@ -358,6 +371,7 @@
     public void endClassOrInterface() throws IOException {
       if (mVerbose) verbose("endClassOrInterface");
       checkStateForWrite();
  +    printCommentsIfNeeded();
       reduceIndent();
       printIndents();
       mOut.println('}');
  @@ -365,6 +379,7 @@
   
     public void endFile() throws IOException {
       checkStateForWrite();
  +    printCommentsIfNeeded();
       if (mVerbose) verbose("endFile");
       closeOut();
     }
  @@ -373,6 +388,10 @@
       return this;
     }
   
  +  public Annotation createAnnotation(String type) {
  +    return new AnnotationImpl(type);
  +  }
  +
     public void close() throws IOException {
       if (mVerbose) verbose("close");
       closeOut();//just to be safe
  @@ -404,11 +423,39 @@
     // ========================================================================
     // Private methods
   
  +  private PrintWriter getCommentPrinter() {
  +    if (mCommentPrinter == null) {
  +      mCommentBuffer = new StringWriter();
  +      mCommentPrinter = new PrintWriter(mCommentBuffer);
  +    }
  +    return mCommentPrinter;
  +  }
  +
  +  private void printCommentsIfNeeded() {
  +    if (mCommentBuffer == null) return;
  +    checkStateForWrite();
  +    String comment = mCommentBuffer.toString();
  +    printIndents();
  +    mOut.println("/**");
  +    StringTokenizer st = new StringTokenizer(makeI18nSafe(comment),
  +                                             COMMENT_LINE_DELIMITERS);
  +    while (st.hasMoreTokens()) {
  +      printIndents();
  +      mOut.print(" * ");
  +      mOut.println(st.nextToken());
  +    }
  +    printIndents();
  +    mOut.println(" */");
  +    mCommentBuffer = null;
  +    mCommentPrinter = null;
  +  }
  +
     private void checkStateForWrite() {
       if (mOut == null) {
         throw new IllegalStateException("Attempt to generate code when no "+
  -                                      "file open.  This is due to broken" +
  -                                      "logic in the calling class");
  +                                      "file open.  This is indicates that "+
  +                                      "there is some broken logic in the " +
  +                                      "calling class");
       }
     }
   
  @@ -425,8 +472,9 @@
       mIndentLevel--;
       if (mIndentLevel < 0) {
         throw new IllegalStateException("Indent level reduced below zero. "+
  -                                      "This is a result of an error in the "+
  -                                      "calling code.");
  +                                      "This is indicates that "+
  +                                      "there is some broken logic in the " +
  +                                      "calling class");
       }
     }
   
  @@ -511,15 +559,25 @@
        JavaOutputStream joust = new ValidatingJavaOutputStream(sjos);
       ExpressionFactory exp = joust.getExpressionFactory();
       joust.startFile("foo.bar.baz","MyClass");
  +    Annotation author = joust.createAnnotation("author");
  +    author.setValue("name","Patrick Calahan");
  +    joust.writeComment("Test class");
  +    joust.writeAnnotation(author);
  +
  +
       joust.startClass(Modifier.PUBLIC | Modifier.FINAL,"MyBaseClass", null);
  -    String[] paramNames = {"count", "fooList"};
       String[] paramTypes = {"int", "List"};
  +    String[] paramNames = {"count", "fooList"};
       String[] exceptions = {"IOException"};
  +    Annotation deprecated = joust.createAnnotation("deprecated");
  +    deprecated.setValue("value",true);
       Variable counter =
               joust.writeField(Modifier.PRIVATE, "int", "counter", exp.createInt(99));
  -    joust.writeComment("This is the constructor\n@foo godzilla\n@bar mothra");
  +    joust.writeComment("This is the constructor comment");
  +    joust.writeComment("And here is another.\n\n  ok?");
  +    joust.writeAnnotation(deprecated);
       Variable[] params = joust.startConstructor
  -            (Modifier.PUBLIC, paramNames, paramTypes, exceptions);
  +            (Modifier.PUBLIC, paramTypes, paramNames, exceptions);
       joust.writeAssignmentStatement(counter, params[0]);
       joust.endMethodOrConstructor();
       joust.endClassOrInterface();
  
  
  
  1.3       +8 -0      xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/ValidatingJavaOutputStream.java
  
  Index: ValidatingJavaOutputStream.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/ValidatingJavaOutputStream.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ValidatingJavaOutputStream.java	13 Dec 2003 07:56:28 -0000	1.2
  +++ ValidatingJavaOutputStream.java	20 Dec 2003 02:03:43 -0000	1.3
  @@ -154,6 +154,14 @@
       mDest.writeComment(comment);
     }
   
  +  public void writeAnnotation(Annotation ann) throws IOException {
  +    mDest.writeAnnotation(ann);
  +  }
  +
  +  public Annotation createAnnotation(String type) {
  +    return mDest.createAnnotation(type);
  +  }
  +
     public void writeReturnStatement(Expression expression) throws IOException {
       mDest.writeReturnStatement(expression);
     }
  
  
  
  1.1                  xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/Annotation.java
  
  Index: Annotation.java
  ===================================================================
  /*
  * The Apache Software License, Version 1.1
  *
  *
  * Copyright (c) 2003 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. The end-user documentation included with the redistribution,
  *    if any, must include the following acknowledgment:
  *       "This product includes software developed by the
  *        Apache Software Foundation (http://www.apache.org/)."
  *    Alternately, this acknowledgment may appear in the software itself,
  *    if and wherever such third-party acknowledgments normally appear.
  *
  * 4. The names "Apache" and "Apache Software Foundation" must
  *    not be used to endorse or promote products derived from this
  *    software without prior written permission. For written
  *    permission, please contact apache@apache.org.
  *
  * 5. Products derived from this software may not be called "Apache
  *    XMLBeans", nor may "Apache" appear in their name, without prior
  *    written permission of the Apache Software Foundation.
  *
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  * ====================================================================
  *
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation and was
  * originally based on software copyright (c) 2003 BEA Systems
  * Inc., <http://www.bea.com/>. For more information on the Apache Software
  * Foundation, please see <http://www.apache.org/>.
  */
  package org.apache.xmlbeans.impl.binding.joust;
  
  /**
   * Interface for building up metadata declarations in the generated source.
   * Typically, an instance of this class will be written out as javadoc tags
   * or JSR175 annotations.  However, not that this interface is indifferent
   * about that question - JavaOutputStream implementation decides how to
   * actually write out an Annotation.
   *
   * @author Patrick Calahan <pc...@bea.com>
   */
  public interface Annotation {
  
    public void setValue(String name, Annotation ann);
  
    public void setValue(String name, boolean value);
  
    public void setValue(String name, String value);
  
    public void setValue(String name, int value);
  
    public void setValue(String name, long value);
  
    public void setValue(String name, char value);
  
  
  
  }
  
  
  
  1.1                  xml-xmlbeans/v2/src/binding/org/apache/xmlbeans/impl/binding/joust/AnnotationImpl.java
  
  Index: AnnotationImpl.java
  ===================================================================
  /*
  * The Apache Software License, Version 1.1
  *
  *
  * Copyright (c) 2003 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. The end-user documentation included with the redistribution,
  *    if any, must include the following acknowledgment:
  *       "This product includes software developed by the
  *        Apache Software Foundation (http://www.apache.org/)."
  *    Alternately, this acknowledgment may appear in the software itself,
  *    if and wherever such third-party acknowledgments normally appear.
  *
  * 4. The names "Apache" and "Apache Software Foundation" must
  *    not be used to endorse or promote products derived from this
  *    software without prior written permission. For written
  *    permission, please contact apache@apache.org.
  *
  * 5. Products derived from this software may not be called "Apache
  *    XMLBeans", nor may "Apache" appear in their name, without prior
  *    written permission of the Apache Software Foundation.
  *
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  * ====================================================================
  *
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation and was
  * originally based on software copyright (c) 2003 BEA Systems
  * Inc., <http://www.bea.com/>. For more information on the Apache Software
  * Foundation, please see <http://www.apache.org/>.
  */
  package org.apache.xmlbeans.impl.binding.joust;
  
  import java.util.Map;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   *
   *
   * @author Patrick Calahan <pc...@bea.com>
   */
  public class AnnotationImpl implements Annotation {
  
    // ========================================================================
    // Variables
  
    private Map mNameToValue = new HashMap();
    private String mType;
    //we want to remember the order they were added in
    private List mKeyList = new ArrayList();
  
    // ========================================================================
    // Constructors
  
    /*package*/ AnnotationImpl(String type) {
      if (type == null) throw new IllegalArgumentException("null type");
      mType = type;
    }
  
    // ========================================================================
    // Package methods
  
    /*package*/ String getType() { return mType; }
  
    /*package*/ Iterator getPropertyNames() {
      return mKeyList.iterator();
    }
  
    /*package*/ Object getValue(String name) {
      return mNameToValue.get(name);
    }
  
    /*package*/ String getValueDeclaration(String name) {
      Object o = getValue(name);
      if (o == null) return null;
      if (o instanceof String) return "\""+o+"\"";
      if (o instanceof Character) return "'"+o+"'";
      return o.toString();
    }
  
    // ========================================================================
    // Annotation implementation
  
    public void setValue(String name, Annotation ann) {
      add(name,ann);
    }
  
    public void setValue(String name, boolean value) {
      add(name,Boolean.valueOf(value));
    }
  
    public void setValue(String name, String value) {
      add(name,value);
    }
  
    public void setValue(String name, byte value) {
      add(name,new Byte(value));
    }
  
    public void setValue(String name, int value) {
      add(name,new Integer(value));
    }
  
    public void setValue(String name, long value) {
      add(name,new Long(value));
    }
  
    public void setValue(String name, char value) {
      add(name,new Character(value));
    }
  
    // ========================================================================
    // Private methods
  
    private void add(String name, Object value) {
      if (name == null) throw new IllegalArgumentException("null name");
      if (value == null) throw new IllegalArgumentException("null value");
      mKeyList.add(name);
      mNameToValue.put(name,value);
    }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org