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.simple;
23 
24 import java.util.Map;
25 import java.util.Vector;
26 
27 import org.apache.openoffice.ooxml.schema.misc.Log;
28 import org.apache.openoffice.ooxml.schema.model.simple.Restriction;
29 
30 public class UnionNode
31     implements ISimpleTypeNode
32 {
UnionNode()33     UnionNode ()
34     {
35         maTypes = new Vector<>();
36         mbIsList = false;
37     }
38 
39 
40 
41 
42     @Override
ApplyRestriction( final Restriction aNode, final Map<String,Integer> aValueToIdMap)43     public void ApplyRestriction (
44         final Restriction aNode,
45         final Map<String,Integer> aValueToIdMap)
46     {
47         throw new RuntimeException("can not handle restriction on union");
48     }
49 
50 
51 
52 
53 
AddNode(final ISimpleTypeNode aType)54     public void AddNode (final ISimpleTypeNode aType)
55     {
56         if (aType instanceof UnionNode)
57         {
58             // Integrate union of child into this union.
59             final UnionNode aChildUnion = (UnionNode)aType;
60             for (final ISimpleTypeNode aChildChild : aChildUnion.maTypes)
61                 maTypes.add(aChildChild);
62         }
63         else if (aType instanceof StringNode)
64         {
65             // Is there already a string child?
66             for (int nIndex=0; nIndex<maTypes.size(); ++nIndex)
67             {
68                 final ISimpleTypeNode aChild = maTypes.get(nIndex);
69 
70                 if (aChild instanceof StringNode)
71                 {
72                     // Yes.  Can it be joined with the new child?
73                     final ISimpleTypeNode aNewChild = ((StringNode)aChild).Join((StringNode)aType);
74                     if (aNewChild != null)
75                     {
76                         // Yes. Replace the child with the joined child.
77                         maTypes.set(nIndex, aNewChild);
78                         return;
79                     }
80                 }
81             }
82             // When we reach this point then there was no join possible.
83             // Just add the new type.
84             maTypes.add(aType);
85 
86         }
87         else
88             maTypes.add(aType);
89     }
90 
91 
92 
93 
94     @Override
Print(final Log aLog)95     public void Print (final Log aLog)
96     {
97         aLog.printf("union of %d sub types\n", maTypes.size());
98         aLog.StartBlock();
99         for (final ISimpleTypeNode aType : maTypes)
100             aType.Print(aLog);
101         aLog.EndBlock();
102     }
103 
104 
105 
106 
107     @Override
IsList()108     public boolean IsList ()
109     {
110         return mbIsList;
111     }
112 
113 
114 
115 
116     @Override
SetIsList()117     public void SetIsList ()
118     {
119         mbIsList = true;
120     }
121 
122 
123 
124 
125     @Override
toString()126     public String toString ()
127     {
128         return "union";
129     }
130 
131 
GetChildren()132     ISimpleTypeNode[] GetChildren ()
133     {
134         return maTypes.toArray(new ISimpleTypeNode[maTypes.size()]);
135     }
136 
137 
138 
139 
140     @Override
AcceptVisitor(final ISimpleTypeNodeVisitor aVisitor)141     public void AcceptVisitor (final ISimpleTypeNodeVisitor aVisitor)
142     {
143         aVisitor.Visit(this);
144     }
145 
146 
147 
148 
149     private final Vector<ISimpleTypeNode> maTypes;
150     private boolean mbIsList;
151 }
152