001package com.hfg.xml.xsd;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import com.hfg.util.collection.CollectionUtil;
007import com.hfg.util.StringUtil;
008import com.hfg.xml.XMLNamespace;
009import com.hfg.xml.XMLNode;
010
011//------------------------------------------------------------------------------
012/**
013 Representation of a complexType element in an XML Schema Definition (XSD) specification.
014 <div>
015  @author J. Alex Taylor, hairyfatguy.com
016 </div>
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039public class XsdComplexType extends XsdType
040{
041   private List<XsdAttribute> mAttributes;
042   private List<XsdContent>   mContent;
043   private String             mNameOfBaseType;
044   private XsdComplexType     mBaseType;
045
046   //---------------------------------------------------------------------------
047   public XsdComplexType(String inName)
048   {
049      super(inName);
050   }
051
052   //---------------------------------------------------------------------------
053   public XsdComplexType(XMLNode inXMLTag, XMLNamespace inNamespace)
054   {
055      super(inXMLTag);
056
057      setNamespace(inNamespace);
058
059      for (XMLNode subtag : inXMLTag.getXMLNodeSubtags())
060      {
061         if (subtag.getTagName().equals(XsdXML.ATTRIBUTE.getLocalName()))
062         {
063            // TODO
064         }
065         else if (subtag.getTagName().equals(XsdXML.CHOICE.getLocalName()))
066         {
067            addContent(new XsdChoice(subtag, inNamespace));
068         }
069         else if (subtag.getTagName().equals(XsdXML.SEQUENCE.getLocalName()))
070         {
071            addContent(new XsdSequence(subtag, getNamespace()));
072         }
073         else if (subtag.getTagName().equals(XsdXML.GROUP.getLocalName()))
074         {
075            addContent(new XsdGroup(subtag, inNamespace));
076         }
077         else if (subtag.getTagName().equals(XsdXML.COMPLEX_CONTENT.getLocalName()))
078         {
079            List<XMLNode> complexContentSubtags = subtag.getXMLNodeSubtags();
080            if (CollectionUtil.hasValues(complexContentSubtags)
081                && 1 == complexContentSubtags.size()
082                && complexContentSubtags.get(0).getTagName().equals(XsdXML.EXTENSION.getLocalName()))
083            {
084               setNameOfBaseType(complexContentSubtags.get(0).getAttributeValue(XsdXML.BASE_ATT.getLocalName()));
085
086               List<XMLNode> extensionSubtags = complexContentSubtags.get(0).getXMLNodeSubtags();
087               for (XMLNode extensionSubtag : extensionSubtags)
088               {
089                  if (extensionSubtag.getTagName().equals(XsdXML.ATTRIBUTE.getLocalName()))
090                  {
091                     // TODO
092                  }
093                  else if (extensionSubtag.getTagName().equals(XsdXML.CHOICE.getLocalName()))
094                  {
095                     addContent(new XsdChoice(extensionSubtag, inNamespace));
096                  }
097                  else if (extensionSubtag.getTagName().equals(XsdXML.SEQUENCE.getLocalName()))
098                  {
099                     addContent(new XsdSequence(extensionSubtag, inNamespace));
100                  }
101                  else if (extensionSubtag.getTagName().equals(XsdXML.GROUP.getLocalName()))
102                  {
103                     addContent(new XsdGroup(extensionSubtag, inNamespace));
104                  }
105               }
106            }
107         }
108      }
109   }
110
111   //---------------------------------------------------------------------------
112   public XsdComplexType setNameOfBaseType(String inValue)
113   {
114      mNameOfBaseType = inValue;
115      return this;
116   }
117
118   //---------------------------------------------------------------------------
119   public String getNameOfBaseType()
120   {
121      if (StringUtil.isSet(mNameOfBaseType)
122          && ! mNameOfBaseType.contains(":")
123          && getNamespace() != null
124          && getNamespace().getPrefix() != null)
125      {
126         mNameOfBaseType = getNamespace().getPrefix() + ":" + mNameOfBaseType;
127      }
128
129      return mNameOfBaseType;
130   }
131
132   //---------------------------------------------------------------------------
133   public XsdComplexType setBaseType(XsdComplexType inValue)
134   {
135      mBaseType = inValue;
136      return this;
137   }
138
139   //---------------------------------------------------------------------------
140   public XsdComplexType getBaseType()
141   {
142      return mBaseType;
143   }
144
145   //---------------------------------------------------------------------------
146   public List<XsdContent> getContent()
147   {
148      return mContent;
149   }
150
151   //---------------------------------------------------------------------------
152   public XsdComplexType addContent(XsdContent inValue)
153   {
154      if (null == mContent)
155      {
156         mContent = new ArrayList<>(20);
157      }
158
159      mContent.add(inValue);
160
161      return this;
162   }
163}