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.schema;
23 
24 import java.util.HashMap;
25 import java.util.Map;
26 
27 import org.apache.openoffice.ooxml.schema.model.attribute.Attribute;
28 import org.apache.openoffice.ooxml.schema.model.attribute.AttributeGroup;
29 import org.apache.openoffice.ooxml.schema.model.base.Node;
30 import org.apache.openoffice.ooxml.schema.model.base.QualifiedName;
31 import org.apache.openoffice.ooxml.schema.model.complex.ComplexType;
32 import org.apache.openoffice.ooxml.schema.model.complex.Element;
33 import org.apache.openoffice.ooxml.schema.model.complex.Group;
34 import org.apache.openoffice.ooxml.schema.model.optimize.SchemaOptimizer;
35 import org.apache.openoffice.ooxml.schema.model.simple.BuiltIn;
36 import org.apache.openoffice.ooxml.schema.model.simple.SimpleType;
37 
38 
39 /** Container of elements, complex types, simple types, etc for a set of
40  *  master schema files and the included and imported secondary schema files.
41  *
42  *  See Schema objects for the set of top level elements that are unique to
43  *  each master schema file.
44  */
45 public class SchemaBase
46 {
SchemaBase()47     public SchemaBase ()
48     {
49         Namespaces = new NamespaceMap();
50         TopLevelElements = new TypeContainer<>();
51         ComplexTypes = new TypeContainer<>();
52         SimpleTypes = new TypeContainer<>();
53         Groups = new TypeContainer<>();
54         AttributeGroups = new TypeContainer<>();
55         Attributes = new TypeContainer<>();
56         AttributeValueToIdMap = new HashMap<>();
57 
58         // Initialize the list of simple types with all known built ins (
59         // these are implicitly defined).
60         for (final BuiltIn aType : BuiltIn.GetTypes())
61             SimpleTypes.Add(aType);
62     }
63 
64 
65 
66 
GetTypeForName(final QualifiedName aName)67     public Node GetTypeForName (final QualifiedName aName)
68     {
69         final String sTypeName = aName.GetDisplayName();
70 
71         if (ComplexTypes.Contains(sTypeName))
72             return ComplexTypes.Get(sTypeName);
73         else if (SimpleTypes.Contains(sTypeName))
74             return SimpleTypes.Get(sTypeName);
75         else if (Groups.Contains(sTypeName))
76             return Groups.Get(sTypeName);
77         else if (Attributes.Contains(sTypeName))
78             return Attributes.Get(sTypeName);
79         else if (AttributeGroups.Contains(sTypeName))
80             return AttributeGroups.Get(sTypeName);
81         else
82             return null;
83     }
84 
85 
86 
87 
GetSimpleTypeForName(final QualifiedName aName)88     public Node GetSimpleTypeForName (final QualifiedName aName)
89     {
90         final String sTypeName = aName.GetDisplayName();
91 
92         if (SimpleTypes.Contains(sTypeName))
93             return SimpleTypes.Get(aName.GetDisplayName());
94         else
95             return null;
96     }
97 
98 
99 
100 
101     /** Create a new schema object that contains only the used types, i.e.
102      *  types that are reachable via element transitions, starting with the
103      *  top level elements.
104      */
GetOptimizedSchema(final Iterable<Schema> aTopLevelSchemas)105     public SchemaBase GetOptimizedSchema (final Iterable<Schema> aTopLevelSchemas)
106     {
107         return new SchemaOptimizer(this).Run();
108     }
109 
110 
111 
112 
113     public final NamespaceMap Namespaces;
114     public final TypeContainer<Element> TopLevelElements;
115     public final TypeContainer<ComplexType> ComplexTypes;
116     public final TypeContainer<SimpleType> SimpleTypes;
117     public final TypeContainer<Group> Groups;
118     public final TypeContainer<AttributeGroup> AttributeGroups;
119     public final TypeContainer<Attribute> Attributes;
120     public final Map<String,Integer> AttributeValueToIdMap;
121 }
122