1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 package org.apache.openoffice.ooxml.schema.model.complex;
23 
24 import java.util.Vector;
25 
26 import org.apache.openoffice.ooxml.schema.model.base.INode;
27 import org.apache.openoffice.ooxml.schema.model.base.INodeReference;
28 import org.apache.openoffice.ooxml.schema.model.base.INodeVisitor;
29 import org.apache.openoffice.ooxml.schema.model.base.Location;
30 import org.apache.openoffice.ooxml.schema.model.base.Node;
31 import org.apache.openoffice.ooxml.schema.model.base.NodeType;
32 import org.apache.openoffice.ooxml.schema.model.base.QualifiedName;
33 import org.apache.openoffice.ooxml.schema.model.schema.SchemaBase;
34 
35 /** Representation of the 'extension' XML schema element.
36  *  It extends a complex base type.
37  */
38 public class Extension
39     extends Node
40     implements INodeReference
41 {
Extension( final Node aParent, final QualifiedName aBaseTypeName, final Location aLocation)42     public Extension (
43         final Node aParent,
44         final QualifiedName aBaseTypeName,
45         final Location aLocation)
46     {
47         super(aParent, null, aLocation);
48         maBaseTypeName = aBaseTypeName;
49     }
50 
51 
52 
53 
54     @Override
AcceptVisitor(final INodeVisitor aVisitor)55     public void AcceptVisitor (final INodeVisitor aVisitor)
56     {
57         aVisitor.Visit(this);
58     }
59 
60 
61 
62 
63     @Override
GetNodeType()64     public NodeType GetNodeType()
65     {
66         return NodeType.Extension;
67     }
68 
69 
70 
71 
GetBaseTypeName()72     public QualifiedName GetBaseTypeName()
73     {
74         return maBaseTypeName;
75     }
76 
77 
78 
79 
GetBaseType(final SchemaBase aSchema)80     public INode GetBaseType (final SchemaBase aSchema)
81     {
82         return aSchema.GetTypeForName(maBaseTypeName);
83     }
84 
85 
86 
87 
88     @Override
GetReferencedNode(final SchemaBase aSchema)89     public INode GetReferencedNode (final SchemaBase aSchema)
90     {
91         return GetBaseType(aSchema);
92     }
93 
94 
95 
96 
GetTypeNodes(final SchemaBase aSchemaBase)97     public Vector<INode> GetTypeNodes (final SchemaBase aSchemaBase)
98     {
99         final Vector<INode> aNodes = new Vector<>();
100 
101         AddNodes(aSchemaBase.GetTypeForName(maBaseTypeName), aNodes, aSchemaBase);
102         for (final INode aChild : GetChildren())
103             AddNodes(aChild, aNodes, aSchemaBase);
104 
105         return aNodes;
106     }
107 
108 
109 
110 
111     @Override
toString()112     public String toString ()
113     {
114     	return "extension of base type "+maBaseTypeName.GetDisplayName();
115     }
116 
117 
118 
119 
AddNodes( final INode aParent, final Vector<INode> aNodes, final SchemaBase aSchemaBase)120     private void AddNodes (
121         final INode aParent,
122         final Vector<INode> aNodes,
123         final SchemaBase aSchemaBase)
124     {
125         INode aNode = aParent;
126         while (true)
127         {
128             switch (aNode.GetNodeType())
129             {
130                 case Extension:
131                     aNode = ((Extension)aNode).GetBaseType(aSchemaBase);
132                     break;
133 
134                 case ComplexContent:
135                 case SimpleContent:
136                 case ComplexType:
137                     switch(aNode.GetChildCount())
138                     {
139                         case 0:
140                             return;
141                         case 1:
142                             aNode = aNode.GetOnlyChild();
143                             break;
144                         default:
145                             throw new RuntimeException();
146                     }
147                     break;
148 
149                 default:
150                     aNodes.add(aNode);
151                     return;
152             }
153         }
154     }
155 
156 
157 
158 
159     public QualifiedName maBaseTypeName;
160 }
161