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.optimize;
23 
24 import org.apache.openoffice.ooxml.schema.model.attribute.Attribute;
25 import org.apache.openoffice.ooxml.schema.model.attribute.AttributeGroupReference;
26 import org.apache.openoffice.ooxml.schema.model.attribute.AttributeReference;
27 import org.apache.openoffice.ooxml.schema.model.base.INode;
28 import org.apache.openoffice.ooxml.schema.model.base.NodeVisitorAdapter;
29 import org.apache.openoffice.ooxml.schema.model.complex.Element;
30 import org.apache.openoffice.ooxml.schema.model.complex.Extension;
31 import org.apache.openoffice.ooxml.schema.model.complex.GroupReference;
32 import org.apache.openoffice.ooxml.schema.model.schema.SchemaBase;
33 import org.apache.openoffice.ooxml.schema.model.simple.List;
34 import org.apache.openoffice.ooxml.schema.model.simple.Restriction;
35 import org.apache.openoffice.ooxml.schema.model.simple.SimpleTypeReference;
36 
37 /** A visitor that is called for all nodes of a complex or simple type to mark
38  *  the referenced types as being used.
39  */
40 public class RequestVisitor
41     extends NodeVisitorAdapter
42 {
RequestVisitor( final SchemaBase aSourceSchema, final SchemaOptimizer aOptimizer)43     RequestVisitor (
44         final SchemaBase aSourceSchema,
45         final SchemaOptimizer aOptimizer)
46     {
47         maSourceSchemaBase = aSourceSchema;
48         maSchemaOptimizer = aOptimizer;
49     }
50 
51 
Visit(final Attribute aAttribute)52     @Override public void Visit (final Attribute aAttribute)
53     {
54         maSchemaOptimizer.RequestType(aAttribute.GetTypeName());
55     }
56 
Visit(final AttributeReference aAttributeReference)57     @Override public void Visit (final AttributeReference aAttributeReference)
58     {
59         maSchemaOptimizer.RequestType(aAttributeReference.GetReferencedName());
60     }
61 
Visit(final AttributeGroupReference aAttributeGroupReference)62     @Override public void Visit (final AttributeGroupReference aAttributeGroupReference)
63     {
64         maSchemaOptimizer.RequestType(aAttributeGroupReference.GetReferencedName());
65     }
66 
Visit(final Element aElement)67     @Override public void Visit (final Element aElement)
68     {
69         maSchemaOptimizer.RequestType(aElement.GetTypeName());
70     }
71 
Visit(final Extension aExtension)72     @Override public void Visit (final Extension aExtension)
73     {
74         maSchemaOptimizer.RequestType(aExtension.GetBaseTypeName());
75     }
76 
Visit(final GroupReference aReference)77     @Override public void Visit (final GroupReference aReference)
78     {
79         maSchemaOptimizer.RequestType(aReference.GetReferencedGroup(maSourceSchemaBase));
80     }
81 
Visit(final List aList)82     @Override public void Visit (final List aList)
83     {
84         maSchemaOptimizer.RequestType(aList.GetItemType());
85     }
86 
Visit(final Restriction aRestriction)87     @Override public void Visit (final Restriction aRestriction)
88     {
89         maSchemaOptimizer.RequestType(aRestriction.GetBaseType());
90     }
91 
Visit(final SimpleTypeReference aReference)92     @Override public void Visit (final SimpleTypeReference aReference)
93     {
94         maSchemaOptimizer.RequestType(aReference.GetReferencedSimpleType(maSourceSchemaBase));
95     }
96 
Default(final INode aNode)97     @Override public void Default (final INode aNode)
98     {
99         switch (aNode.GetNodeType())
100         {
101             case All:
102             case Any:
103             case AttributeGroup:
104             case BuiltIn:
105             case Choice:
106             case ComplexContent:
107             case ComplexType:
108             case ElementReference:
109             case Group:
110             case List:
111             case OccurrenceIndicator:
112             case Sequence:
113             case SimpleContent:
114             case SimpleType:
115             case Union:
116                 break;
117 
118             default:
119                 throw new RuntimeException(
120                     String.format("don't know how to request %s which was defined at %s",
121                         aNode.toString(),
122                         aNode.GetLocation()));
123         }
124     }
125 
126     private final SchemaBase maSourceSchemaBase;
127     private final SchemaOptimizer maSchemaOptimizer;
128 }
129