Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » schema » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans.impl.schema;
   17   
   18   import java.io.Writer;
   19   import java.io.IOException;
   20   import java.nio.charset.CharacterCodingException;
   21   import java.nio.charset.Charset;
   22   import java.nio.charset.CharsetEncoder;
   23   import java.util.ArrayList;
   24   import java.util.Map;
   25   import java.util.HashMap;
   26   import java.util.HashSet;
   27   import java.util.LinkedHashMap;
   28   import java.util.List;
   29   
   30   import javax.xml.namespace.QName;
   31   import org.apache.xmlbeans.impl.common.NameUtil;
   32   import org.apache.xmlbeans.PrePostExtension;
   33   import org.apache.xmlbeans.InterfaceExtension;
   34   import org.apache.xmlbeans.SchemaType;
   35   import org.apache.xmlbeans.SchemaTypeSystem;
   36   import org.apache.xmlbeans.SchemaProperty;
   37   import org.apache.xmlbeans.SchemaStringEnumEntry;
   38   import org.apache.xmlbeans.SystemProperties;
   39   import org.apache.xmlbeans.XmlObject;
   40   import org.apache.xmlbeans.XmlOptions;
   41   import org.apache.xmlbeans.SchemaCodePrinter;
   42   
   43   /**
   44    * Prints the java code for a single schema type
   45    */
   46   public final class SchemaTypeCodePrinter implements SchemaCodePrinter
   47   {
   48       Writer _writer;
   49       int    _indent;
   50       boolean _useJava15;
   51   
   52   
   53       static final String LINE_SEPARATOR =
   54           SystemProperties.getProperty("line.separator") == null
   55               ? "\n"
   56               : SystemProperties.getProperty("line.separator");
   57   
   58       static final String MAX_SPACES = "                                        ";
   59       static final int INDENT_INCREMENT = 4;
   60   
   61       public static final String INDEX_CLASSNAME = "TypeSystemHolder";
   62   
   63       public static void printTypeImpl ( Writer writer, SchemaType sType,
   64                                          XmlOptions opt )
   65           throws IOException
   66       {
   67           getPrinter(opt).printTypeImpl( writer, sType );
   68       }
   69   
   70       public static void printType ( Writer writer, SchemaType sType,
   71                                      XmlOptions opt )
   72           throws IOException
   73       {
   74           getPrinter(opt).printType( writer, sType );
   75       }
   76   
   77       /** @deprecated */
   78       public static void printLoader ( Writer writer, SchemaTypeSystem system,
   79                                        XmlOptions opt )
   80           throws IOException
   81       {
   82           getPrinter(opt).printLoader( writer, system );
   83       }
   84   
   85       private static SchemaCodePrinter getPrinter(XmlOptions opt)
   86       {
   87           Object printer = XmlOptions.safeGet
   88               (opt, XmlOptions.SCHEMA_CODE_PRINTER);
   89           if (printer == null || !(printer instanceof SchemaCodePrinter))
   90           {
   91               printer = new SchemaTypeCodePrinter(opt);
   92           }
   93           return (SchemaCodePrinter) printer;
   94       }
   95   
   96       public SchemaTypeCodePrinter (XmlOptions opt)
   97       {
   98           _indent = 0;
   99   
  100           String genversion = null;
  101   
  102           if (opt != null && XmlOptions.hasOption(opt, XmlOptions.GENERATE_JAVA_VERSION))
  103               genversion = (String)opt.get(XmlOptions.GENERATE_JAVA_VERSION);
  104   
  105           if (genversion == null)
  106               genversion = XmlOptions.GENERATE_JAVA_14;
  107   
  108           _useJava15 = XmlOptions.GENERATE_JAVA_15.equals(genversion);
  109       }
  110   
  111       void indent()
  112       {
  113           _indent += INDENT_INCREMENT;
  114       }
  115   
  116       void outdent()
  117       {
  118           _indent -= INDENT_INCREMENT;
  119       }
  120   
  121       String encodeString ( String s )
  122       {
  123           StringBuffer sb = new StringBuffer();
  124   
  125           sb.append( '"' );
  126   
  127           for ( int i = 0 ; i < s.length() ; i++ )
  128           {
  129               char ch = s.charAt( i );
  130   
  131               if (ch == '"')
  132               {
  133                   sb.append( '\\' );
  134                   sb.append( '\"' );
  135               }
  136               else if (ch == '\\')
  137               {
  138                   sb.append( '\\' );
  139                   sb.append( '\\' );
  140               }
  141               else if (ch == '\r')
  142               {
  143                   sb.append( '\\' );
  144                   sb.append( 'r' );
  145               }
  146               else if (ch == '\n')
  147               {
  148                   sb.append( '\\' );
  149                   sb.append( 'n' );
  150               }
  151               else if (ch == '\t')
  152               {
  153                   sb.append( '\\' );
  154                   sb.append( 't' );
  155               }
  156               else
  157                   sb.append( ch );
  158           }
  159   
  160           sb.append( '"' );
  161   
  162           return sb.toString();
  163       }
  164   
  165       void emit(String s) throws IOException
  166       {
  167           int indent = _indent;
  168           
  169           if (indent > MAX_SPACES.length() / 2)
  170               indent = MAX_SPACES.length() / 4 + indent / 2;
  171           
  172           if (indent > MAX_SPACES.length())
  173               indent = MAX_SPACES.length();
  174           
  175           _writer.write(MAX_SPACES.substring(0, indent));
  176           try
  177           {
  178               _writer.write(s);
  179           }
  180           catch (CharacterCodingException cce)
  181           {
  182               _writer.write(makeSafe(s));
  183           }
  184           _writer.write(LINE_SEPARATOR);
  185           
  186           // System.out.print(MAX_SPACES.substring(0, indent));
  187           // System.out.println(s);
  188       }
  189   
  190       private static String makeSafe(String s)
  191       {
  192           Charset charset = Charset.forName(System.getProperty("file.encoding"));
  193           if (charset == null)
  194               throw new IllegalStateException("Default character set is null!");
  195           CharsetEncoder cEncoder = charset.newEncoder();
  196           StringBuffer result = new StringBuffer();
  197           int i;
  198           for (i = 0; i < s.length(); i++)
  199           {
  200               char c = s.charAt(i);
  201               if (!cEncoder.canEncode(c))
  202                   break;
  203           }
  204           for (; i < s.length(); i++)
  205           {
  206               char c = s.charAt(i);
  207               if (cEncoder.canEncode(c))
  208                   result.append(c);
  209               else
  210               {
  211                   String hexValue = Integer.toHexString((int) c);
  212                   switch (hexValue.length())
  213                   {
  214                   case 1:
  215                       result.append("\\u000").append(hexValue); break;
  216                   case 2:
  217                       result.append("\\u00").append(hexValue); break;
  218                   case 3:
  219                       result.append("\\u0").append(hexValue); break;
  220                   case 4:
  221                       result.append("\\u").append(hexValue); break;
  222                   default:
  223                       throw new IllegalStateException();
  224                   }
  225               }
  226           }
  227           return result.toString();
  228       }
  229   
  230       public void printType(Writer writer, SchemaType sType) throws IOException
  231       {
  232           _writer = writer;
  233           printTopComment(sType);
  234           printPackage(sType, true);
  235           emit("");
  236           printInnerType(sType, sType.getTypeSystem());
  237           _writer.flush();
  238       }
  239   
  240       public void printTypeImpl(Writer writer, SchemaType sType) 
  241           throws IOException
  242       {
  243           _writer = writer;
  244           printTopComment(sType);
  245           printPackage(sType, false);
  246           printInnerTypeImpl(sType, sType.getTypeSystem(), false);
  247       }
  248   
  249       /**
  250        * Since not all schema types have java types, this skips
  251        * over any that don't and gives you the nearest java base type.
  252        */
  253       String findJavaType ( SchemaType sType )
  254       {
  255           while ( sType.getFullJavaName() == null )
  256               sType = sType.getBaseType();
  257           
  258           return sType.getFullJavaName();
  259       }
  260   
  261       static String prettyQName(QName qname)
  262       {
  263           String result = qname.getLocalPart();
  264           if (qname.getNamespaceURI() != null)
  265               result += "(@" + qname.getNamespaceURI() + ")";
  266           return result;
  267       }
  268   
  269       void printInnerTypeJavaDoc(SchemaType sType) throws IOException
  270       {
  271           QName name = sType.getName();
  272           if (name == null)
  273           {
  274               if (sType.isDocumentType())
  275                   name = sType.getDocumentElementName();
  276               else if (sType.isAttributeType())
  277                   name = sType.getAttributeTypeAttributeName();
  278               else if (sType.getContainerField() != null)
  279                   name = sType.getContainerField().getName();
  280           }
  281   
  282           emit("/**");
  283           if (sType.isDocumentType())
  284               emit(" * A document containing one " + prettyQName(name) + " element.");
  285           else if (sType.isAttributeType())
  286               emit(" * A document containing one " + prettyQName(name) + " attribute.");
  287           else if (name != null)
  288               emit(" * An XML " + prettyQName(name) + ".");
  289           else
  290               emit(" * An anonymous inner XML type.");
  291           emit(" *");
  292           switch (sType.getSimpleVariety())
  293           {
  294               case SchemaType.NOT_SIMPLE:
  295                   emit(" * This is a complex type.");
  296                   break;
  297               case SchemaType.ATOMIC:
  298                   emit(" * This is an atomic type that is a restriction of " + getFullJavaName(sType) + ".");
  299                   break;
  300               case SchemaType.LIST:
  301                   emit(" * This is a list type whose items are " + sType.getListItemType().getFullJavaName() + ".");
  302                   break;
  303               case SchemaType.UNION:
  304                   emit(" * This is a union type. Instances are of one of the following types:");
  305                   SchemaType[] members = sType.getUnionConstituentTypes();
  306                   for (int i = 0; i < members.length; i++)
  307                       emit(" *     " + members[i].getFullJavaName());
  308                   break;
  309           }
  310           emit(" */");
  311       }
  312   
  313       private String getFullJavaName(SchemaType sType)
  314       {
  315   
  316           SchemaTypeImpl sTypeI = (SchemaTypeImpl) sType;
  317           String ret = sTypeI.getFullJavaName();
  318   
  319           while (sTypeI.isRedefinition())
  320           {
  321               ret = sTypeI.getFullJavaName();
  322               sTypeI = (SchemaTypeImpl) sTypeI.getBaseType();
  323           }
  324           return ret;
  325       }
  326       
  327       private String getUserTypeStaticHandlerMethod(boolean encode, SchemaTypeImpl stype)
  328       {
  329           String unqualifiedName = stype.getName().getLocalPart();
  330           if (unqualifiedName.length() < 2)
  331               unqualifiedName = unqualifiedName.toUpperCase();
  332           else
  333               unqualifiedName = unqualifiedName.substring(0, 1).toUpperCase() + unqualifiedName.substring(1);
  334           
  335           if (encode)
  336               return stype.getUserTypeHandlerName() + ".encode" + unqualifiedName;
  337           else
  338               return stype.getUserTypeHandlerName() + ".decode" + unqualifiedName;
  339       }
  340   
  341   
  342       public static String indexClassForSystem(SchemaTypeSystem system)
  343       {
  344           String name = system.getName();
  345           return name + "." + INDEX_CLASSNAME;
  346       }
  347   
  348       static String shortIndexClassForSystem(SchemaTypeSystem system)
  349       {
  350           return INDEX_CLASSNAME;
  351       }
  352   
  353       void printStaticTypeDeclaration(SchemaType sType, SchemaTypeSystem system) throws IOException
  354       {
  355           String interfaceShortName = sType.getShortJavaName();
  356           emit("public static final org.apache.xmlbeans.SchemaType type = (org.apache.xmlbeans.SchemaType)");
  357           indent();
  358           emit("org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(" +
  359                interfaceShortName + ".class.getClassLoader(), \"" + system.getName() + "\")" +
  360                ".resolveHandle(\"" +
  361                ((SchemaTypeSystemImpl)system).handleForType(sType) + "\");");
  362           outdent();
  363       }
  364   
  365       /** @deprecated */
  366       public void printLoader(Writer writer, SchemaTypeSystem system)
  367           throws IOException
  368       {
  369           // deprecated
  370       }
  371   
  372       void printInnerType(SchemaType sType, SchemaTypeSystem system) throws IOException
  373       {
  374           emit("");
  375   
  376           printInnerTypeJavaDoc(sType);
  377   
  378           startInterface(sType);
  379   
  380           printStaticTypeDeclaration(sType, system);
  381   
  382           if (sType.isSimpleType())
  383           {
  384               if (sType.hasStringEnumValues())
  385                   printStringEnumeration(sType);
  386           }
  387           else
  388           {
  389               if (sType.getContentType() == SchemaType.SIMPLE_CONTENT && sType.hasStringEnumValues())
  390                   printStringEnumeration(sType);
  391   
  392               SchemaProperty[] props = getDerivedProperties(sType);
  393   
  394               for (int i = 0; i < props.length; i++)
  395               {
  396                   SchemaProperty prop = props[i];
  397   
  398                   printPropertyGetters(
  399                       prop.getName(),
  400                       prop.isAttribute(),
  401                       prop.getJavaPropertyName(),
  402                       prop.getJavaTypeCode(),
  403                       javaTypeForProperty(prop),
  404                       xmlTypeForProperty(prop),
  405                       prop.hasNillable() != SchemaProperty.NEVER,
  406                       prop.extendsJavaOption(),
  407                       prop.extendsJavaArray(),
  408                       prop.extendsJavaSingleton()
  409                   );
  410   
  411                   if (!prop.isReadOnly())
  412                   {
  413                       printPropertySetters(
  414                           prop.getName(),
  415                           prop.isAttribute(),
  416                           prop.getJavaPropertyName(),
  417                           prop.getJavaTypeCode(),
  418                           javaTypeForProperty(prop),
  419                           xmlTypeForProperty(prop),
  420                           prop.hasNillable() != SchemaProperty.NEVER,
  421                           prop.extendsJavaOption(),
  422                           prop.extendsJavaArray(),
  423                           prop.extendsJavaSingleton()
  424                       );
  425                   }
  426               }
  427   
  428           }
  429   
  430           printNestedInnerTypes(sType, system);
  431   
  432           printFactory(sType);
  433   
  434           endBlock();
  435       }
  436   
  437       void printFactory(SchemaType sType) throws IOException
  438       {
  439           // Only need full factories for top-level types
  440           boolean fullFactory = true;
  441           if (sType.isAnonymousType() && ! sType.isDocumentType() && !sType.isAttributeType())
  442               fullFactory = false;
  443   
  444           String fullName = sType.getFullJavaName().replace('$', '.');
  445   
  446           emit("");
  447           emit("/**");
  448           emit(" * A factory class with static methods for creating instances");
  449           emit(" * of this type.");
  450           emit(" */");
  451           emit("");
  452           // BUGBUG - Can I use the name loader here?  could it be a
  453           // nested type name?  It is lower case!
  454           emit("public static final class Factory");
  455           emit("{");
  456           indent();
  457   
  458           if (sType.isSimpleType())
  459           {
  460               emit("public static " + fullName + " newValue(java.lang.Object obj) {");
  461               emit("  return (" + fullName + ") type.newValue( obj ); }");
  462               emit("");
  463           }
  464   
  465           // Only need newInstance() for non-abstract types
  466           if (sType.isAbstract()) {
  467               emit("/** @deprecated No need to be able to create instances of abstract types */");
  468               if (_useJava15)
  469                   emit("@Deprecated");
  470           }
  471           emit("public static " + fullName + " newInstance() {");
  472           emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().newInstance( type, null ); }");
  473           emit("");
  474   
  475           // Only need newInstance() for non-abstract types
  476           if (sType.isAbstract()) {
  477               emit("/** @deprecated No need to be able to create instances of abstract types */");
  478               if (_useJava15)
  479                   emit("@Deprecated");
  480           }
  481           emit("public static " + fullName + " newInstance(org.apache.xmlbeans.XmlOptions options) {");
  482           emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().newInstance( type, options ); }");
  483           emit("");
  484   
  485           if (fullFactory)
  486           {
  487               emit("/** @param xmlAsString the string value to parse */");
  488               emit("public static " + fullName + " parse(java.lang.String xmlAsString) throws org.apache.xmlbeans.XmlException {");
  489               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( xmlAsString, type, null ); }");
  490               emit("");
  491   
  492               emit("public static " + fullName + " parse(java.lang.String xmlAsString, org.apache.xmlbeans.XmlOptions options) throws org.apache.xmlbeans.XmlException {");
  493               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( xmlAsString, type, options ); }");
  494               emit("");
  495   
  496               emit("/** @param file the file from which to load an xml document */");
  497               emit("public static " + fullName + " parse(java.io.File file) throws org.apache.xmlbeans.XmlException, java.io.IOException {");
  498               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( file, type, null ); }");
  499               emit("");
  500   
  501               emit("public static " + fullName + " parse(java.io.File file, org.apache.xmlbeans.XmlOptions options) throws org.apache.xmlbeans.XmlException, java.io.IOException {");
  502               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( file, type, options ); }");
  503               emit("");
  504   
  505               emit("public static " + fullName + " parse(java.net.URL u) throws org.apache.xmlbeans.XmlException, java.io.IOException {");
  506               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( u, type, null ); }");
  507               emit("");
  508   
  509               emit("public static " + fullName + " parse(java.net.URL u, org.apache.xmlbeans.XmlOptions options) throws org.apache.xmlbeans.XmlException, java.io.IOException {");
  510               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( u, type, options ); }");
  511               emit("");
  512   
  513               emit("public static " + fullName + " parse(java.io.InputStream is) throws org.apache.xmlbeans.XmlException, java.io.IOException {");
  514               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( is, type, null ); }");
  515               emit("");
  516   
  517               emit("public static " + fullName + " parse(java.io.InputStream is, org.apache.xmlbeans.XmlOptions options) throws org.apache.xmlbeans.XmlException, java.io.IOException {");
  518               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( is, type, options ); }");
  519               emit("");
  520   
  521               emit("public static " + fullName + " parse(java.io.Reader r) throws org.apache.xmlbeans.XmlException, java.io.IOException {");
  522               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( r, type, null ); }");
  523               emit("");
  524   
  525               emit("public static " + fullName + " parse(java.io.Reader r, org.apache.xmlbeans.XmlOptions options) throws org.apache.xmlbeans.XmlException, java.io.IOException {");
  526               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( r, type, options ); }");
  527               emit("");
  528   
  529               emit("public static " + fullName + " parse(javax.xml.stream.XMLStreamReader sr) throws org.apache.xmlbeans.XmlException {");
  530               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( sr, type, null ); }");
  531               emit("");
  532   
  533               emit("public static " + fullName + " parse(javax.xml.stream.XMLStreamReader sr, org.apache.xmlbeans.XmlOptions options) throws org.apache.xmlbeans.XmlException {");
  534               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( sr, type, options ); }");
  535               emit("");
  536   
  537               emit("public static " + fullName + " parse(org.w3c.dom.Node node) throws org.apache.xmlbeans.XmlException {");
  538               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( node, type, null ); }");
  539               emit("");
  540   
  541               emit("public static " + fullName + " parse(org.w3c.dom.Node node, org.apache.xmlbeans.XmlOptions options) throws org.apache.xmlbeans.XmlException {");
  542               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( node, type, options ); }");
  543               emit("");
  544   
  545               emit("/** @deprecated {@link org.apache.xmlbeans.xml.stream.XMLInputStream} */");
  546               if (_useJava15)
  547                   emit("@Deprecated");
  548               emit("public static " + fullName + " parse(org.apache.xmlbeans.xml.stream.XMLInputStream xis) throws org.apache.xmlbeans.XmlException, org.apache.xmlbeans.xml.stream.XMLStreamException {");
  549               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( xis, type, null ); }");
  550               emit("");
  551   
  552               emit("/** @deprecated {@link org.apache.xmlbeans.xml.stream.XMLInputStream} */");
  553               if (_useJava15)
  554                   emit("@Deprecated");
  555               emit("public static " + fullName + " parse(org.apache.xmlbeans.xml.stream.XMLInputStream xis, org.apache.xmlbeans.XmlOptions options) throws org.apache.xmlbeans.XmlException, org.apache.xmlbeans.xml.stream.XMLStreamException {");
  556               emit("  return (" + fullName + ") org.apache.xmlbeans.XmlBeans.getContextTypeLoader().parse( xis, type, options ); }");
  557               emit("");
  558   
  559               // Don't have XMLInputStream anymore
  560               emit("/** @deprecated {@link org.apache.xmlbeans.xml.stream.XMLInputStream} */");
  561               if (_useJava15)
  562                   emit("@Deprecated");
  563               emit("public static org.apache.xmlbeans.xml.stream.XMLInputStream newValidatingXMLInputStream(org.apache.xmlbeans.xml.stream.XMLInputStream xis) throws org.apache.xmlbeans.XmlException, org.apache.xmlbeans.xml.stream.XMLStreamException {");
  564               emit("  return org.apache.xmlbeans.XmlBeans.getContextTypeLoader().newValidatingXMLInputStream( xis, type, null ); }");
  565               emit("");
  566   
  567               // Don't have XMLInputStream anymore
  568               emit("/** @deprecated {@link org.apache.xmlbeans.xml.stream.XMLInputStream} */");
  569               if (_useJava15)
  570                   emit("@Deprecated");
  571               emit("public static org.apache.xmlbeans.xml.stream.XMLInputStream newValidatingXMLInputStream(org.apache.xmlbeans.xml.stream.XMLInputStream xis, org.apache.xmlbeans.XmlOptions options) throws org.apache.xmlbeans.XmlException, org.apache.xmlbeans.xml.stream.XMLStreamException {");
  572               emit("  return org.apache.xmlbeans.XmlBeans.getContextTypeLoader().newValidatingXMLInputStream( xis, type, options ); }");
  573               emit("");
  574           }
  575   
  576           emit("private Factory() { } // No instance of this class allowed");
  577           outdent();
  578           emit("}");
  579       }
  580   
  581       void printNestedInnerTypes(SchemaType sType, SchemaTypeSystem system) throws IOException
  582       {
  583           boolean redefinition = sType.getName() != null &&
  584               sType.getName().equals(sType.getBaseType().getName());
  585           while (sType != null)
  586           {
  587               SchemaType[] anonTypes = sType.getAnonymousTypes();
  588               for (int i = 0; i < anonTypes.length; i++)
  589               {
  590                   if (anonTypes[i].isSkippedAnonymousType())
  591                       printNestedInnerTypes(anonTypes[i], system);
  592                   else
  593                       printInnerType(anonTypes[i], system);
  594               }
  595               // For redefinition other than by extension for complex types, go ahead and print
  596               // the anonymous types in the base
  597               if (!redefinition ||
  598                   (sType.getDerivationType() != SchemaType.DT_EXTENSION && !sType.isSimpleType()))
  599                   break;
  600               sType = sType.getBaseType();
  601           }
  602       }
  603   
  604       void printTopComment(SchemaType sType) throws IOException
  605       {
  606           emit("/*");
  607           if (sType.getName() != null)
  608           {
  609               emit(" * XML Type:  " + sType.getName().getLocalPart());
  610               emit(" * Namespace: " + sType.getName().getNamespaceURI());
  611           }
  612           else
  613           {
  614               QName thename = null;
  615   
  616               if (sType.isDocumentType())
  617               {
  618                   thename = sType.getDocumentElementName();
  619                   emit(" * An XML document type.");
  620               }
  621               else if (sType.isAttributeType())
  622               {
  623                   thename = sType.getAttributeTypeAttributeName();
  624                   emit(" * An XML attribute type.");
  625               }
  626               else
  627                   assert false;
  628   
  629               assert( thename != null );
  630               
  631               emit(" * Localname: " + thename.getLocalPart());
  632               emit(" * Namespace: " + thename.getNamespaceURI());
  633           }
  634           emit(" * Java type: " + sType.getFullJavaName());
  635           emit(" *");
  636           emit(" * Automatically generated - do not modify.");
  637           emit(" */");
  638       }
  639   
  640       void printPackage(SchemaType sType, boolean intf) throws IOException
  641       {
  642           String fqjn;
  643           if (intf)
  644               fqjn = sType.getFullJavaName();
  645           else
  646               fqjn = sType.getFullJavaImplName();
  647   
  648           int lastdot = fqjn.lastIndexOf('.');
  649           if (lastdot < 0)
  650               return;
  651           String pkg = fqjn.substring(0, lastdot);
  652           emit("package " + pkg + ";");
  653       }
  654   
  655       void startInterface(SchemaType sType) throws IOException
  656       {
  657           String shortName = sType.getShortJavaName();
  658           
  659           String baseInterface = findJavaType(sType.getBaseType());
  660   
  661           /*
  662           StringBuffer specializedInterfaces = new StringBuffer();
  663   
  664           if (sType.getSimpleVariety() == SchemaType.ATOMIC &&
  665               sType.getPrimitiveType().getBuiltinTypeCode() == SchemaType.BTC_DECIMAL)
  666           {
  667               int bits = sType.getDecimalSize();
  668               if (bits == SchemaType.SIZE_BIG_INTEGER)
  669                   specializedInterfaces.append(", org.apache.xmlbeans.BigIntegerValue");
  670               if (bits == SchemaType.SIZE_LONG)
  671                   specializedInterfaces.append(", org.apache.xmlbeans.LongValue");
  672               if (bits <= SchemaType.SIZE_INT)
  673                   specializedInterfaces.append(", org.apache.xmlbeans.IntValue");
  674           }
  675           if (sType.getSimpleVariety() == SchemaType.LIST)
  676               specializedInterfaces.append(", org.apache.xmlbeans.ListValue");
  677   
  678           if (sType.getSimpleVariety() == SchemaType.UNION)
  679           {
  680               SchemaType ctype = sType.getUnionCommonBaseType();
  681               String javaTypeHolder = javaTypeHolderForType(ctype);
  682               if (javaTypeHolder != null)
  683                   specializedInterfaces.append(", " + javaTypeHolder);
  684           }
  685           */
  686   
  687           emit("public interface " + shortName + " extends " + baseInterface + getExtensionInterfaces(sType));
  688           emit("{");
  689           indent();
  690           emitSpecializedAccessors(sType);
  691       }
  692   
  693       private static String getExtensionInterfaces(SchemaType sType)
  694       {
  695           SchemaTypeImpl sImpl = getImpl(sType);
  696           if (sImpl == null)
  697               return "";
  698   
  699           StringBuffer sb = new StringBuffer();
  700   
  701           InterfaceExtension[] exts = sImpl.getInterfaceExtensions();
  702           if (exts != null) for (int i = 0; i < exts.length; i++)
  703               sb.append(", " + exts[i].getInterface());
  704   
  705           return sb.toString();
  706       }
  707   
  708       private static SchemaTypeImpl getImpl(SchemaType sType)
  709       {
  710           if (sType instanceof SchemaTypeImpl)
  711               return (SchemaTypeImpl) sType;
  712           else
  713               return null;
  714       }
  715   
  716       private void emitSpecializedAccessors(SchemaType sType) throws IOException
  717       {
  718           if (sType.getSimpleVariety() == SchemaType.ATOMIC &&
  719               sType.getPrimitiveType().getBuiltinTypeCode() == SchemaType.BTC_DECIMAL)
  720           {
  721               int bits = sType.getDecimalSize();
  722               int parentBits = sType.getBaseType().getDecimalSize();
  723               if (bits != parentBits || sType.getBaseType().getFullJavaName() == null)
  724               {
  725                   if (bits == SchemaType.SIZE_BIG_INTEGER)
  726                   {
  727                       emit("java.math.BigInteger getBigIntegerValue();");
  728                       emit("void setBigIntegerValue(java.math.BigInteger bi);");
  729                       emit("/** @deprecated */");
  730                       if (_useJava15)
  731                           emit("@Deprecated");
  732                       emit("java.math.BigInteger bigIntegerValue();");
  733                       emit("/** @deprecated */");
  734                       if (_useJava15)
  735                           emit("@Deprecated");
  736                       emit("void set(java.math.BigInteger bi);");
  737                   }
  738                   else if (bits == SchemaType.SIZE_LONG)
  739                   {
  740                       emit("long getLongValue();");
  741                       emit("void setLongValue(long l);");
  742                       emit("/** @deprecated */");
  743                       if (_useJava15)
  744                           emit("@Deprecated");
  745                       emit("long longValue();");
  746                       emit("/** @deprecated */");
  747                       if (_useJava15)
  748                           emit("@Deprecated");
  749                       emit("void set(long l);");
  750                   }
  751                   else if (bits == SchemaType.SIZE_INT)
  752                   {
  753                       emit("int getIntValue();");
  754                       emit("void setIntValue(int i);");
  755                       emit("/** @deprecated */");
  756                       if (_useJava15)
  757                           emit("@Deprecated");
  758                       emit("int intValue();");
  759                       emit("/** @deprecated */");
  760                       if (_useJava15)
  761                           emit("@Deprecated");
  762                       emit("void set(int i);");
  763                   }
  764                   else if (bits == SchemaType.SIZE_SHORT)
  765                   {
  766                       emit("short getShortValue();");
  767                       emit("void setShortValue(short s);");
  768                       emit("/** @deprecated */");
  769                       if (_useJava15)
  770                           emit("@Deprecated");
  771                       emit("short shortValue();");
  772                       emit("/** @deprecated */");
  773                       if (_useJava15)
  774                           emit("@Deprecated");
  775                       emit("void set(short s);");
  776                   }
  777                   else if (bits == SchemaType.SIZE_BYTE)
  778                   {
  779                       emit("byte getByteValue();");
  780                       emit("void setByteValue(byte b);");
  781                       emit("/** @deprecated */");
  782                       if (_useJava15)
  783                           emit("@Deprecated");
  784                       emit("byte byteValue();");
  785                       emit("/** @deprecated */");
  786                       if (_useJava15)
  787                           emit("@Deprecated");
  788                       emit("void set(byte b);");
  789                   }
  790               }
  791           }
  792   
  793           if (sType.getSimpleVariety() == SchemaType.UNION)
  794           {
  795               emit("java.lang.Object getObjectValue();");
  796               emit("void setObjectValue(java.lang.Object val);");
  797               emit("/** @deprecated */");
  798               if (_useJava15)
  799                   emit("@Deprecated");
  800               emit("java.lang.Object objectValue();");
  801               emit("/** @deprecated */");
  802               if (_useJava15)
  803                   emit("@Deprecated");
  804               emit("void objectSet(java.lang.Object val);");
  805               emit("org.apache.xmlbeans.SchemaType instanceType();");
  806               SchemaType ctype = sType.getUnionCommonBaseType();
  807               if (ctype != null && ctype.getSimpleVariety() != SchemaType.UNION);
  808                   emitSpecializedAccessors(ctype);
  809           }
  810   
  811           if (sType.getSimpleVariety() == SchemaType.LIST)
  812           {
  813               emit("java.util.List getListValue();");
  814               emit("java.util.List xgetListValue();");
  815               emit("void setListValue(java.util.List list);");
  816               emit("/** @deprecated */");
  817               if (_useJava15)
  818                   emit("@Deprecated");
  819               emit("java.util.List listValue();");
  820               emit("/** @deprecated */");
  821               if (_useJava15)
  822                   emit("@Deprecated");
  823               emit("java.util.List xlistValue();");
  824               emit("/** @deprecated */");
  825               if (_useJava15)
  826                   emit("@Deprecated");
  827               emit("void set(java.util.List list);");
  828           }
  829       }
  830   
  831       void startBlock() throws IOException
  832       {
  833           emit("{");
  834           indent();
  835       }
  836       void endBlock() throws IOException
  837       {
  838           outdent();
  839           emit("}");
  840       }
  841   
  842       void printJavaDoc(String sentence) throws IOException
  843       {
  844           emit("");
  845           emit("/**");
  846           emit(" * " + sentence);
  847           emit(" */");
  848       }
  849   
  850       void printShortJavaDoc(String sentence) throws IOException
  851       {
  852           emit("/** " + sentence + " */");
  853       }
  854   
  855       public static String javaStringEscape(String str)
  856       {
  857           // forbidden: \n, \r, \", \\.
  858           test: {
  859               for (int i = 0; i < str.length(); i++)
  860               {
  861                   switch (str.charAt(i))
  862                   {
  863                       case '\n':
  864                       case '\r':
  865                       case '\"':
  866                       case '\\':
  867                           break test;
  868                   }
  869               }
  870               return str;
  871           }
  872           StringBuffer sb = new StringBuffer();
  873           for (int i = 0; i < str.length(); i++)
  874           {
  875               char ch = str.charAt(i);
  876               switch (ch)
  877               {
  878                   default:
  879                       sb.append(ch);
  880                       break;
  881                   case '\n':
  882                       sb.append("\\n");
  883                       break;
  884                   case '\r':
  885                       sb.append("\\r");
  886                       break;
  887                   case '\"':
  888                       sb.append("\\\"");
  889                       break;
  890                   case '\\':
  891                       sb.append("\\\\");
  892                       break;
  893               }
  894           }
  895           return sb.toString();
  896       }
  897   
  898       void printStringEnumeration(SchemaType sType) throws IOException
  899       {
  900           SchemaType baseEnumType = sType.getBaseEnumType();
  901           String baseEnumClass = baseEnumType.getFullJavaName();
  902           boolean hasBase = hasBase(sType);
  903   
  904           if (!hasBase)
  905           {
  906               emit("");
  907               emit("org.apache.xmlbeans.StringEnumAbstractBase enumValue();");
  908               emit("void set(org.apache.xmlbeans.StringEnumAbstractBase e);");
  909           }
  910   
  911           emit("");
  912           SchemaStringEnumEntry[] entries = sType.getStringEnumEntries();
  913           HashSet seenValues = new HashSet();
  914           HashSet repeatValues = new HashSet();
  915           for (int i = 0; i < entries.length; i++)
  916           {
  917               String enumValue = entries[i].getString();
  918               if (seenValues.contains(enumValue))
  919               {
  920                   repeatValues.add(enumValue);
  921                   continue;
  922               }
  923               else
  924                   seenValues.add(enumValue);
  925               String constName = entries[i].getEnumName();
  926               if (hasBase)
  927                   emit("static final " + baseEnumClass + ".Enum " + constName + " = " + baseEnumClass + "." + constName + ";");
  928               else
  929                   emit("static final Enum " + constName + " = Enum.forString(\"" + javaStringEscape(enumValue) + "\");");
  930           }
  931           emit("");
  932           for (int i = 0; i < entries.length; i++)
  933           {
  934               if (repeatValues.contains(entries[i].getString()))
  935                   continue;
  936               String constName = "INT_" + entries[i].getEnumName();
  937               if (hasBase)
  938                   emit("static final int " + constName + " = " + baseEnumClass + "." + constName + ";");
  939               else
  940                   emit("static final int " + constName + " = Enum." + constName + ";");
  941           }
  942           if (!hasBase)
  943           {
  944               emit("");
  945               emit("/**");
  946               emit(" * Enumeration value class for " + baseEnumClass + ".");
  947               emit(" * These enum values can be used as follows:");
  948               emit(" * <pre>");
  949               emit(" * enum.toString(); // returns the string value of the enum");
  950               emit(" * enum.intValue(); // returns an int value, useful for switches");
  951               if (entries.length > 0)
  952               emit(" * // e.g., case Enum.INT_" + entries[0].getEnumName());
  953               emit(" * Enum.forString(s); // returns the enum value for a string");
  954               emit(" * Enum.forInt(i); // returns the enum value for an int");
  955               emit(" * </pre>");
  956               emit(" * Enumeration objects are immutable singleton objects that");
  957               emit(" * can be compared using == object equality. They have no");
  958               emit(" * public constructor. See the constants defined within this");
  959               emit(" * class for all the valid values.");
  960               emit(" */");
  961               emit("static final class Enum extends org.apache.xmlbeans.StringEnumAbstractBase");
  962               emit("{");
  963               indent();
  964               emit("/**");
  965               emit(" * Returns the enum value for a string, or null if none.");
  966               emit(" */");
  967               emit("public static Enum forString(java.lang.String s)");
  968               emit("    { return (Enum)table.forString(s); }");
  969               emit("/**");
  970               emit(" * Returns the enum value corresponding to an int, or null if none.");
  971               emit(" */");
  972               emit("public static Enum forInt(int i)");
  973               emit("    { return (Enum)table.forInt(i); }");
  974               emit("");
  975               emit("private Enum(java.lang.String s, int i)");
  976               emit("    { super(s, i); }");
  977               emit("");
  978               for (int i = 0; i < entries.length; i++)
  979               {
  980                   String constName = "INT_" + entries[i].getEnumName();
  981                   int intValue = entries[i].getIntValue();
  982                   emit("static final int " + constName + " = " + intValue + ";");
  983               }
  984               emit("");
  985               emit("public static final org.apache.xmlbeans.StringEnumAbstractBase.Table table =");
  986               emit("    new org.apache.xmlbeans.StringEnumAbstractBase.Table");
  987               emit("(");
  988               indent();
  989               emit("new Enum[]");
  990               emit("{");
  991               indent();
  992               for (int i = 0; i < entries.length; i++)
  993               {
  994                   String enumValue = entries[i].getString();
  995                   String constName = "INT_" + entries[i].getEnumName();
  996                   emit("new Enum(\"" + javaStringEscape(enumValue) + "\", " + constName + "),");
  997               }
  998               outdent();
  999               emit("}");
 1000               outdent();
 1001               emit(");");
 1002               emit("private static final long serialVersionUID = 1L;");
 1003               emit("private java.lang.Object readResolve() { return forInt(intValue()); } ");
 1004               outdent();
 1005               emit("}");
 1006           }
 1007       }
 1008   
 1009       private boolean hasBase(SchemaType sType)
 1010       {
 1011           boolean hasBase;
 1012          	SchemaType baseEnumType = sType.getBaseEnumType();
 1013           if (baseEnumType.isAnonymousType() && baseEnumType.isSkippedAnonymousType())
 1014           {
 1015               if (sType.getContentBasedOnType() != null)
 1016                   hasBase = sType.getContentBasedOnType().getBaseType() != baseEnumType;
 1017               else
 1018                   hasBase = sType.getBaseType() != baseEnumType;
 1019           }
 1020           else
 1021               hasBase = baseEnumType != sType;
 1022   	return hasBase;
 1023       }
 1024   
 1025       String xmlTypeForProperty(SchemaProperty sProp)
 1026       {
 1027           SchemaType sType = sProp.javaBasedOnType();
 1028           return findJavaType(sType).replace('$', '.');
 1029       }
 1030   
 1031       static boolean xmlTypeForPropertyIsUnion(SchemaProperty sProp)
 1032       {
 1033           SchemaType sType = sProp.javaBasedOnType();
 1034           return (sType.isSimpleType() && sType.getSimpleVariety() == SchemaType.UNION);
 1035       }
 1036   
 1037       static boolean isJavaPrimitive(int javaType)
 1038       {
 1039           return (javaType < SchemaProperty.JAVA_FIRST_PRIMITIVE ? false :
 1040               (javaType > SchemaProperty.JAVA_LAST_PRIMITIVE ? false : true));
 1041       }
 1042   
 1043       /** Returns the wrapped type for a java primitive. */
 1044       static String javaWrappedType(int javaType)
 1045       {
 1046           switch (javaType)
 1047           {
 1048               case SchemaProperty.JAVA_BOOLEAN:
 1049                   return "java.lang.Boolean";
 1050               case SchemaProperty.JAVA_FLOAT:
 1051                   return "java.lang.Float";
 1052               case SchemaProperty.JAVA_DOUBLE:
 1053                   return "java.lang.Double";
 1054               case SchemaProperty.JAVA_BYTE:
 1055                   return "java.lang.Byte";
 1056               case SchemaProperty.JAVA_SHORT:
 1057                   return "java.lang.Short";
 1058               case SchemaProperty.JAVA_INT:
 1059                   return "java.lang.Integer";
 1060               case SchemaProperty.JAVA_LONG:
 1061                   return "java.lang.Long";
 1062   
 1063               // anything else is not a java primitive
 1064               default:
 1065                   assert false;
 1066                   throw new IllegalStateException();
 1067           }
 1068       }
 1069   
 1070       String javaTypeForProperty(SchemaProperty sProp)
 1071       {
 1072           // The type to use is the XML object....
 1073           if (sProp.getJavaTypeCode() == SchemaProperty.XML_OBJECT)
 1074           {
 1075               SchemaType sType = sProp.javaBasedOnType();
 1076               return findJavaType(sType).replace('$', '.');
 1077           }
 1078           
 1079           if (sProp.getJavaTypeCode() == SchemaProperty.JAVA_USER)
 1080           {
 1081                  return ((SchemaTypeImpl)sProp.getType()).getUserTypeName();
 1082           }
 1083   
 1084           switch (sProp.getJavaTypeCode())
 1085           {
 1086               case SchemaProperty.JAVA_BOOLEAN:
 1087                   return "boolean";
 1088               case SchemaProperty.JAVA_FLOAT:
 1089                   return "float";
 1090               case SchemaProperty.JAVA_DOUBLE:
 1091                   return "double";
 1092               case SchemaProperty.JAVA_BYTE:
 1093                   return "byte";
 1094               case SchemaProperty.JAVA_SHORT:
 1095                   return "short";
 1096               case SchemaProperty.JAVA_INT:
 1097                   return "int";
 1098               case SchemaProperty.JAVA_LONG:
 1099                   return "long";
 1100   
 1101               case SchemaProperty.JAVA_BIG_DECIMAL:
 1102                   return "java.math.BigDecimal";
 1103               case SchemaProperty.JAVA_BIG_INTEGER:
 1104                   return "java.math.BigInteger";
 1105               case SchemaProperty.JAVA_STRING:
 1106                   return "java.lang.String";
 1107               case SchemaProperty.JAVA_BYTE_ARRAY:
 1108                   return "byte[]";
 1109               case SchemaProperty.JAVA_GDATE:
 1110                   return "org.apache.xmlbeans.GDate";
 1111               case SchemaProperty.JAVA_GDURATION:
 1112                   return "org.apache.xmlbeans.GDuration";
 1113               case SchemaProperty.JAVA_DATE:
 1114                   return "java.util.Date";
 1115               case SchemaProperty.JAVA_QNAME:
 1116                   return "javax.xml.namespace.QName";
 1117               case SchemaProperty.JAVA_LIST:
 1118                   return "java.util.List";
 1119               case SchemaProperty.JAVA_CALENDAR:
 1120                   return "java.util.Calendar";
 1121   
 1122               case SchemaProperty.JAVA_ENUM:
 1123                   SchemaType sType = sProp.javaBasedOnType();
 1124                   if (sType.getSimpleVariety() == SchemaType.UNION)
 1125                       sType = sType.getUnionCommonBaseType();
 1126                   assert sType.getBaseEnumType() != null;
 1127                   if (hasBase(sType)) 
 1128                       return findJavaType(sType.getBaseEnumType()).replace('$', '.') + ".Enum";
 1129                   else
 1130                       return findJavaType(sType).replace('$', '.') + ".Enum";
 1131   
 1132               case SchemaProperty.JAVA_OBJECT:
 1133                   return "java.lang.Object";
 1134   
 1135               default:
 1136                   assert(false);
 1137                   throw new IllegalStateException();
 1138           }
 1139       }
 1140   
 1141       void printPropertyGetters(QName qName, boolean isAttr,
 1142                          String propertyName, int javaType,
 1143                          String type, String xtype,
 1144                          boolean nillable, boolean optional,
 1145                          boolean several, boolean singleton)
 1146          throws IOException
 1147       {
 1148           String propdesc = "\"" + qName.getLocalPart() + "\"" + (isAttr ? " attribute" : " element");
 1149           boolean xmltype = (javaType == SchemaProperty.XML_OBJECT);
 1150   
 1151           if (singleton)
 1152           {
 1153               printJavaDoc((several ? "Gets first " : "Gets the ") + propdesc);
 1154               emit(type + " get" + propertyName + "();");
 1155   
 1156               if (!xmltype)
 1157               {
 1158                   printJavaDoc((several ? "Gets (as xml) first " : "Gets (as xml) the ") + propdesc);
 1159                   emit(xtype + " xget" + propertyName + "();");
 1160               }
 1161   
 1162               if (nillable)
 1163               {
 1164                   printJavaDoc((several ? "Tests for nil first " : "Tests for nil ") + propdesc);
 1165                   emit("boolean isNil" + propertyName + "();");
 1166               }
 1167           }
 1168   
 1169           if (optional)
 1170           {
 1171               printJavaDoc((several ? "True if has at least one " : "True if has ") + propdesc);
 1172               emit("boolean isSet" + propertyName + "();");
 1173           }
 1174   
 1175           if (several)
 1176           {
 1177               String arrayName = propertyName + "Array";
 1178   
 1179               if (_useJava15)
 1180               {
 1181                   String wrappedType = type;
 1182                   if (isJavaPrimitive(javaType))
 1183                       wrappedType = javaWrappedType(javaType);
 1184   
 1185                   printJavaDoc("Gets a List of " + propdesc + "s");
 1186                   emit("java.util.List<" + wrappedType + "> get" + propertyName + "List();");
 1187               }
 1188   
 1189               if (_useJava15)
 1190               {
 1191                   emit("");
 1192                   emit("/**");
 1193                   emit(" * Gets array of all " + propdesc + "s");
 1194                   emit(" * @deprecated");
 1195                   emit(" */");
 1196                   emit("@Deprecated");
 1197               }
 1198               else
 1199                   printJavaDoc("Gets array of all " + propdesc + "s");
 1200               emit(type + "[] get" + arrayName + "();");
 1201   
 1202               printJavaDoc("Gets ith " + propdesc);
 1203               emit(type + " get" + arrayName + "(int i);");
 1204   
 1205               if (!xmltype)
 1206               {
 1207                   if (_useJava15)
 1208                   {
 1209                       printJavaDoc("Gets (as xml) a List of " + propdesc + "s");
 1210                       emit("java.util.List<" + xtype + "> xget" + propertyName + "List();");
 1211                   }
 1212   
 1213                   if (_useJava15)
 1214                   {
 1215                       emit("");
 1216                       emit("/**");
 1217                       emit(" * Gets (as xml) array of all " + propdesc + "s");
 1218                       emit(" * @deprecated");
 1219                       emit(" */");
 1220                       emit("@Deprecated");
 1221                   }
 1222                   else
 1223                       printJavaDoc("Gets (as xml) array of all " + propdesc + "s");
 1224                   emit(xtype + "[] xget" + arrayName + "();");
 1225   
 1226                   printJavaDoc("Gets (as xml) ith " + propdesc);
 1227                   emit(xtype + " xget" + arrayName + "(int i);");
 1228               }
 1229   
 1230               if (nillable)
 1231               {
 1232                   printJavaDoc("Tests for nil ith " + propdesc);
 1233                   emit("boolean isNil" + arrayName + "(int i);");
 1234               }
 1235   
 1236               printJavaDoc("Returns number of " + propdesc);
 1237               emit("int sizeOf" + arrayName + "();");
 1238           }
 1239       }
 1240   
 1241       void printPropertySetters(QName qName, boolean isAttr,
 1242                          String propertyName, int javaType, String type, String xtype,
 1243                          boolean nillable, boolean optional,
 1244                          boolean several, boolean singleton)
 1245          throws IOException
 1246       {
 1247           String safeVarName = NameUtil.nonJavaKeyword(NameUtil.lowerCamelCase(propertyName));
 1248           if (safeVarName.equals("i"))
 1249               safeVarName = "iValue";
 1250           boolean xmltype = (javaType == SchemaProperty.XML_OBJECT);
 1251   
 1252           String propdesc = "\"" + qName.getLocalPart() + "\"" + (isAttr ? " attribute" : " element");
 1253   
 1254           if (singleton)
 1255           {
 1256               printJavaDoc((several ? "Sets first " : "Sets the ") + propdesc);
 1257               emit("void set" + propertyName + "(" + type + " " + safeVarName + ");");
 1258   
 1259               if (!xmltype)
 1260               {
 1261                   printJavaDoc((several ? "Sets (as xml) first " : "Sets (as xml) the ") + propdesc);
 1262                   emit("void xset" + propertyName + "(" + xtype + " " + safeVarName + ");");
 1263               }
 1264   
 1265               if (xmltype && !several)
 1266               {
 1267                   printJavaDoc("Appends and returns a new empty " + propdesc);
 1268                   emit(xtype + " addNew" + propertyName + "();");
 1269               }
 1270   
 1271               if (nillable)
 1272               {
 1273                   printJavaDoc((several ? "Nils the first " : "Nils the ") + propdesc);
 1274                   emit("void setNil" + propertyName + "();");
 1275               }
 1276           }
 1277   
 1278           if (optional)
 1279           {
 1280               printJavaDoc((several ? "Removes first " : "Unsets the ") + propdesc);
 1281               emit("void unset" + propertyName + "();");
 1282           }
 1283   
 1284           if (several)
 1285           {
 1286               String arrayName = propertyName + "Array";
 1287   
 1288               printJavaDoc("Sets array of all " + propdesc);
 1289               emit("void set" + arrayName + "(" + type + "[] " + safeVarName + "Array);");
 1290   
 1291               printJavaDoc("Sets ith " + propdesc);
 1292               emit("void set" + arrayName + "(int i, " + type + " " + safeVarName + ");");
 1293   
 1294               if (!xmltype)
 1295               {
 1296                   printJavaDoc("Sets (as xml) array of all " + propdesc);
 1297                   emit("void xset" + arrayName + "(" + xtype + "[] " + safeVarName + "Array);");
 1298   
 1299                   printJavaDoc("Sets (as xml) ith " + propdesc);
 1300                   emit("void xset" + arrayName + "(int i, " + xtype + " " + safeVarName + ");");
 1301               }
 1302   
 1303               if (nillable)
 1304               {
 1305                   printJavaDoc("Nils the ith " + propdesc);
 1306                   emit("void setNil" + arrayName + "(int i);");
 1307               }
 1308   
 1309               if (!xmltype)
 1310               {
 1311                   printJavaDoc("Inserts the value as the ith " + propdesc);
 1312                   emit("void insert" + propertyName + "(int i, " + type + " " + safeVarName + ");");
 1313   
 1314                   printJavaDoc("Appends the value as the last " + propdesc);
 1315                   emit("void add" + propertyName + "(" + type + " " + safeVarName + ");");
 1316               }
 1317   
 1318               printJavaDoc("Inserts and returns a new empty value (as xml) as the ith " + propdesc);
 1319               emit(xtype + " insertNew" + propertyName + "(int i);");
 1320   
 1321               printJavaDoc("Appends and returns a new empty value (as xml) as the last " + propdesc);
 1322               emit(xtype + " addNew" + propertyName + "();");
 1323   
 1324               printJavaDoc("Removes the ith " + propdesc);
 1325               emit("void remove" + propertyName + "(int i);");
 1326           }
 1327       }
 1328   
 1329       String getAtomicRestrictionType(SchemaType sType) {
 1330           SchemaType pType = sType.getPrimitiveType();
 1331           switch (pType.getBuiltinTypeCode())
 1332           {
 1333               case SchemaType.BTC_ANY_SIMPLE:
 1334                   return "org.apache.xmlbeans.impl.values.XmlAnySimpleTypeImpl";
 1335               case SchemaType.BTC_BOOLEAN:
 1336                   return "org.apache.xmlbeans.impl.values.JavaBooleanHolderEx";
 1337               case SchemaType.BTC_BASE_64_BINARY:
 1338                   return "org.apache.xmlbeans.impl.values.JavaBase64HolderEx";
 1339               case SchemaType.BTC_HEX_BINARY:
 1340                   return "org.apache.xmlbeans.impl.values.JavaHexBinaryHolderEx";
 1341               case SchemaType.BTC_ANY_URI:
 1342                   return "org.apache.xmlbeans.impl.values.JavaUriHolderEx";
 1343               case SchemaType.BTC_QNAME:
 1344                   return "org.apache.xmlbeans.impl.values.JavaQNameHolderEx";
 1345               case SchemaType.BTC_NOTATION:
 1346                   return "org.apache.xmlbeans.impl.values.JavaNotationHolderEx";
 1347               case SchemaType.BTC_FLOAT:
 1348                   return "org.apache.xmlbeans.impl.values.JavaFloatHolderEx";
 1349               case SchemaType.BTC_DOUBLE:
 1350                   return "org.apache.xmlbeans.impl.values.JavaDoubleHolderEx";
 1351               case SchemaType.BTC_DECIMAL:
 1352                   switch (sType.getDecimalSize())
 1353                   {
 1354                       default:
 1355                           assert(false);
 1356                       case SchemaType.SIZE_BIG_DECIMAL:
 1357                           return "org.apache.xmlbeans.impl.values.JavaDecimalHolderEx";
 1358                       case SchemaType.SIZE_BIG_INTEGER:
 1359                           return "org.apache.xmlbeans.impl.values.JavaIntegerHolderEx";
 1360                       case SchemaType.SIZE_LONG:
 1361                           return "org.apache.xmlbeans.impl.values.JavaLongHolderEx";
 1362                       case SchemaType.SIZE_INT:
 1363                       case SchemaType.SIZE_SHORT:
 1364                       case SchemaType.SIZE_BYTE:
 1365                           return "org.apache.xmlbeans.impl.values.JavaIntHolderEx";
 1366                   }
 1367               case SchemaType.BTC_STRING:
 1368                   if (sType.hasStringEnumValues())
 1369                       return "org.apache.xmlbeans.impl.values.JavaStringEnumerationHolderEx";
 1370                   else
 1371                       return "org.apache.xmlbeans.impl.values.JavaStringHolderEx";
 1372   
 1373               case SchemaType.BTC_DATE_TIME:
 1374               case SchemaType.BTC_TIME:
 1375               case SchemaType.BTC_DATE:
 1376               case SchemaType.BTC_G_YEAR_MONTH:
 1377               case SchemaType.BTC_G_YEAR:
 1378               case SchemaType.BTC_G_MONTH_DAY:
 1379               case SchemaType.BTC_G_DAY:
 1380               case SchemaType.BTC_G_MONTH:
 1381                   return "org.apache.xmlbeans.impl.values.JavaGDateHolderEx";
 1382   
 1383               case SchemaType.BTC_DURATION:
 1384                   return "org.apache.xmlbeans.impl.values.JavaGDurationHolderEx";
 1385               default:
 1386                   assert(false) : "unrecognized primitive type";
 1387                   return null;
 1388           }
 1389       }
 1390   
 1391       static SchemaType findBaseType(SchemaType sType)
 1392       {
 1393           while (sType.getFullJavaName() == null)
 1394               sType = sType.getBaseType();
 1395           return sType;
 1396       }
 1397   
 1398       String getBaseClass(SchemaType sType) {
 1399           SchemaType baseType = findBaseType(sType.getBaseType());
 1400   
 1401           switch (sType.getSimpleVariety())
 1402           {
 1403               case SchemaType.NOT_SIMPLE:
 1404                   // non-simple-content: inherit from base type impl
 1405                   if (!XmlObject.type.equals(baseType))
 1406                       return baseType.getFullJavaImplName();
 1407                   return "org.apache.xmlbeans.impl.values.XmlComplexContentImpl";
 1408   
 1409               case SchemaType.ATOMIC:
 1410                   // We should only get called for restrictions
 1411                   assert(! sType.isBuiltinType());
 1412                   return getAtomicRestrictionType(sType);
 1413   
 1414               case SchemaType.LIST:
 1415                   return "org.apache.xmlbeans.impl.values.XmlListImpl";
 1416   
 1417               case SchemaType.UNION:
 1418                   return "org.apache.xmlbeans.impl.values.XmlUnionImpl";
 1419   
 1420               default:
 1421                   throw new IllegalStateException();
 1422           }
 1423       }
 1424   
 1425       void printConstructor(SchemaType sType, String shortName) throws IOException {
 1426           emit("");
 1427           emit("public " + shortName + "(org.apache.xmlbeans.SchemaType sType)");
 1428           startBlock();
 1429           emit("super(sType" + (sType.getSimpleVariety() == SchemaType.NOT_SIMPLE ?
 1430                                "":
 1431                                ", " + !sType.isSimpleType()) +
 1432                ");");
 1433           endBlock();
 1434   
 1435           if (sType.getSimpleVariety() != SchemaType.NOT_SIMPLE)
 1436           {
 1437               emit("");
 1438               emit("protected " + shortName + "(org.apache.xmlbeans.SchemaType sType, boolean b)");
 1439               startBlock();
 1440               emit("super(sType, b);");
 1441               endBlock();
 1442           }
 1443       }
 1444   
 1445       void startClass(SchemaType sType, boolean isInner) throws IOException
 1446       {
 1447           String shortName = sType.getShortJavaImplName();
 1448           String baseClass = getBaseClass(sType);
 1449           StringBuffer interfaces = new StringBuffer();
 1450           interfaces.append(sType.getFullJavaName().replace('$', '.'));
 1451   
 1452           if (sType.getSimpleVariety() == SchemaType.UNION) {
 1453               SchemaType[] memberTypes = sType.getUnionMemberTypes();
 1454               for (int i = 0 ; i < memberTypes.length ; i++)
 1455                   interfaces.append(", " + memberTypes[i].getFullJavaName().replace('$', '.'));
 1456           }
 1457   
 1458           emit("public " + ( isInner ? "static ": "" ) + "class " + shortName +
 1459               " extends " + baseClass + " implements " + interfaces.toString());
 1460   
 1461           startBlock();
 1462   
 1463           emit("private static final long serialVersionUID = 1L;");
 1464       }
 1465   
 1466       void makeAttributeDefaultValue(String jtargetType, SchemaProperty prop, String identifier) throws IOException
 1467       {
 1468           String fullName = jtargetType;
 1469           if (fullName == null)
 1470               fullName = prop.javaBasedOnType().getFullJavaName().replace('$', '.');
 1471   
 1472           emit("target = (" + fullName + ")get_default_attribute_value(" + identifier + ");");
 1473       }
 1474   
 1475       void makeMissingValue(int javaType) throws IOException
 1476       {
 1477           switch (javaType)
 1478           {
 1479               case SchemaProperty.JAVA_BOOLEAN:
 1480                   emit("return false;"); break;
 1481   
 1482               case SchemaProperty.JAVA_FLOAT:
 1483                   emit("return 0.0f;"); break;
 1484   
 1485               case SchemaProperty.JAVA_DOUBLE:
 1486                   emit("return 0.0;"); break;
 1487   
 1488               case SchemaProperty.JAVA_BYTE:
 1489               case SchemaProperty.JAVA_SHORT:
 1490               case SchemaProperty.JAVA_INT:
 1491                   emit("return 0;"); break;
 1492   
 1493               case SchemaProperty.JAVA_LONG:
 1494                   emit("return 0L;"); break;
 1495   
 1496               default:
 1497               case SchemaProperty.XML_OBJECT:
 1498               case SchemaProperty.JAVA_BIG_DECIMAL:
 1499               case SchemaProperty.JAVA_BIG_INTEGER:
 1500               case SchemaProperty.JAVA_STRING:
 1501               case SchemaProperty.JAVA_BYTE_ARRAY:
 1502               case SchemaProperty.JAVA_GDATE:
 1503               case SchemaProperty.JAVA_GDURATION:
 1504               case SchemaProperty.JAVA_DATE:
 1505               case SchemaProperty.JAVA_QNAME:
 1506               case SchemaProperty.JAVA_LIST:
 1507               case SchemaProperty.JAVA_CALENDAR:
 1508               case SchemaProperty.JAVA_ENUM:
 1509               case SchemaProperty.JAVA_OBJECT:
 1510                   emit("return null;"); break;
 1511           }
 1512       }
 1513   
 1514       void printJGetArrayValue(int javaType, String type, SchemaTypeImpl stype) throws IOException {
 1515           switch (javaType)
 1516           {
 1517               case SchemaProperty.XML_OBJECT:
 1518                   emit(type + "[] result = new " + type + "[targetList.size()];");
 1519                   emit("targetList.toArray(result);");
 1520                   break;
 1521   
 1522               case SchemaProperty.JAVA_ENUM:
 1523                   emit(type + "[] result = new " + type + "[targetList.size()];");
 1524                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1525                   emit("    result[i] = (" + type + ")((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getEnumValue();");
 1526                   break;
 1527   
 1528               case SchemaProperty.JAVA_BOOLEAN:
 1529                   emit("boolean[] result = new boolean[targetList.size()];");
 1530                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1531                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getBooleanValue();");
 1532                   break;
 1533   
 1534               case SchemaProperty.JAVA_FLOAT:
 1535                   emit("float[] result = new float[targetList.size()];");
 1536                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1537                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getFloatValue();");
 1538                   break;
 1539   
 1540               case SchemaProperty.JAVA_DOUBLE:
 1541                   emit("double[] result = new double[targetList.size()];");
 1542                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1543                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getDoubleValue();");
 1544                   break;
 1545   
 1546               case SchemaProperty.JAVA_BYTE:
 1547                   emit("byte[] result = new byte[targetList.size()];");
 1548                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1549                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getByteValue();");
 1550                   break;
 1551   
 1552               case SchemaProperty.JAVA_SHORT:
 1553                   emit("short[] result = new short[targetList.size()];");
 1554                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1555                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getShortValue();");
 1556                   break;
 1557   
 1558               case SchemaProperty.JAVA_INT:
 1559                   emit("int[] result = new int[targetList.size()];");
 1560                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1561                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getIntValue();");
 1562                   break;
 1563   
 1564               case SchemaProperty.JAVA_LONG:
 1565                   emit("long[] result = new long[targetList.size()];");
 1566                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1567                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getLongValue();");
 1568                   break;
 1569   
 1570               case SchemaProperty.JAVA_BIG_DECIMAL:
 1571                   emit("java.math.BigDecimal[] result = new java.math.BigDecimal[targetList.size()];");
 1572                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1573                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getBigDecimalValue();");
 1574                   break;
 1575   
 1576               case SchemaProperty.JAVA_BIG_INTEGER:
 1577                   emit("java.math.BigInteger[] result = new java.math.BigInteger[targetList.size()];");
 1578                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1579                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getBigIntegerValue();");
 1580                   break;
 1581   
 1582               case SchemaProperty.JAVA_STRING:
 1583                   emit("java.lang.String[] result = new java.lang.String[targetList.size()];");
 1584                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1585                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getStringValue();");
 1586                   break;
 1587   
 1588               case SchemaProperty.JAVA_BYTE_ARRAY:
 1589                   emit("byte[][] result = new byte[targetList.size()][];");
 1590                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1591                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getByteArrayValue();");
 1592                   break;
 1593   
 1594               case SchemaProperty.JAVA_CALENDAR:
 1595                   emit("java.util.Calendar[] result = new java.util.Calendar[targetList.size()];");
 1596                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1597                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getCalendarValue();");
 1598                   break;
 1599   
 1600               case SchemaProperty.JAVA_DATE:
 1601                   emit("java.util.Date[] result = new java.util.Date[targetList.size()];");
 1602                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1603                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getDateValue();");
 1604                   break;
 1605   
 1606               case SchemaProperty.JAVA_GDATE:
 1607                   emit("org.apache.xmlbeans.GDate[] result = new org.apache.xmlbeans.GDate[targetList.size()];");
 1608                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1609                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getGDateValue();");
 1610                   break;
 1611   
 1612               case SchemaProperty.JAVA_GDURATION:
 1613                   emit("org.apache.xmlbeans.GDuration[] result = new org.apache.xmlbeans.GDuration[targetList.size()];");
 1614                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1615                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getGDurationValue();");
 1616                   break;
 1617   
 1618               case SchemaProperty.JAVA_QNAME:
 1619                   emit("javax.xml.namespace.QName[] result = new javax.xml.namespace.QName[targetList.size()];");
 1620                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1621                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getQNameValue();");
 1622                   break;
 1623   
 1624               case SchemaProperty.JAVA_LIST:
 1625                   emit("java.util.List[] result = new java.util.List[targetList.size()];");
 1626                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1627                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getListValue();");
 1628                   break;
 1629   
 1630               case SchemaProperty.JAVA_OBJECT:
 1631                   emit("java.lang.Object[] result = new java.lang.Object[targetList.size()];");
 1632                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1633                   emit("    result[i] = ((org.apache.xmlbeans.SimpleValue)targetList.get(i)).getObjectValue();");
 1634                   break;
 1635   
 1636               case SchemaProperty.JAVA_USER:
 1637                   emit(stype.getUserTypeName() + "[] result = new " + stype.getUserTypeName() + "[targetList.size()];");
 1638                   emit("for (int i = 0, len = targetList.size() ; i < len ; i++)");
 1639                   emit("    result[i] = " + getUserTypeStaticHandlerMethod(false, stype)
 1640                           + "((org.apache.xmlbeans.SimpleValue)targetList.get(i));");
 1641                   break;
 1642                   
 1643               default:
 1644                   throw new IllegalStateException();
 1645           }
 1646           emit("return result;");
 1647       }
 1648       void printJGetValue(int javaType, String type, SchemaTypeImpl stype) throws IOException {
 1649           switch (javaType)
 1650           {
 1651           case SchemaProperty.XML_OBJECT:
 1652               emit("return target;"); break;
 1653   
 1654           case SchemaProperty.JAVA_BOOLEAN:
 1655               emit("return target.getBooleanValue();"); break;
 1656   
 1657           case SchemaProperty.JAVA_FLOAT:
 1658               emit("return target.getFloatValue();"); break;
 1659   
 1660           case SchemaProperty.JAVA_DOUBLE:
 1661               emit("return target.getDoubleValue();"); break;
 1662   
 1663           case SchemaProperty.JAVA_BYTE:
 1664               emit("return target.getByteValue();"); break;
 1665   
 1666           case SchemaProperty.JAVA_SHORT:
 1667               emit("return target.getShortValue();"); break;
 1668   
 1669           case SchemaProperty.JAVA_INT:
 1670               emit("return target.getIntValue();"); break;
 1671   
 1672           case SchemaProperty.JAVA_LONG:
 1673               emit("return target.getLongValue();"); break;
 1674   
 1675           case SchemaProperty.JAVA_BIG_DECIMAL:
 1676               emit("return target.getBigDecimalValue();"); break;
 1677   
 1678           case SchemaProperty.JAVA_BIG_INTEGER:
 1679               emit("return target.getBigIntegerValue();"); break;
 1680   
 1681           case SchemaProperty.JAVA_STRING:
 1682               emit("return target.getStringValue();"); break;
 1683   
 1684           case SchemaProperty.JAVA_BYTE_ARRAY:
 1685               emit("return target.getByteArrayValue();"); break;
 1686   
 1687           case SchemaProperty.JAVA_GDATE:
 1688               emit("return target.getGDateValue();"); break;
 1689   
 1690           case SchemaProperty.JAVA_GDURATION:
 1691               emit("return target.getGDurationValue();"); break;
 1692   
 1693           case SchemaProperty.JAVA_CALENDAR:
 1694               emit("return target.getCalendarValue();"); break;
 1695   
 1696           case SchemaProperty.JAVA_DATE:
 1697               emit("return target.getDateValue();"); break;
 1698   
 1699           case SchemaProperty.JAVA_QNAME:
 1700               emit("return target.getQNameValue();"); break;
 1701   
 1702           case SchemaProperty.JAVA_LIST:
 1703               emit("return target.getListValue();"); break;
 1704   
 1705           case SchemaProperty.JAVA_ENUM:
 1706               emit("return (" + type + ")target.getEnumValue();"); break;
 1707   
 1708           case SchemaProperty.JAVA_OBJECT:
 1709               emit("return target.getObjectValue();"); break;
 1710               
 1711           case SchemaProperty.JAVA_USER:
 1712               emit("return " + getUserTypeStaticHandlerMethod(false, stype)
 1713                       + "(target);");
 1714               break;
 1715               
 1716           default:
 1717               throw new IllegalStateException();
 1718           }
 1719       }
 1720       void printJSetValue(int javaType, String safeVarName, SchemaTypeImpl stype) throws IOException {
 1721           switch (javaType)
 1722           {
 1723               case SchemaProperty.XML_OBJECT:
 1724                   emit("target.set(" + safeVarName + ");"); break;
 1725   
 1726               case SchemaProperty.JAVA_BOOLEAN:
 1727                   emit("target.setBooleanValue(" + safeVarName + ");"); break;
 1728   
 1729               case SchemaProperty.JAVA_FLOAT:
 1730                   emit("target.setFloatValue(" + safeVarName + ");"); break;
 1731   
 1732               case SchemaProperty.JAVA_DOUBLE:
 1733                   emit("target.setDoubleValue(" + safeVarName + ");"); break;
 1734   
 1735               case SchemaProperty.JAVA_BYTE:
 1736                   emit("target.setByteValue(" + safeVarName + ");"); break;
 1737   
 1738               case SchemaProperty.JAVA_SHORT:
 1739                   emit("target.setShortValue(" + safeVarName + ");"); break;
 1740   
 1741               case SchemaProperty.JAVA_INT:
 1742                   emit("target.setIntValue(" + safeVarName + ");"); break;
 1743   
 1744               case SchemaProperty.JAVA_LONG:
 1745                   emit("target.setLongValue(" + safeVarName + ");"); break;
 1746   
 1747               case SchemaProperty.JAVA_BIG_DECIMAL:
 1748                   emit("target.setBigDecimalValue(" + safeVarName + ");"); break;
 1749   
 1750               case SchemaProperty.JAVA_BIG_INTEGER:
 1751                   emit("target.setBigIntegerValue(" + safeVarName + ");"); break;
 1752   
 1753               case SchemaProperty.JAVA_STRING:
 1754                   emit("target.setStringValue(" + safeVarName + ");"); break;
 1755   
 1756               case SchemaProperty.JAVA_BYTE_ARRAY:
 1757                   emit("target.setByteArrayValue(" + safeVarName + ");"); break;
 1758   
 1759               case SchemaProperty.JAVA_GDATE:
 1760                   emit("target.setGDateValue(" + safeVarName + ");"); break;
 1761   
 1762               case SchemaProperty.JAVA_GDURATION:
 1763                   emit("target.setGDurationValue(" + safeVarName + ");"); break;
 1764   
 1765               case SchemaProperty.JAVA_CALENDAR:
 1766                   emit("target.setCalendarValue(" + safeVarName + ");"); break;
 1767   
 1768               case SchemaProperty.JAVA_DATE:
 1769                   emit("target.setDateValue(" + safeVarName + ");"); break;
 1770   
 1771               case SchemaProperty.JAVA_QNAME:
 1772                   emit("target.setQNameValue(" + safeVarName + ");"); break;
 1773   
 1774               case SchemaProperty.JAVA_LIST:
 1775                   emit("target.setListValue(" + safeVarName + ");"); break;
 1776   
 1777               case SchemaProperty.JAVA_ENUM:
 1778                   emit("target.setEnumValue(" + safeVarName + ");"); break;
 1779   
 1780               case SchemaProperty.JAVA_OBJECT:
 1781                   emit("target.setObjectValue(" + safeVarName + ");"); break;
 1782                   
 1783               case SchemaProperty.JAVA_USER:
 1784                   emit(getUserTypeStaticHandlerMethod(true, stype)
 1785                           + "(" + safeVarName + ", target);");
 1786                   break;
 1787   
 1788               default:
 1789                   throw new IllegalStateException();
 1790           }
 1791       }
 1792   
 1793       String getIdentifier(Map qNameMap, QName qName) {
 1794           return ((String[])qNameMap.get(qName))[0];
 1795       }
 1796   
 1797       String getSetIdentifier(Map qNameMap, QName qName) {
 1798           String[] identifiers = (String[])qNameMap.get(qName);
 1799           return identifiers[1] == null ? identifiers[0] : identifiers[1];
 1800       }
 1801   
 1802       Map printStaticFields(SchemaProperty[] properties) throws IOException {
 1803           final Map results = new HashMap();
 1804   
 1805           emit("");
 1806           for (int i = 0; i < properties.length; i++)
 1807           {
 1808               final String[] identifiers = new String[2];
 1809               final SchemaProperty prop = properties[i];
 1810               final QName name = prop.getName();
 1811               results.put(name, identifiers);
 1812               final String javaName = prop.getJavaPropertyName();
 1813               identifiers[0] = (javaName + "$" + (i * 2)).toUpperCase();
 1814               final String uriString =  "\"" + name.getNamespaceURI() + "\"";
 1815   
 1816               emit("private static final javax.xml.namespace.QName " + identifiers[0] +
 1817                    " = " );
 1818               indent();
 1819               emit("new javax.xml.namespace.QName(" +
 1820                    uriString + ", \"" + name.getLocalPart() + "\");");
 1821               outdent();
 1822   
 1823               if (properties[i].acceptedNames() != null)
 1824               {
 1825                   final QName[] qnames = properties[i].acceptedNames();
 1826   
 1827                   if (qnames.length > 1)
 1828                   {
 1829                       identifiers[1] = (javaName + "$" + (i*2+1)).toUpperCase();
 1830   
 1831                       emit("private static final org.apache.xmlbeans.QNameSet " + identifiers[1] +
 1832                           " = org.apache.xmlbeans.QNameSet.forArray( new javax.xml.namespace.QName[] { " );
 1833                       indent();
 1834                       for (int j = 0 ; j < qnames.length ; j++)
 1835                       {
 1836                           emit("new javax.xml.namespace.QName(\"" + qnames[j].getNamespaceURI() +
 1837                               "\", \"" + qnames[j].getLocalPart() + "\"),");
 1838                       }
 1839   
 1840                       outdent();
 1841   
 1842                       emit("});");
 1843                   }
 1844               }
 1845           }
 1846           emit("");
 1847           return results;
 1848       }
 1849   
 1850       void emitImplementationPreamble() throws IOException
 1851       {
 1852           emit("synchronized (monitor())");
 1853           emit("{");
 1854           indent();
 1855           emit("check_orphaned();");
 1856       }
 1857   
 1858       void emitImplementationPostamble() throws IOException
 1859       {
 1860           outdent();
 1861           emit("}");
 1862       }
 1863   
 1864       void emitDeclareTarget(boolean declareTarget, String xtype)
 1865           throws IOException
 1866       {
 1867            if (declareTarget)
 1868                emit(xtype + " target = null;");
 1869     	}
 1870   
 1871       void emitAddTarget(String identifier, boolean isAttr, boolean declareTarget, String xtype)
 1872           throws IOException
 1873       {
 1874           if (isAttr)
 1875               emit("target = (" + xtype + ")get_store().add_attribute_user(" + identifier + ");");
 1876           else
 1877               emit("target = (" + xtype + ")get_store().add_element_user(" + identifier + ");");
 1878       }
 1879   
 1880       void emitPre(SchemaType sType, int opType, String identifier, boolean isAttr) throws IOException
 1881       {
 1882           emitPre(sType, opType, identifier, isAttr, "-1");
 1883       }
 1884   
 1885       void emitPre(SchemaType sType, int opType, String identifier, boolean isAttr, String index) throws IOException
 1886       {
 1887           SchemaTypeImpl sImpl = getImpl(sType);
 1888           if (sImpl == null)
 1889               return;
 1890   
 1891           PrePostExtension ext = sImpl.getPrePostExtension();
 1892           if (ext != null)
 1893           {
 1894               if (ext.hasPreCall())
 1895               {
 1896                   emit("if ( " + ext.getStaticHandler() + ".preSet(" + prePostOpString(opType) + ", this, " + identifier + ", " + isAttr + ", " + index + "))");
 1897                   startBlock();
 1898               }
 1899           }
 1900       }
 1901   
 1902       void emitPost(SchemaType sType, int opType, String identifier, boolean isAttr) throws IOException
 1903       {
 1904           emitPost(sType, opType, identifier, isAttr, "-1");
 1905       }
 1906   
 1907       void emitPost(SchemaType sType, int opType, String identifier, boolean isAttr, String index) throws IOException
 1908       {
 1909           SchemaTypeImpl sImpl = getImpl(sType);
 1910           if (sImpl == null)
 1911               return;
 1912   
 1913           PrePostExtension ext = sImpl.getPrePostExtension();
 1914           if (ext != null)
 1915           {
 1916               if (ext.hasPreCall())
 1917               {
 1918                   endBlock();
 1919               }
 1920   
 1921               if (ext.hasPostCall())
 1922                   emit(ext.getStaticHandler() + ".postSet(" + prePostOpString(opType) + ", this, " + identifier + ", " + isAttr + ", " + index + ");");
 1923           }
 1924       }
 1925   
 1926       String prePostOpString(int opType)
 1927       {
 1928           switch (opType)
 1929           {
 1930               default:
 1931                   assert false;
 1932   
 1933               case PrePostExtension.OPERATION_SET:
 1934                   return "org.apache.xmlbeans.PrePostExtension.OPERATION_SET";
 1935   
 1936               case PrePostExtension.OPERATION_INSERT:
 1937                   return "org.apache.xmlbeans.PrePostExtension.OPERATION_INSERT";
 1938   
 1939               case PrePostExtension.OPERATION_REMOVE:
 1940                   return "org.apache.xmlbeans.PrePostExtension.OPERATION_REMOVE";
 1941           }
 1942       }
 1943   
 1944       private static final int NOTHING = 1;
 1945       private static final int ADD_NEW_VALUE = 3;
 1946       private static final int THROW_EXCEPTION = 4;
 1947   
 1948       void emitGetTarget(String setIdentifier,
 1949                          String identifier,
 1950                          boolean isAttr,
 1951                          String index,
 1952                          int nullBehaviour,
 1953                          String xtype)
 1954           throws IOException
 1955       {
 1956           assert setIdentifier != null && identifier != null;
 1957   
 1958           emit(xtype + " target = null;");
 1959   
 1960           if (isAttr)
 1961               emit("target = (" + xtype + ")get_store().find_attribute_user(" + identifier + ");");
 1962           else
 1963               emit("target = (" + xtype + ")get_store().find_element_user(" + setIdentifier + ", " + index + ");");
 1964   
 1965           if (nullBehaviour == NOTHING)
 1966               return;
 1967   
 1968           emit("if (target == null)");
 1969   
 1970           startBlock();
 1971   
 1972           switch (nullBehaviour)
 1973           {
 1974               case ADD_NEW_VALUE:
 1975                   // target already emited, no need for emitDeclareTarget(false, xtype);
 1976                   emitAddTarget(identifier, isAttr, false, xtype);
 1977                   break;
 1978   
 1979               case THROW_EXCEPTION:
 1980                   emit("throw new IndexOutOfBoundsException();");
 1981                   break;
 1982   
 1983               case NOTHING:
 1984                   break;
 1985   
 1986               default:
 1987                   assert false : "Bad behaviour type: " + nullBehaviour;
 1988           }
 1989   
 1990           endBlock();
 1991       }
 1992   
 1993       void printListGetter15Impl(String parentJavaName,
 1994           String propdesc, String propertyName,
 1995           String wrappedType, String xtype,
 1996           boolean xmltype, boolean xget)
 1997               throws IOException
 1998       {
 1999           String arrayName = propertyName + "Array";
 2000           String listName = propertyName + "List";
 2001           String parentThis = parentJavaName + ".this.";
 2002   
 2003           String xgetMethod = (xget ? "x" : "") + "get";
 2004           String xsetMethod = (xget ? "x" : "") + "set";
 2005   
 2006           printJavaDoc("Gets " + (xget ? "(as xml) " : "") + "a List of " + propdesc + "s");
 2007   
 2008           emit("public java.util.List<" + wrappedType + "> " + xgetMethod + listName  + "()");
 2009           startBlock();
 2010   
 2011           emit("final class " + listName + " extends java.util.AbstractList<" + wrappedType + ">");
 2012           startBlock();
 2013   
 2014           // Object get(i)
 2015           if (_useJava15)
 2016               emit("@Override");
 2017           emit("public " + wrappedType + " get(int i)");
 2018           emit("    { return " + parentThis + xgetMethod + arrayName + "(i); }");
 2019           emit("");
 2020   
 2021           // Object set(i, o)
 2022           if (_useJava15)
 2023               emit("@Override");
 2024           emit("public " + wrappedType + " set(int i, " + wrappedType + " o)");
 2025           startBlock();
 2026           emit(wrappedType + " old = " + parentThis + xgetMethod + arrayName + "(i);");
 2027           emit(parentThis + xsetMethod + arrayName + "(i, o);");
 2028           emit("return old;");
 2029           endBlock();
 2030           emit("");
 2031   
 2032           // void add(i, o)
 2033           if (_useJava15)
 2034               emit("@Override");
 2035           emit("public void add(int i, " + wrappedType +" o)");
 2036           if (xmltype || xget)
 2037               emit("    { " + parentThis + "insertNew" + propertyName + "(i).set(o); }");
 2038           else
 2039               emit("    { " + parentThis + "insert" + propertyName + "(i, o); }");
 2040           emit("");
 2041   
 2042           // Object remove(i)
 2043           if (_useJava15)
 2044               emit("@Override");
 2045           emit("public " + wrappedType +" remove(int i)");
 2046           startBlock();
 2047           emit(wrappedType + " old = " + parentThis + xgetMethod + arrayName + "(i);");
 2048           emit(parentThis + "remove" + propertyName + "(i);");
 2049           emit("return old;");
 2050           endBlock();
 2051           emit("");
 2052   
 2053           // int size()
 2054           if (_useJava15)
 2055               emit("@Override");
 2056           emit("public int size()");
 2057           emit("    { return " + parentThis + "sizeOf" + arrayName + "(); }");
 2058           emit("");
 2059   
 2060           endBlock();
 2061   
 2062           emit("");
 2063   
 2064           emitImplementationPreamble();
 2065   
 2066           emit("return new " + listName + "();");
 2067   
 2068           emitImplementationPostamble();
 2069           endBlock();
 2070       }
 2071   
 2072       void printGetterImpls(String parentJavaName,
 2073           SchemaProperty prop, QName qName, boolean isAttr, String propertyName,
 2074           int javaType, String type, String xtype, boolean nillable,
 2075           boolean optional, boolean several, boolean singleton,
 2076           boolean isunion,
 2077           String identifier, String setIdentifier )
 2078               throws IOException
 2079       {
 2080           String propdesc = "\"" + qName.getLocalPart() + "\"" + (isAttr ? " attribute" : " element");
 2081           boolean xmltype = (javaType == SchemaProperty.XML_OBJECT);
 2082           String jtargetType = (isunion || !xmltype) ? "org.apache.xmlbeans.SimpleValue" : xtype;
 2083   
 2084           if (singleton)
 2085           {
 2086               // Value getProp()
 2087               printJavaDoc((several ? "Gets first " : "Gets the ") + propdesc);
 2088               emit("public " + type + " get" + propertyName + "()");
 2089               startBlock();
 2090               emitImplementationPreamble();
 2091   
 2092               emitGetTarget(setIdentifier, identifier, isAttr, "0", NOTHING, jtargetType);
 2093   
 2094               if (isAttr && (prop.hasDefault() == SchemaProperty.CONSISTENTLY ||
 2095                       prop.hasFixed() == SchemaProperty.CONSISTENTLY))
 2096               {
 2097                   emit("if (target == null)");
 2098                   startBlock();
 2099                   makeAttributeDefaultValue(jtargetType, prop, identifier);
 2100                   endBlock();
 2101               }
 2102               emit("if (target == null)");
 2103               startBlock();
 2104               makeMissingValue(javaType);
 2105               endBlock();
 2106   
 2107               
 2108               printJGetValue(javaType, type, (SchemaTypeImpl)prop.getType());    
 2109               
 2110   
 2111               emitImplementationPostamble();
 2112   
 2113               endBlock();
 2114   
 2115               if (!xmltype)
 2116               {
 2117                   // Value xgetProp()
 2118                   printJavaDoc((several ? "Gets (as xml) first " : "Gets (as xml) the ") + propdesc);
 2119                   emit("public " + xtype + " xget" + propertyName + "()");
 2120                   startBlock();
 2121                   emitImplementationPreamble();
 2122                   emitGetTarget(setIdentifier, identifier, isAttr, "0", NOTHING, xtype);
 2123   
 2124                   if (isAttr && (prop.hasDefault() == SchemaProperty.CONSISTENTLY ||
 2125                           prop.hasFixed() == SchemaProperty.CONSISTENTLY))
 2126                   {
 2127                       emit("if (target == null)");
 2128                       startBlock();
 2129                       makeAttributeDefaultValue(xtype, prop, identifier);
 2130                       endBlock();
 2131                   }
 2132   
 2133                   emit("return target;");
 2134                   emitImplementationPostamble();
 2135                   endBlock();
 2136               }
 2137   
 2138               if (nillable)
 2139               {
 2140                   // boolean isNilProp()
 2141                   printJavaDoc((several ? "Tests for nil first " : "Tests for nil ") + propdesc);
 2142                   emit("public boolean isNil" + propertyName + "()");
 2143                   startBlock();
 2144                   emitImplementationPreamble();
 2145                   emitGetTarget(setIdentifier, identifier, isAttr, "0", NOTHING, xtype);
 2146   
 2147                   emit("if (target == null) return false;");
 2148                   emit("return target.isNil();");
 2149                   emitImplementationPostamble();
 2150                   endBlock();
 2151               }
 2152           }
 2153   
 2154           if (optional)
 2155           {
 2156               // boolean isSetProp()
 2157               printJavaDoc((several ? "True if has at least one " : "True if has ") + propdesc);
 2158               emit("public boolean isSet" + propertyName + "()");
 2159   
 2160               startBlock();
 2161               emitImplementationPreamble();
 2162   
 2163               if (isAttr)
 2164                   emit("return get_store().find_attribute_user(" + identifier +") != null;");
 2165               else
 2166                   emit("return get_store().count_elements(" + setIdentifier + ") != 0;");
 2167   
 2168               emitImplementationPostamble();
 2169               endBlock();
 2170           }
 2171   
 2172           if (several)
 2173           {
 2174               String arrayName = propertyName + "Array";
 2175   
 2176               if (_useJava15)
 2177               {
 2178                   // use boxed type if the java type is a primitive and jdk1.5
 2179                   // jdk1.5 will box/unbox for us
 2180                   String wrappedType = type;
 2181                   if (isJavaPrimitive(javaType))
 2182                       wrappedType = javaWrappedType(javaType);
 2183   
 2184                   printListGetter15Impl(parentJavaName, propdesc, propertyName, wrappedType, xtype, xmltype, false);
 2185               }
 2186   
 2187               // Value[] getProp()
 2188               if (_useJava15)
 2189               {
 2190                   emit("");
 2191                   emit("/**");
 2192                   emit(" * Gets array of all " + propdesc + "s");
 2193                   emit(" * @deprecated");
 2194                   emit(" */");
 2195                   emit("@Deprecated");
 2196               }
 2197               else
 2198                   printJavaDoc("Gets array of all " + propdesc + "s");
 2199               emit("public " + type + "[] get" + arrayName + "()");
 2200               startBlock();
 2201               emitImplementationPreamble();
 2202   
 2203               if (_useJava15)
 2204                   emit("java.util.List<" + xtype + "> targetList = new java.util.ArrayList<" + xtype + ">();");
 2205               else
 2206                   emit("java.util.List targetList = new java.util.ArrayList();");
 2207               emit("get_store().find_all_element_users(" + setIdentifier + ", targetList);");
 2208   
 2209               printJGetArrayValue(javaType, type, (SchemaTypeImpl)prop.getType());
 2210   
 2211               emitImplementationPostamble();
 2212               endBlock();
 2213   
 2214               // Value getProp(int i)
 2215               printJavaDoc("Gets ith " + propdesc);
 2216               emit("public " + type + " get" + arrayName + "(int i)");
 2217               startBlock();
 2218               emitImplementationPreamble();
 2219   
 2220               emitGetTarget(setIdentifier, identifier, isAttr, "i", THROW_EXCEPTION, jtargetType);
 2221               printJGetValue(javaType, type, (SchemaTypeImpl)prop.getType());
 2222   
 2223               emitImplementationPostamble();
 2224               endBlock();
 2225   
 2226               if (!xmltype)
 2227               {
 2228                   if (_useJava15)
 2229                   {
 2230                       printListGetter15Impl(parentJavaName, propdesc, propertyName, xtype, xtype, xmltype, true);
 2231                   }
 2232   
 2233                   // Value[] xgetProp()
 2234                   if (_useJava15)
 2235                   {
 2236                       emit("");
 2237                       emit("/**");
 2238                       emit(" * Gets array of all " + propdesc + "s");
 2239                       emit(" * @deprecated");
 2240                       emit(" */");
 2241                       emit("@Deprecated");
 2242                   }
 2243                   else
 2244                       printJavaDoc("Gets (as xml) array of all " + propdesc + "s");
 2245                   emit("public " + xtype + "[] xget" + arrayName + "()");
 2246                   startBlock();
 2247                   emitImplementationPreamble();
 2248                   if (_useJava15)
 2249                       emit("java.util.List<" + xtype +  "> targetList = new java.util.ArrayList<" + xtype +  ">();");
 2250                   else
 2251                       emit("java.util.List targetList = new java.util.ArrayList();");
 2252                   emit("get_store().find_all_element_users(" + setIdentifier + ", targetList);");
 2253                   emit(xtype + "[] result = new " + xtype + "[targetList.size()];");
 2254                   emit("targetList.toArray(result);");
 2255                   emit("return result;");
 2256                   emitImplementationPostamble();
 2257                   endBlock();
 2258   
 2259                   // Value xgetProp(int i)
 2260                   printJavaDoc("Gets (as xml) ith " + propdesc);
 2261                   emit("public " + xtype + " xget" + arrayName + "(int i)");
 2262                   startBlock();
 2263                   emitImplementationPreamble();
 2264                   emitGetTarget(setIdentifier, identifier, isAttr, "i", THROW_EXCEPTION, xtype);
 2265                   emit("return target;");
 2266                   emitImplementationPostamble();
 2267                   endBlock();
 2268   
 2269               }
 2270   
 2271               if (nillable)
 2272               {
 2273                   // boolean isNil(int i);
 2274                   printJavaDoc("Tests for nil ith " + propdesc);
 2275                   emit("public boolean isNil" + arrayName + "(int i)");
 2276                   startBlock();
 2277                   emitImplementationPreamble();
 2278                   emitGetTarget(setIdentifier, identifier, isAttr, "i", THROW_EXCEPTION, xtype);
 2279                   emit("return target.isNil();");
 2280                   emitImplementationPostamble();
 2281                   endBlock();
 2282               }
 2283   
 2284               // int countProp();
 2285               printJavaDoc("Returns number of " + propdesc);
 2286               emit("public int sizeOf" + arrayName + "()");
 2287               startBlock();
 2288               emitImplementationPreamble();
 2289               emit("return get_store().count_elements(" + setIdentifier +");");
 2290               emitImplementationPostamble();
 2291               endBlock();
 2292           }
 2293       }
 2294   
 2295          void printSetterImpls(QName qName, SchemaProperty prop, boolean isAttr,
 2296                          String propertyName, int javaType, String type, String xtype,
 2297                          boolean nillable, boolean optional, boolean several, boolean singleton,
 2298                          boolean isunion, String identifier, String setIdentifier, SchemaType sType)
 2299           throws IOException
 2300       {
 2301           String safeVarName = NameUtil.nonJavaKeyword(NameUtil.lowerCamelCase(propertyName));
 2302           safeVarName = NameUtil.nonExtraKeyword(safeVarName);
 2303   
 2304           boolean xmltype = (javaType == SchemaProperty.XML_OBJECT);
 2305           boolean isobj = (javaType == SchemaProperty.JAVA_OBJECT);
 2306           boolean isSubstGroup = identifier != setIdentifier;
 2307           String jtargetType = (isunion || !xmltype) ? "org.apache.xmlbeans.SimpleValue" : xtype;
 2308   
 2309           String propdesc = "\"" + qName.getLocalPart() + "\"" + (isAttr ? " attribute" : " element");
 2310   
 2311           if (singleton)
 2312           {
 2313               // void setProp(Value v);
 2314               printJavaDoc((several ? "Sets first " : "Sets the ") + propdesc);
 2315               emit("public void set" + propertyName + "(" + type + " " + safeVarName + ")");
 2316               startBlock();
 2317               if ( xmltype && !isSubstGroup )
 2318               {
 2319                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, several ? "0" : "-1");
 2320                   emit("generatedSetterHelperImpl(" + safeVarName + ", " + setIdentifier + ", 0, " +
 2321                           "org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_SINGLETON);");
 2322                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, several ? "0" : "-1");
 2323               }
 2324               else
 2325               {
 2326                   emitImplementationPreamble();
 2327                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, several ? "0" : "-1");
 2328                   emitGetTarget(setIdentifier, identifier, isAttr, "0", ADD_NEW_VALUE, jtargetType);
 2329                   printJSetValue(javaType, safeVarName, (SchemaTypeImpl)prop.getType());
 2330                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, several ? "0" : "-1");
 2331                   emitImplementationPostamble();
 2332               }
 2333               endBlock();
 2334   
 2335               if (!xmltype)
 2336               {
 2337                   // void xsetProp(Value v)
 2338                   printJavaDoc((several ? "Sets (as xml) first " : "Sets (as xml) the ") + propdesc);
 2339                   emit("public void xset" + propertyName + "(" + xtype + " " + safeVarName + ")");
 2340                   startBlock();
 2341                   emitImplementationPreamble();
 2342                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, several ? "0" : "-1");
 2343                   emitGetTarget(setIdentifier, identifier, isAttr, "0", ADD_NEW_VALUE, xtype);
 2344                   emit("target.set(" + safeVarName + ");");
 2345                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, several ? "0" : "-1");
 2346                   emitImplementationPostamble();
 2347                   endBlock();
 2348   
 2349               }
 2350   
 2351               if (xmltype && !several)
 2352               {
 2353                   // Value addNewProp()
 2354                   printJavaDoc("Appends and returns a new empty " + propdesc);
 2355                   emit("public " + xtype + " addNew" + propertyName + "()");
 2356                   startBlock();
 2357                   emitImplementationPreamble();
 2358                   emitDeclareTarget(true, xtype);
 2359     	            emitPre(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr);
 2360                   emitAddTarget(identifier, isAttr, true, xtype);
 2361                   emitPost(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr);
 2362                   emit("return target;");
 2363                   emitImplementationPostamble();
 2364                   endBlock();
 2365               }
 2366   
 2367               if (nillable)
 2368               {
 2369                   printJavaDoc((several ? "Nils the first " : "Nils the ") + propdesc);
 2370                   emit("public void setNil" + propertyName + "()");
 2371                   startBlock();
 2372                   emitImplementationPreamble();
 2373                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, several ? "0" : "-1");
 2374                   emitGetTarget(setIdentifier, identifier, isAttr, "0", ADD_NEW_VALUE, xtype);
 2375                   emit("target.setNil();");
 2376                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, several ? "0" : "-1");
 2377                   emitImplementationPostamble();
 2378                   endBlock();
 2379               }
 2380           }
 2381   
 2382           if (optional)
 2383           {
 2384               printJavaDoc((several ? "Removes first " : "Unsets the ") + propdesc);
 2385               emit("public void unset" + propertyName + "()");
 2386               startBlock();
 2387               emitImplementationPreamble();
 2388               emitPre(sType, PrePostExtension.OPERATION_REMOVE, identifier, isAttr, several ? "0" : "-1");
 2389               if (isAttr)
 2390                   emit("get_store().remove_attribute(" + identifier + ");");
 2391               else
 2392                   emit("get_store().remove_element(" + setIdentifier + ", 0);");
 2393               emitPost(sType, PrePostExtension.OPERATION_REMOVE, identifier, isAttr, several ? "0" : "-1");
 2394               emitImplementationPostamble();
 2395               endBlock();
 2396           }
 2397   
 2398           if (several)
 2399           {
 2400               String arrayName = propertyName + "Array";
 2401   
 2402               if ( xmltype )
 2403               {
 2404                   printJavaDoc("Sets array of all " + propdesc + "  WARNING: This method is not atomicaly synchronized.");
 2405                   emit("public void set" + arrayName + "(" + type + "[] " + safeVarName + "Array)");
 2406                   startBlock();
 2407                   // do not use synchronize (monitor()) {  and GlobalLock inside  } !!! deadlock
 2408                   //emitImplementationPreamble();
 2409                   emit("check_orphaned();");
 2410                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr);
 2411   
 2412                   if (isobj)
 2413                   {
 2414                       if (!isSubstGroup)
 2415                           emit("unionArraySetterHelper(" + safeVarName + "Array" + ", " + identifier + ");" );
 2416                       else
 2417                           emit("unionArraySetterHelper(" + safeVarName + "Array" + ", " + identifier + ", " + setIdentifier + ");" );
 2418                   }
 2419                   else
 2420                   {
 2421                       if (!isSubstGroup)
 2422                           emit("arraySetterHelper(" + safeVarName + "Array" + ", " + identifier + ");" );
 2423                       else
 2424                           emit("arraySetterHelper(" + safeVarName + "Array" + ", " + identifier + ", " + setIdentifier + ");" );
 2425                   }
 2426   
 2427                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr);
 2428                   //emitImplementationPostamble();  to avoid deadlock
 2429                   endBlock();
 2430               }
 2431               else
 2432               {
 2433                   printJavaDoc("Sets array of all " + propdesc );
 2434                   emit("public void set" + arrayName + "(" + type + "[] " + safeVarName + "Array)");
 2435                   startBlock();
 2436                   emitImplementationPreamble();
 2437                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr);
 2438   
 2439                   if (isobj)
 2440                   {
 2441                       if (!isSubstGroup)
 2442                           emit("unionArraySetterHelper(" + safeVarName + "Array" + ", " + identifier + ");" );
 2443                       else
 2444                           emit("unionArraySetterHelper(" + safeVarName + "Array" + ", " + identifier + ", " + setIdentifier + ");" );
 2445                   }
 2446                   else if (prop.getJavaTypeCode() == SchemaProperty.JAVA_USER)
 2447                   {
 2448                       if (!isSubstGroup)
 2449                       {
 2450                           emit("org.apache.xmlbeans.SimpleValue[] dests = arraySetterHelper(" + safeVarName + "Array.length" + ", " + identifier + ");" );
 2451                           emit("for ( int i = 0 ; i < dests.length ; i++ ) {");
 2452                           emit("    " + getUserTypeStaticHandlerMethod(true, (SchemaTypeImpl)prop.getType())
 2453                                   + "(" + safeVarName + "Array[i], dests[i]);");
 2454                           emit("}");
 2455                       }
 2456                       else
 2457                       {
 2458                           emit("org.apache.xmlbeans.SimpleValue[] dests = arraySetterHelper(" + safeVarName + "Array.length" + ", " + identifier + ", " + setIdentifier + ");" );
 2459                           emit("for ( int i = 0 ; i < dests.length ; i++ ) {");
 2460                           emit("    " + getUserTypeStaticHandlerMethod(true, (SchemaTypeImpl)prop.getType())
 2461                                   + "(" + safeVarName + "Array[i], dests[i]);");
 2462                           emit("}");
 2463                       }
 2464                   }
 2465                   else
 2466                   {
 2467                       if (!isSubstGroup)
 2468                           emit("arraySetterHelper(" + safeVarName + "Array" + ", " + identifier + ");" );
 2469                       else
 2470                           emit("arraySetterHelper(" + safeVarName + "Array" + ", " + identifier + ", " + setIdentifier + ");" );
 2471                   }
 2472   
 2473                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr);
 2474                   emitImplementationPostamble();
 2475                   endBlock();
 2476               }
 2477   
 2478               printJavaDoc("Sets ith " + propdesc);
 2479               emit("public void set" + arrayName + "(int i, " + type + " " + safeVarName + ")");
 2480               startBlock();
 2481               if ( isobj && !isSubstGroup )
 2482               {
 2483                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
 2484                   emit("generatedSetterHelperImpl(" + safeVarName + ", " + setIdentifier + ", i, " +
 2485                           "org.apache.xmlbeans.impl.values.XmlObjectBase.KIND_SETTERHELPER_ARRAYITEM);");
 2486                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
 2487               }
 2488               else
 2489               {
 2490                   emitImplementationPreamble();
 2491                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
 2492                   emitGetTarget(setIdentifier, identifier, isAttr, "i", THROW_EXCEPTION, jtargetType);
 2493                   printJSetValue(javaType, safeVarName, (SchemaTypeImpl)prop.getType());
 2494                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
 2495                   emitImplementationPostamble();
 2496               }
 2497               endBlock();
 2498   
 2499               if (!xmltype)
 2500               {
 2501                   printJavaDoc("Sets (as xml) array of all " + propdesc);
 2502                   emit("public void xset" + arrayName + "(" + xtype + "[]" + safeVarName + "Array)");
 2503                   startBlock();
 2504                   emitImplementationPreamble();
 2505                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr);
 2506                   emit("arraySetterHelper(" + safeVarName + "Array" + ", " + identifier + ");" );
 2507                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr);
 2508                   emitImplementationPostamble();
 2509                   endBlock();
 2510   
 2511                   printJavaDoc("Sets (as xml) ith " + propdesc);
 2512                   emit("public void xset" + arrayName + "(int i, " + xtype + " " + safeVarName + ")");
 2513                   startBlock();
 2514                   emitImplementationPreamble();
 2515                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
 2516                   emitGetTarget(setIdentifier, identifier, isAttr, "i", THROW_EXCEPTION, xtype);
 2517                   emit("target.set(" + safeVarName + ");");
 2518                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
 2519                   emitImplementationPostamble();
 2520                   endBlock();
 2521               }
 2522   
 2523               if (nillable)
 2524               {
 2525                   printJavaDoc("Nils the ith " + propdesc);
 2526                   emit("public void setNil" + arrayName + "(int i)");
 2527                   startBlock();
 2528                   emitImplementationPreamble();
 2529                   emitPre(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
 2530                   emitGetTarget(setIdentifier, identifier, isAttr, "i", THROW_EXCEPTION, xtype);
 2531                   emit("target.setNil();");
 2532                   emitPost(sType, PrePostExtension.OPERATION_SET, identifier, isAttr, "i");
 2533                   emitImplementationPostamble();
 2534                   endBlock();
 2535               }
 2536   
 2537               if (!xmltype)
 2538               {
 2539                   printJavaDoc("Inserts the value as the ith " + propdesc);
 2540                   emit("public void insert" + propertyName + "(int i, " + type + " " + safeVarName + ")");
 2541                   startBlock();
 2542                   emitImplementationPreamble();
 2543                   emitPre(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr, "i");
 2544                   emit(jtargetType + " target = ");
 2545                   indent();
 2546                   if (!isSubstGroup)
 2547                       emit("(" + jtargetType + ")get_store().insert_element_user(" + identifier + ", i);");
 2548                   else // This is a subst group case
 2549                       emit ("(" + jtargetType +")get_store().insert_element_user(" + setIdentifier + ", " +
 2550                               identifier + ", i);");
 2551                   outdent();
 2552                   printJSetValue(javaType, safeVarName, (SchemaTypeImpl)prop.getType());
 2553                   emitPost(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr, "i");
 2554                   emitImplementationPostamble();
 2555                   endBlock();
 2556   
 2557                   printJavaDoc("Appends the value as the last " + propdesc);
 2558                   emit("public void add" + propertyName + "(" + type + " " + safeVarName + ")");
 2559                   startBlock();
 2560                   emitImplementationPreamble();
 2561                   emitDeclareTarget(true, jtargetType);
 2562     	            emitPre(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr);
 2563                   emitAddTarget(identifier, isAttr, true, jtargetType);
 2564                   printJSetValue(javaType, safeVarName, (SchemaTypeImpl)prop.getType());
 2565                   emitPost(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr);
 2566                   emitImplementationPostamble();
 2567                   endBlock();
 2568               }
 2569   
 2570               printJavaDoc("Inserts and returns a new empty value (as xml) as the ith " + propdesc);
 2571               emit("public " + xtype + " insertNew" + propertyName + "(int i)");
 2572               startBlock();
 2573               emitImplementationPreamble();
 2574               emitDeclareTarget(true, xtype);
 2575               emitPre(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr, "i");
 2576               if (!isSubstGroup)
 2577               {
 2578                   emit("target = (" + xtype + ")get_store().insert_element_user(" + identifier + ", i);");
 2579               }
 2580               else // This is a subst group case
 2581               {
 2582                   emit("target = (" + xtype + ")get_store().insert_element_user(" +
 2583                           setIdentifier + ", " + identifier + ", i);");
 2584               }
 2585               emitPost(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr, "i");
 2586               emit("return target;");
 2587               emitImplementationPostamble();
 2588               endBlock();
 2589   
 2590               printJavaDoc("Appends and returns a new empty value (as xml) as the last " + propdesc);
 2591               emit("public " + xtype + " addNew" + propertyName + "()");
 2592               startBlock();
 2593               emitImplementationPreamble();
 2594               emitDeclareTarget(true, xtype);
 2595               emitPre(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr);
 2596               emitAddTarget(identifier, isAttr, true, xtype);
 2597               emitPost(sType, PrePostExtension.OPERATION_INSERT, identifier, isAttr);
 2598               emit("return target;");
 2599               emitImplementationPostamble();
 2600               endBlock();
 2601   
 2602               printJavaDoc("Removes the ith " + propdesc);
 2603               emit("public void remove" + propertyName + "(int i)");
 2604               startBlock();
 2605               emitImplementationPreamble();
 2606               emitPre(sType, PrePostExtension.OPERATION_REMOVE, identifier, isAttr, "i");
 2607               emit("get_store().remove_element(" + setIdentifier + ", i);");
 2608               emitPost(sType, PrePostExtension.OPERATION_REMOVE, identifier, isAttr, "i");
 2609               emitImplementationPostamble();
 2610               endBlock();
 2611           }
 2612       }
 2613   
 2614       static void getTypeName(Class c, StringBuffer sb) {
 2615           int arrayCount = 0;
 2616           while (c.isArray()) {
 2617               c = c.getComponentType();
 2618               arrayCount++;
 2619           }
 2620   
 2621           sb.append(c.getName());
 2622   
 2623           for (int i = 0 ; i < arrayCount; i++)
 2624               sb.append("[]");
 2625   
 2626       }
 2627   
 2628       void printInnerTypeImpl(
 2629           SchemaType sType, SchemaTypeSystem system, boolean isInner ) throws IOException
 2630       {
 2631           String shortName = sType.getShortJavaImplName();
 2632   
 2633           printInnerTypeJavaDoc(sType);
 2634   
 2635           startClass(sType, isInner);
 2636   
 2637           printConstructor(sType, shortName);
 2638   
 2639           printExtensionImplMethods(sType);
 2640   
 2641           if (!sType.isSimpleType())
 2642           {
 2643               SchemaProperty[] properties;
 2644   
 2645               if (sType.getContentType() == SchemaType.SIMPLE_CONTENT)
 2646               {
 2647                   // simple content types impls derive directly from "holder" impls
 2648                   // in order to handle the case (for ints or string enums e.g.) where
 2649                   // there is a simple type restriction.  So property getters need to
 2650                   // be implemented "from scratch" for each derived complex type
 2651                   // Moreover, attribute or element properties can be removed via restriction,
 2652                   // but we still need to implement them because this class is supposed to
 2653                   // also implement all the interfaces
 2654                   SchemaType baseType = sType.getBaseType();
 2655                   List extraProperties = null;
 2656                   while (!baseType.isSimpleType() && !baseType.isBuiltinType())
 2657                   {
 2658                       SchemaProperty[] baseProperties = baseType.getDerivedProperties();
 2659                       for (int i = 0; i < baseProperties.length; i++)
 2660                           if (!(baseProperties[i].isAttribute() &&
 2661                                   sType.getAttributeProperty(baseProperties[i].getName()) != null))
 2662                           {
 2663                               if (extraProperties == null)
 2664                                   extraProperties = new ArrayList();
 2665                               extraProperties.add(baseProperties[i]);
 2666                           }
 2667                       baseType = baseType.getBaseType();
 2668                   }
 2669   
 2670                   properties = sType.getProperties();
 2671                   if (extraProperties != null)
 2672                   {
 2673                       for (int i = 0; i < properties.length; i++)
 2674                           extraProperties.add(properties[i]);
 2675                       properties = (SchemaProperty[]) extraProperties.
 2676                           toArray(new SchemaProperty[extraProperties.size()]);
 2677                   }
 2678               }
 2679               else
 2680               {
 2681                   // complex content type implementations derive from base type impls
 2682                   // so derived property impls can be reused
 2683   
 2684                   properties = getDerivedProperties(sType);
 2685               }
 2686   
 2687               Map qNameMap = printStaticFields(properties);
 2688   
 2689               for (int i = 0; i < properties.length; i++)
 2690               {
 2691                   SchemaProperty prop = properties[i];
 2692   
 2693                   QName name = prop.getName();
 2694                   String xmlType = xmlTypeForProperty( prop );
 2695   
 2696                   printGetterImpls(
 2697                       shortName,
 2698                       prop,
 2699                       name,
 2700                       prop.isAttribute(),
 2701                       prop.getJavaPropertyName(),
 2702                       prop.getJavaTypeCode(),
 2703                       javaTypeForProperty(prop),
 2704                       xmlType,
 2705                       prop.hasNillable() != SchemaProperty.NEVER,
 2706                       prop.extendsJavaOption(),
 2707                       prop.extendsJavaArray(),
 2708                       prop.extendsJavaSingleton(),
 2709                       xmlTypeForPropertyIsUnion(prop),
 2710                       getIdentifier(qNameMap, name),
 2711                       getSetIdentifier(qNameMap, name)
 2712                   );
 2713   
 2714                   if (!prop.isReadOnly())
 2715                   {
 2716                       printSetterImpls(
 2717                           name,
 2718                           prop,
 2719                           prop.isAttribute(),
 2720                           prop.getJavaPropertyName(),
 2721                           prop.getJavaTypeCode(),
 2722                           javaTypeForProperty(prop),
 2723                           xmlType,
 2724                           prop.hasNillable() != SchemaProperty.NEVER,
 2725                           prop.extendsJavaOption(),
 2726                           prop.extendsJavaArray(),
 2727                           prop.extendsJavaSingleton(),
 2728                           xmlTypeForPropertyIsUnion(prop),
 2729                           getIdentifier(qNameMap, name),
 2730                           getSetIdentifier(qNameMap, name),
 2731     	                    sType
 2732                       );
 2733                   }
 2734               }
 2735           }
 2736   
 2737           printNestedTypeImpls(sType, system);
 2738   
 2739           endBlock();
 2740       }
 2741   
 2742       private SchemaProperty[] getDerivedProperties(SchemaType sType)
 2743       {
 2744           // We have to see if this is redefined, because if it is we have
 2745           // to include all properties associated to its supertypes
 2746           QName name = sType.getName();
 2747           if (name != null && name.equals(sType.getBaseType().getName()))
 2748           {
 2749               SchemaType sType2 = sType.getBaseType();
 2750               // Walk all the redefined types and record any properties
 2751               // not present in sType, because the redefined types do not
 2752               // have a generated class to represent them
 2753               SchemaProperty[] props = sType.getDerivedProperties();
 2754               Map propsByName = new LinkedHashMap();
 2755               for (int i = 0; i < props.length; i++)
 2756                   propsByName.put(props[i].getName(), props[i]);
 2757               while (sType2 != null && name.equals(sType2.getName()))
 2758               {
 2759                   props = sType2.getDerivedProperties();
 2760                   for (int i = 0; i < props.length; i++)
 2761                       if (!propsByName.containsKey(props[i].getName()))
 2762                           propsByName.put(props[i].getName(), props[i]);
 2763                   sType2 = sType2.getBaseType();
 2764               }
 2765               return (SchemaProperty[]) propsByName.values().toArray(new SchemaProperty[0]);
 2766           }
 2767           else
 2768               return sType.getDerivedProperties();
 2769       }
 2770   
 2771       private void printExtensionImplMethods(SchemaType sType) throws IOException
 2772       {
 2773           SchemaTypeImpl sImpl = getImpl(sType);
 2774           if (sImpl == null)
 2775               return;
 2776   
 2777           InterfaceExtension[] exts = sImpl.getInterfaceExtensions();
 2778           if (exts != null) for (int i = 0; i < exts.length; i++)
 2779           {
 2780               InterfaceExtension.MethodSignature[] methods = exts[i].getMethods();
 2781               if (methods != null)
 2782               {
 2783                   for (int j = 0; j < methods.length; j++)
 2784                   {
 2785                       printJavaDoc("Implementation method for interface " + exts[i].getStaticHandler());
 2786                       printInterfaceMethodDecl(methods[j]);
 2787                       startBlock();
 2788                       printInterfaceMethodImpl(exts[i].getStaticHandler(), methods[j]);
 2789                       endBlock();
 2790                   }
 2791               }
 2792           }
 2793       }
 2794   
 2795       void printInterfaceMethodDecl(InterfaceExtension.MethodSignature method) throws IOException
 2796       {
 2797           StringBuffer decl = new StringBuffer(60);
 2798   
 2799           decl.append("public ").append(method.getReturnType());
 2800           decl.append(" ").append(method.getName()).append("(");
 2801   
 2802           String[] paramTypes = method.getParameterTypes();
 2803           for (int i = 0; i < paramTypes.length; i++)
 2804           {
 2805               if (i != 0)
 2806                   decl.append(", ");
 2807               decl.append(paramTypes[i]).append(" p").append(i);
 2808           }
 2809   
 2810           decl.append(")");
 2811   
 2812           String[] exceptions = method.getExceptionTypes();
 2813           for (int i = 0; i < exceptions.length; i++)
 2814               decl.append((i == 0 ? " throws " : ", ") + exceptions[i]);
 2815   
 2816           emit(decl.toString());
 2817       }
 2818   
 2819       void printInterfaceMethodImpl(String handler, InterfaceExtension.MethodSignature method) throws IOException
 2820       {
 2821           StringBuffer impl = new StringBuffer(60);
 2822   
 2823           if (!method.getReturnType().equals("void"))
 2824               impl.append("return ");
 2825   
 2826           impl.append(handler).append(".").append(method.getName()).append("(this");
 2827   
 2828           String[] params = method.getParameterTypes();
 2829           for (int i = 0; i < params.length; i++)
 2830               impl.append(", p" + i);
 2831   
 2832           impl.append(");");
 2833   
 2834           emit(impl.toString());
 2835       }
 2836   
 2837       void printNestedTypeImpls(SchemaType sType, SchemaTypeSystem system) throws IOException
 2838       {
 2839           boolean redefinition = sType.getName() != null &&
 2840               sType.getName().equals(sType.getBaseType().getName());
 2841           while (sType != null)
 2842           {
 2843               SchemaType[] anonTypes = sType.getAnonymousTypes();
 2844               for (int i = 0; i < anonTypes.length; i++)
 2845               {
 2846                   if (anonTypes[i].isSkippedAnonymousType())
 2847                       printNestedTypeImpls(anonTypes[i], system);
 2848                   else
 2849                       printInnerTypeImpl(anonTypes[i], system, true);
 2850               }
 2851               // For redefinition by extension, go ahead and print the anonymous
 2852               // types in the base
 2853               if (!redefinition ||
 2854                   (sType.getDerivationType() != SchemaType.DT_EXTENSION && !sType.isSimpleType()))
 2855                   break;
 2856               sType = sType.getBaseType();
 2857           }
 2858       }
 2859   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » schema » [javadoc | source]