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 java.util.ArrayList; |
|
29 |
import java.util.HashMap; |
|
30 |
import java.util.List; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.Set; |
|
33 |
||
34 |
import javax.xml.namespace.QName; |
|
35 |
||
36 |
import com.sun.codemodel.internal.JCodeModel; |
|
37 |
import com.sun.codemodel.internal.JPackage; |
|
38 |
import com.sun.tools.internal.xjc.Options; |
|
39 |
import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo; |
|
40 |
import com.sun.tools.internal.xjc.model.CClassInfo; |
|
41 |
import com.sun.tools.internal.xjc.model.CClassInfoParent; |
|
42 |
import com.sun.tools.internal.xjc.model.CEnumConstant; |
|
43 |
import com.sun.tools.internal.xjc.model.CEnumLeafInfo; |
|
44 |
import com.sun.tools.internal.xjc.model.CNonElement; |
|
45 |
import com.sun.tools.internal.xjc.model.CTypeInfo; |
|
46 |
import com.sun.tools.internal.xjc.model.Model; |
|
47 |
import com.sun.tools.internal.xjc.model.TypeUse; |
|
48 |
import com.sun.xml.internal.bind.api.impl.NameConverter; |
|
49 |
||
50 |
import com.sun.xml.internal.rngom.digested.DChoicePattern; |
|
51 |
import com.sun.xml.internal.rngom.digested.DDefine; |
|
52 |
import com.sun.xml.internal.rngom.digested.DElementPattern; |
|
53 |
import com.sun.xml.internal.rngom.digested.DPattern; |
|
54 |
import com.sun.xml.internal.rngom.digested.DPatternWalker; |
|
55 |
import com.sun.xml.internal.rngom.digested.DRefPattern; |
|
56 |
import com.sun.xml.internal.rngom.digested.DValuePattern; |
|
57 |
import com.sun.xml.internal.rngom.nc.NameClass; |
|
58 |
import com.sun.xml.internal.rngom.xml.util.WellKnownNamespaces; |
|
59 |
||
60 |
/** |
|
61 |
* @author Kohsuke Kawaguchi |
|
62 |
*/ |
|
63 |
public final class RELAXNGCompiler { |
|
64 |
/** |
|
65 |
* Schema to compile. |
|
66 |
*/ |
|
67 |
final DPattern grammar; |
|
68 |
||
69 |
/** |
|
70 |
* All named patterns in this schema. |
|
71 |
*/ |
|
72 |
final Set<DDefine> defs; |
|
73 |
||
74 |
final Options opts; |
|
75 |
||
76 |
final Model model; |
|
77 |
||
78 |
/** |
|
79 |
* The package to which we generate the code into. |
|
80 |
*/ |
|
81 |
final JPackage pkg; |
|
82 |
||
83 |
final Map<String,DatatypeLib> datatypes = new HashMap<String, DatatypeLib>(); |
|
84 |
||
85 |
/** |
|
86 |
* Patterns that are mapped to Java concepts. |
|
87 |
* |
|
88 |
* <p> |
|
89 |
* The value is an array because we map elements with finite names |
|
90 |
* to multiple classes. |
|
91 |
* |
|
92 |
* TODO: depending on the type of the key, the type of the values can be further |
|
93 |
* restricted. Make this into its own class to represent those constraints better. |
|
94 |
*/ |
|
95 |
final Map<DPattern,CTypeInfo[]> classes = new HashMap<DPattern,CTypeInfo[]>(); |
|
96 |
||
97 |
/** |
|
98 |
* Classes that need to be bound. |
|
99 |
* |
|
100 |
* The value is the content model to be bound. |
|
101 |
*/ |
|
102 |
final Map<CClassInfo,DPattern> bindQueue = new HashMap<CClassInfo,DPattern>(); |
|
103 |
||
104 |
final TypeUseBinder typeUseBinder = new TypeUseBinder(this); |
|
105 |
||
106 |
public static Model build(DPattern grammar, JCodeModel codeModel, Options opts ) { |
|
107 |
RELAXNGCompiler compiler = new RELAXNGCompiler(grammar, codeModel, opts); |
|
108 |
compiler.compile(); |
|
109 |
return compiler.model; |
|
110 |
} |
|
111 |
||
112 |
public RELAXNGCompiler(DPattern grammar, JCodeModel codeModel, Options opts) { |
|
113 |
this.grammar = grammar; |
|
114 |
this.opts = opts; |
|
115 |
this.model = new Model(opts,codeModel, NameConverter.smart,opts.classNameAllocator,null); |
|
116 |
||
117 |
datatypes.put("",DatatypeLib.BUILTIN); |
|
118 |
datatypes.put(WellKnownNamespaces.XML_SCHEMA_DATATYPES,DatatypeLib.XMLSCHEMA); |
|
119 |
||
120 |
// find all defines |
|
121 |
DefineFinder deff = new DefineFinder(); |
|
122 |
grammar.accept(deff); |
|
123 |
this.defs = deff.defs; |
|
124 |
||
125 |
if(opts.defaultPackage2!=null) |
|
126 |
pkg = codeModel._package(opts.defaultPackage2); |
|
127 |
else |
|
128 |
if(opts.defaultPackage!=null) |
|
129 |
pkg = codeModel._package(opts.defaultPackage); |
|
130 |
else |
|
131 |
pkg = codeModel.rootPackage(); |
|
132 |
} |
|
133 |
||
134 |
private void compile() { |
|
135 |
// decide which patterns to map to classes |
|
136 |
promoteElementDefsToClasses(); |
|
137 |
promoteTypeSafeEnums(); |
|
138 |
// TODO: promote patterns with <jaxb:class> to classes |
|
139 |
// TODO: promote 'type' patterns to classes |
|
140 |
promoteTypePatternsToClasses(); |
|
141 |
||
142 |
for (Map.Entry<CClassInfo,DPattern> e : bindQueue.entrySet()) |
|
143 |
bindContentModel(e.getKey(),e.getValue()); |
|
144 |
} |
|
145 |
||
146 |
private void bindContentModel(CClassInfo clazz, DPattern pattern) { |
|
147 |
// first we decide which patterns in it map to properties |
|
148 |
// then we process each of them by using RawTypeSetBuilder. |
|
149 |
// much like DefaultParticleBinder in XSD |
|
150 |
pattern.accept(new ContentModelBinder(this,clazz)); |
|
151 |
} |
|
152 |
||
153 |
private void promoteTypeSafeEnums() { |
|
154 |
// we'll be trying a lot of choices, |
|
155 |
// and most of them will not be type-safe enum. |
|
156 |
// using the same list improves the memory efficiency. |
|
157 |
List<CEnumConstant> members = new ArrayList<CEnumConstant>(); |
|
158 |
||
159 |
OUTER: |
|
160 |
for( DDefine def : defs ) { |
|
161 |
DPattern p = def.getPattern(); |
|
162 |
if (p instanceof DChoicePattern) { |
|
163 |
DChoicePattern cp = (DChoicePattern) p; |
|
164 |
||
165 |
members.clear(); |
|
166 |
||
167 |
// check if the choice consists of all value patterns |
|
168 |
// and that they are of the same datatype |
|
169 |
DValuePattern vp = null; |
|
170 |
||
171 |
for( DPattern child : cp ) { |
|
172 |
if(child instanceof DValuePattern) { |
|
173 |
DValuePattern c = (DValuePattern) child; |
|
174 |
if(vp==null) |
|
175 |
vp=c; |
|
176 |
else { |
|
177 |
if(!vp.getDatatypeLibrary().equals(c.getDatatypeLibrary()) |
|
178 |
|| !vp.getType().equals(c.getType()) ) |
|
179 |
continue OUTER; // different type name |
|
180 |
} |
|
181 |
||
182 |
members.add(new CEnumConstant( |
|
183 |
model.getNameConverter().toConstantName(c.getValue()), |
|
184 |
null, c.getValue(), null, null/*TODO*/, c.getLocation() |
|
185 |
)); |
|
186 |
} else |
|
187 |
continue OUTER; // not a value |
|
188 |
} |
|
189 |
||
190 |
if(members.isEmpty()) |
|
191 |
continue; // empty choice |
|
192 |
||
193 |
CNonElement base = CBuiltinLeafInfo.STRING; |
|
194 |
||
195 |
DatatypeLib lib = datatypes.get(vp.getNs()); |
|
196 |
if(lib!=null) { |
|
197 |
TypeUse use = lib.get(vp.getType()); |
|
198 |
if(use instanceof CNonElement) |
|
199 |
base = (CNonElement)use; |
|
200 |
} |
|
201 |
||
202 |
CEnumLeafInfo xducer = new CEnumLeafInfo(model, null, |
|
203 |
new CClassInfoParent.Package(pkg), def.getName(), base, |
|
204 |
new ArrayList<CEnumConstant>(members), |
|
205 |
null, null/*TODO*/, cp.getLocation()); |
|
206 |
||
207 |
classes.put(cp,new CTypeInfo[]{xducer}); |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
||
213 |
private void promoteElementDefsToClasses() { |
|
214 |
// look for elements among named patterns |
|
215 |
for( DDefine def : defs ) { |
|
216 |
DPattern p = def.getPattern(); |
|
217 |
if (p instanceof DElementPattern) { |
|
218 |
DElementPattern ep = (DElementPattern) p; |
|
219 |
||
220 |
mapToClass(ep); |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
// also look for root elements |
|
225 |
grammar.accept(new DPatternWalker() { |
|
226 |
public Void onRef(DRefPattern p) { |
|
227 |
return null; // stop recursion |
|
228 |
} |
|
229 |
||
230 |
public Void onElement(DElementPattern p) { |
|
231 |
mapToClass(p); |
|
232 |
return null; |
|
233 |
} |
|
234 |
}); |
|
235 |
} |
|
236 |
||
237 |
private void mapToClass(DElementPattern p) { |
|
238 |
NameClass nc = p.getName(); |
|
239 |
if(nc.isOpen()) |
|
240 |
return; // infinite name. can't map to a class. |
|
241 |
||
242 |
Set<QName> names = nc.listNames(); |
|
243 |
||
244 |
CClassInfo[] types = new CClassInfo[names.size()]; |
|
245 |
int i=0; |
|
246 |
for( QName n : names ) { |
|
247 |
// TODO: read class names from customization |
|
248 |
String name = model.getNameConverter().toClassName(n.getLocalPart()); |
|
249 |
||
250 |
bindQueue.put( |
|
251 |
types[i++] = new CClassInfo(model,pkg,name,p.getLocation(),null,n,null,null/*TODO*/), |
|
252 |
p.getChild() ); |
|
253 |
} |
|
254 |
||
255 |
classes.put(p,types); |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Looks for named patterns that are not bound to classes so far, |
|
260 |
* but that can be bound to classes. |
|
261 |
*/ |
|
262 |
private void promoteTypePatternsToClasses() { |
|
263 |
||
264 |
// for( DDefine def : defs ) { |
|
265 |
// ; |
|
266 |
// |
|
267 |
// def.getPattern().accept(new InheritanceChecker()); |
|
268 |
// } |
|
269 |
} |
|
270 |
} |