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.AttributeGroupReference;
25 import org.apache.openoffice.ooxml.schema.model.attribute.AttributeReference;
26 import org.apache.openoffice.ooxml.schema.model.base.INode;
27 import org.apache.openoffice.ooxml.schema.model.base.NodeVisitorAdapter;
28 import org.apache.openoffice.ooxml.schema.model.complex.ComplexType;
29 import org.apache.openoffice.ooxml.schema.model.complex.GroupReference;
30 import org.apache.openoffice.ooxml.schema.model.schema.SchemaBase;
31 import org.apache.openoffice.ooxml.schema.model.simple.SimpleType;
32 
33 /** This visitor is called to copy the used types from the source to the target
34  *  schema base.
35  */
36 public class CopyVisitor
37     extends NodeVisitorAdapter
38 {
CopyVisitor( final SchemaBase aSourceSchemaBase, final SchemaBase aTargetSchemaBase)39     CopyVisitor (
40         final SchemaBase aSourceSchemaBase,
41         final SchemaBase aTargetSchemaBase)
42     {
43         maSourceSchemaBase = aSourceSchemaBase;
44         maTargetSchemaBase = aTargetSchemaBase;
45     }
46 
47 
48 
49 
Visit(final ComplexType aComplexType)50     @Override public void Visit (final ComplexType aComplexType)
51     {
52         maTargetSchemaBase.ComplexTypes.Add(aComplexType);
53     }
54 
Visit(final GroupReference aGroupReference)55     @Override public void Visit (final GroupReference aGroupReference)
56     {
57         maTargetSchemaBase.Groups.Add(
58             aGroupReference.GetReferencedGroup(maSourceSchemaBase));
59     }
60 
Visit(final SimpleType aSimpleType)61     @Override public void Visit (final SimpleType aSimpleType)
62     {
63         maTargetSchemaBase.SimpleTypes.Add(aSimpleType);
64     }
65 
Visit(final AttributeReference aAttributeReference)66     @Override public void Visit (final AttributeReference aAttributeReference)
67     {
68         maTargetSchemaBase.Attributes.Add(
69             aAttributeReference.GetReferencedAttribute(maSourceSchemaBase));
70     }
71 
Visit(final AttributeGroupReference aAttributeGroupReference)72     @Override public void Visit (final AttributeGroupReference aAttributeGroupReference)
73     {
74         maTargetSchemaBase.AttributeGroups.Add(
75             aAttributeGroupReference.GetReferencedAttributeGroup(maSourceSchemaBase));
76     }
77 
Default(final INode aNode)78     @Override public void Default (final INode aNode)
79     {
80         switch(aNode.GetNodeType())
81         {
82             case All:
83             case Any:
84             case Attribute:
85             case AttributeGroup:
86             case BuiltIn:
87             case Choice:
88             case ComplexContent:
89             case Element:
90             case ElementReference:
91             case Extension:
92             case Group:
93             case List:
94             case OccurrenceIndicator:
95             case Restriction:
96             case Sequence:
97             case SimpleContent:
98             case SimpleTypeReference:
99             case Union:
100                 break;
101 
102             default:
103                 throw new RuntimeException("don't know how to copy "+aNode.toString());
104         }
105     }
106 
107 
108 
109 
110     private final SchemaBase maSourceSchemaBase;
111     private final SchemaBase maTargetSchemaBase;
112 }
113