author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.internal.xjc.reader.relaxng; |
|
27 |
||
28 |
import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo; |
|
29 |
import com.sun.tools.internal.xjc.model.TypeUse; |
|
30 |
import com.sun.tools.internal.xjc.model.TypeUseFactory; |
|
31 |
||
32 |
import com.sun.xml.internal.rngom.digested.DAttributePattern; |
|
33 |
import com.sun.xml.internal.rngom.digested.DChoicePattern; |
|
34 |
import com.sun.xml.internal.rngom.digested.DContainerPattern; |
|
35 |
import com.sun.xml.internal.rngom.digested.DDataPattern; |
|
36 |
import com.sun.xml.internal.rngom.digested.DElementPattern; |
|
37 |
import com.sun.xml.internal.rngom.digested.DEmptyPattern; |
|
38 |
import com.sun.xml.internal.rngom.digested.DGrammarPattern; |
|
39 |
import com.sun.xml.internal.rngom.digested.DGroupPattern; |
|
40 |
import com.sun.xml.internal.rngom.digested.DInterleavePattern; |
|
41 |
import com.sun.xml.internal.rngom.digested.DListPattern; |
|
42 |
import com.sun.xml.internal.rngom.digested.DMixedPattern; |
|
43 |
import com.sun.xml.internal.rngom.digested.DNotAllowedPattern; |
|
44 |
import com.sun.xml.internal.rngom.digested.DOneOrMorePattern; |
|
45 |
import com.sun.xml.internal.rngom.digested.DOptionalPattern; |
|
46 |
import com.sun.xml.internal.rngom.digested.DPattern; |
|
47 |
import com.sun.xml.internal.rngom.digested.DPatternVisitor; |
|
48 |
import com.sun.xml.internal.rngom.digested.DRefPattern; |
|
49 |
import com.sun.xml.internal.rngom.digested.DTextPattern; |
|
50 |
import com.sun.xml.internal.rngom.digested.DValuePattern; |
|
51 |
import com.sun.xml.internal.rngom.digested.DZeroOrMorePattern; |
|
52 |
||
53 |
/** |
|
54 |
* Walks the pattern tree and binds it to a {@link TypeUse}. |
|
55 |
* |
|
56 |
* The singleton instance is kept in {@link RELAXNGCompiler}. |
|
57 |
* |
|
58 |
* TODO: I should really normalize before process. |
|
59 |
* |
|
60 |
* @author Kohsuke Kawaguchi |
|
61 |
*/ |
|
62 |
final class TypeUseBinder implements DPatternVisitor<TypeUse> { |
|
63 |
private final RELAXNGCompiler compiler; |
|
64 |
||
65 |
public TypeUseBinder(RELAXNGCompiler compiler) { |
|
66 |
this.compiler = compiler; |
|
67 |
} |
|
68 |
||
69 |
||
70 |
public TypeUse onGrammar(DGrammarPattern p) { |
|
71 |
return CBuiltinLeafInfo.STRING; |
|
72 |
} |
|
73 |
||
74 |
public TypeUse onChoice(DChoicePattern p) { |
|
75 |
// can't support unions |
|
76 |
return CBuiltinLeafInfo.STRING; |
|
77 |
} |
|
78 |
||
79 |
public TypeUse onData(DDataPattern p) { |
|
80 |
return onDataType(p.getDatatypeLibrary(), p.getType()); |
|
81 |
} |
|
82 |
||
83 |
public TypeUse onValue(DValuePattern p) { |
|
84 |
return onDataType(p.getDatatypeLibrary(),p.getType()); |
|
85 |
} |
|
86 |
||
87 |
private TypeUse onDataType(String datatypeLibrary, String type) { |
|
88 |
DatatypeLib lib = compiler.datatypes.get(datatypeLibrary); |
|
89 |
if(lib!=null) { |
|
90 |
TypeUse use = lib.get(type); |
|
91 |
if(use!=null) |
|
92 |
return use; |
|
93 |
} |
|
94 |
||
95 |
// unknown |
|
96 |
return CBuiltinLeafInfo.STRING; |
|
97 |
} |
|
98 |
||
99 |
public TypeUse onInterleave(DInterleavePattern p) { |
|
100 |
return onContainer(p); |
|
101 |
} |
|
102 |
||
103 |
public TypeUse onGroup(DGroupPattern p) { |
|
104 |
return onContainer(p); |
|
105 |
} |
|
106 |
||
107 |
private TypeUse onContainer(DContainerPattern p) { |
|
108 |
TypeUse t=null; |
|
109 |
for( DPattern child : p ) { |
|
110 |
TypeUse s = child.accept(this); |
|
111 |
if(t!=null && t!=s) |
|
112 |
return CBuiltinLeafInfo.STRING; // heterogenous |
|
113 |
t = s; |
|
114 |
} |
|
115 |
return t; |
|
116 |
} |
|
117 |
||
118 |
public TypeUse onNotAllowed(DNotAllowedPattern p) { |
|
119 |
// TODO |
|
120 |
return error(); |
|
121 |
} |
|
122 |
||
123 |
public TypeUse onEmpty(DEmptyPattern p) { |
|
124 |
return CBuiltinLeafInfo.STRING; |
|
125 |
} |
|
126 |
||
127 |
public TypeUse onList(DListPattern p) { |
|
128 |
return p.getChild().accept(this); |
|
129 |
} |
|
130 |
||
131 |
public TypeUse onOneOrMore(DOneOrMorePattern p) { |
|
132 |
return TypeUseFactory.makeCollection( p.getChild().accept(this) ); |
|
133 |
} |
|
134 |
||
135 |
public TypeUse onZeroOrMore(DZeroOrMorePattern p) { |
|
136 |
return TypeUseFactory.makeCollection( p.getChild().accept(this) ); |
|
137 |
} |
|
138 |
||
139 |
public TypeUse onOptional(DOptionalPattern p) { |
|
140 |
return CBuiltinLeafInfo.STRING; |
|
141 |
} |
|
142 |
||
143 |
public TypeUse onRef(DRefPattern p) { |
|
144 |
// TODO: check for enums |
|
145 |
return p.getTarget().getPattern().accept(this); |
|
146 |
} |
|
147 |
||
148 |
public TypeUse onText(DTextPattern p) { |
|
149 |
return CBuiltinLeafInfo.STRING; |
|
150 |
} |
|
151 |
||
152 |
// |
|
153 |
// |
|
154 |
// Not allowed in this context |
|
155 |
// |
|
156 |
// |
|
157 |
public TypeUse onAttribute(DAttributePattern p) { |
|
158 |
return error(); |
|
159 |
} |
|
160 |
||
161 |
public TypeUse onElement(DElementPattern p) { |
|
162 |
return error(); |
|
163 |
} |
|
164 |
||
165 |
public TypeUse onMixed(DMixedPattern p) { |
|
166 |
return error(); |
|
167 |
} |
|
168 |
||
169 |
private TypeUse error() { |
|
170 |
throw new IllegalStateException(); |
|
171 |
} |
|
172 |
} |