Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » inst2xsd » util » [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   package org.apache.xmlbeans.impl.inst2xsd.util;
   16   
   17   import org.apache.xmlbeans.XmlQName;
   18   import org.apache.xmlbeans.XmlCursor;
   19   import org.apache.xmlbeans.impl.common.PrefixResolver;
   20   import org.apache.xmlbeans.impl.values.XmlQNameImpl;
   21   
   22   import javax.xml.namespace.QName;
   23   import java.util.List;
   24   import java.util.ArrayList;
   25   
   26   /**
   27    * @author Cezar Andrei (cezar.andrei at bea.com) Date: Jul 16, 2004
   28    */
   29   public class Type
   30   {
   31       private QName _name;
   32   
   33       private int _kind = SIMPLE_TYPE_SIMPLE_CONTENT;
   34       // _kind value space
   35       public static final int SIMPLE_TYPE_SIMPLE_CONTENT   = 1; // ie no atts, no elems, just text
   36       public static final int COMPLEX_TYPE_SIMPLE_CONTENT  = 2; // ie atts*, no elems, text*   - simple type extension
   37       public static final int COMPLEX_TYPE_COMPLEX_CONTENT = 3; // ie atts*, elems, no text
   38       public static final int COMPLEX_TYPE_MIXED_CONTENT   = 4; // ie atts*, elems, text
   39       public static final int COMPLEX_TYPE_EMPTY_CONTENT   = 5; // no elems, no text, just atts
   40   
   41       private int _topParticleForComplexOrMixedContent = PARTICLE_SEQUENCE;
   42       // _topParticleForComplexOrMixedContent
   43       public static final int PARTICLE_SEQUENCE         = 1;
   44       public static final int PARTICLE_CHOICE_UNBOUNDED = 2;
   45   
   46       private List _elements;  // size>0 COMPLEX
   47       private List _attributes; // size>0 COMPLEX
   48   
   49       private Type _extensionType;
   50       private boolean _isGlobal = false;
   51   
   52       private List _enumerationValues;
   53       private boolean _acceptsEnumerationValue = true;
   54       private List _enumerationQNames;                    // This is a special case where the lexical representation
   55                                                           // is not enough for a value, the QNames need to be remembered
   56                                                           // in case the _extensionType is QName
   57   
   58       protected Type()
   59       {}
   60   
   61       public static Type createNamedType(QName name, int contentType)
   62       {
   63           assert name!=null;
   64           Type type = new Type();
   65           type.setName(name);
   66           type.setContentType(contentType);
   67           return type;
   68       }
   69   
   70       public static Type createUnnamedType(int contentType)
   71       {
   72           assert contentType==SIMPLE_TYPE_SIMPLE_CONTENT ||
   73               contentType==COMPLEX_TYPE_SIMPLE_CONTENT ||
   74               contentType==COMPLEX_TYPE_COMPLEX_CONTENT ||
   75               contentType==COMPLEX_TYPE_MIXED_CONTENT ||
   76               contentType==COMPLEX_TYPE_EMPTY_CONTENT: "Unknown contentType: " + contentType;
   77           Type type = new Type();
   78           type.setContentType(contentType);
   79           return type;
   80       }
   81   
   82   
   83       public QName getName()
   84       {
   85           return _name;
   86       }
   87   
   88       public void setName(QName name)
   89       {
   90           this._name = name;
   91       }
   92   
   93       /**
   94        * @return
   95        *   SIMPLE_TYPE_SIMPLE_CONTENT   // ie no atts, no elems, just text
   96        *   COMPLEX_TYPE_SIMPLE_CONTENT  // ie atts*, no elems, text*   - simple type extension
   97        *   COMPLEX_TYPE_COMPLEX_CONTENT // ie atts*, elems, no text
   98        *   COMPLEX_TYPE_MIXED_CONTENT   // ie atts*, elems, text
   99        *   COMPLEX_TYPE_EMPTY_CONTENT   // no elems, no text, just atts
  100        */
  101       public int getContentType()
  102       {
  103           return _kind;
  104       }
  105   
  106       /**
  107        * @param kind 4 kinds:
  108        *   SIMPLE_TYPE_SIMPLE_CONTENT   // ie no atts, no elems, just text
  109        *   COMPLEX_TYPE_SIMPLE_CONTENT  // ie atts*, no elems, text*   - simple type extension
  110        *   COMPLEX_TYPE_COMPLEX_CONTENT // ie atts*, elems, no text
  111        *   COMPLEX_TYPE_MIXED_CONTENT   // ie atts*, elems, text
  112        *   COMPLEX_TYPE_EMPTY_CONTENT   // no elems, no text, just atts
  113        */
  114       public void setContentType(int kind)
  115       {
  116           this._kind = kind;
  117       }
  118   
  119       public List getElements()
  120       {
  121           ensureElements();
  122           return _elements;
  123       }
  124   
  125       public void addElement(Element element)
  126       {
  127           ensureElements();
  128           _elements.add(element);
  129       }
  130   
  131       public void setElements(List elements)
  132       {
  133           ensureElements();
  134           _elements.clear();
  135           _elements.addAll(elements);
  136       }
  137   
  138       private void ensureElements()
  139       {
  140           if (_elements==null)
  141               _elements = new ArrayList();
  142       }
  143   
  144       public List getAttributes()
  145       {
  146           ensureAttributes();
  147           return _attributes;
  148       }
  149   
  150       public void addAttribute(Attribute attribute)
  151       {
  152           ensureAttributes();
  153           _attributes.add(attribute);
  154       }
  155   
  156       public Attribute getAttribute(QName name)
  157       {
  158           for (int i = 0; i < _attributes.size(); i++)
  159           {
  160               Attribute attribute = (Attribute) _attributes.get(i);
  161               if (attribute.getName().equals(name))
  162                   return attribute;
  163           }
  164           return null;
  165       }
  166   
  167       private void ensureAttributes()
  168       {
  169           if (_attributes==null)
  170               _attributes = new ArrayList();
  171       }
  172   
  173       public boolean isComplexType()
  174       {
  175           return (_kind==COMPLEX_TYPE_COMPLEX_CONTENT ||
  176               _kind==COMPLEX_TYPE_MIXED_CONTENT||
  177               _kind==COMPLEX_TYPE_SIMPLE_CONTENT);
  178       }
  179   
  180       public boolean hasSimpleContent()
  181       {
  182           return (_kind==SIMPLE_TYPE_SIMPLE_CONTENT ||
  183               _kind==COMPLEX_TYPE_SIMPLE_CONTENT);
  184       }
  185   
  186       /**
  187        * @return PARTICLE_SEQUENCE or PARTICLE_CHOICE_UNBOUNDED
  188        */
  189       public int getTopParticleForComplexOrMixedContent()
  190       {
  191           return _topParticleForComplexOrMixedContent;
  192       }
  193   
  194       /**
  195        * @param topParticleForComplexOrMixedContent  PARTICLE_SEQUENCE or PARTICLE_CHOICE_UNBOUNDED
  196        */
  197       public void setTopParticleForComplexOrMixedContent(int topParticleForComplexOrMixedContent)
  198       {
  199           this._topParticleForComplexOrMixedContent = topParticleForComplexOrMixedContent;
  200       }
  201   
  202       public boolean isGlobal()
  203       {
  204           return _isGlobal;
  205       }
  206   
  207       public void setGlobal(boolean isGlobal)
  208       {
  209           assert isGlobal && getName()!=null;
  210           _isGlobal = isGlobal;
  211       }
  212   
  213       public Type getExtensionType()
  214       {
  215           return _extensionType;
  216       }
  217   
  218       public void setExtensionType(Type extendedType)
  219       {
  220           assert _kind == COMPLEX_TYPE_SIMPLE_CONTENT : "Extension used only for type which are COMPLEX_TYPE_SIMPLE_CONTENT";
  221           assert extendedType!=null && extendedType.getName()!=null : "Extended type must be a named type.";
  222           _extensionType = extendedType;
  223       }
  224   
  225       public List getEnumerationValues()
  226       {
  227           ensureEnumerationValues();
  228           return _enumerationValues;
  229       }
  230   
  231       public List getEnumerationQNames()
  232       {
  233           ensureEnumerationValues();
  234           return _enumerationQNames;
  235       }
  236   
  237       public void addEnumerationValue(String enumerationValue, final XmlCursor xc)
  238       {
  239           assert _kind==SIMPLE_TYPE_SIMPLE_CONTENT || _kind==COMPLEX_TYPE_SIMPLE_CONTENT : "Enumerations possible only on simple content";
  240           ensureEnumerationValues();
  241           if (_acceptsEnumerationValue && !_enumerationValues.contains(enumerationValue))
  242           {
  243               _enumerationValues.add(enumerationValue);
  244               if (_name.equals(XmlQName.type.getName()))
  245               {
  246                   // check for QName
  247                   PrefixResolver prefixResolver = new PrefixResolver()
  248                   {
  249                       public String getNamespaceForPrefix(String prefix)
  250                       {  return xc.namespaceForPrefix(prefix); }
  251                   };
  252   
  253                   QName qname = XmlQNameImpl.validateLexical(enumerationValue, null, prefixResolver);
  254   
  255                   assert qname!=null : "The check for QName should allready have happened.";
  256                   _enumerationQNames.add(qname);
  257               }
  258           }
  259       }
  260   
  261       private void ensureEnumerationValues()
  262       {
  263           if (_enumerationValues==null)
  264           {
  265               _enumerationValues = new ArrayList();
  266               _enumerationQNames = new ArrayList();
  267           }
  268       }
  269   
  270       public boolean isEnumeration()
  271       {
  272           return _acceptsEnumerationValue && _enumerationValues!=null && _enumerationValues.size()>1;
  273       }
  274   
  275       public boolean isQNameEnumeration()
  276       {
  277           return isEnumeration() && _name.equals(XmlQName.type.getName()) && _enumerationQNames!=null && _enumerationQNames.size()>1;
  278       }
  279   
  280       public void closeEnumeration()
  281       {
  282           _acceptsEnumerationValue=false;
  283       }
  284   
  285       public String toString()
  286       {
  287           return "Type{" +
  288               "_name = " + _name +
  289               ", _extensionType = " + _extensionType +
  290               ", _kind = " + _kind +
  291               ", _elements = " + _elements +
  292               ", _attributes = " + _attributes +
  293               "}";
  294       }
  295   
  296       public void addAllEnumerationsFrom(Type from)
  297       {
  298           assert _kind==SIMPLE_TYPE_SIMPLE_CONTENT || _kind==COMPLEX_TYPE_SIMPLE_CONTENT : "Enumerations possible only on simple content";
  299           ensureEnumerationValues();
  300   
  301           if (_name.equals(XmlQName.type.getName()) && from._name.equals(XmlQName.type.getName()))
  302           {
  303               for (int i = 0; i < from.getEnumerationValues().size(); i++)
  304               {
  305                   String enumValue = (String) from.getEnumerationValues().get(i);
  306                   QName enumQNameValue = (QName) from.getEnumerationQNames().get(i);
  307                   if (_acceptsEnumerationValue && !_enumerationQNames.contains(enumQNameValue))
  308                   {
  309                           _enumerationValues.add(enumValue);
  310                           _enumerationQNames.add(enumQNameValue);
  311                   }
  312               }
  313           }
  314           else
  315           {
  316               for (int i = 0; i < from.getEnumerationValues().size(); i++)
  317               {
  318                   String enumValue = (String) from.getEnumerationValues().get(i);
  319                   if (_acceptsEnumerationValue && !_enumerationValues.contains(enumValue))
  320                   {
  321                       _enumerationValues.add(enumValue);
  322                   }
  323               }
  324           }
  325       }
  326   }

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