Debian Codegraph @

mod/convert-from-xml/src/main/com/thaiopensource/relaxng/input/xml/Inferrer.java

raw
1 package com.thaiopensource.relaxng.input.xml;
2
3 import com.thaiopensource.datatype.DatatypeLibraryLoader;
4 import com.thaiopensource.relaxng.edit.AttributePattern;
5 import com.thaiopensource.relaxng.edit.ChoicePattern;
6 import com.thaiopensource.relaxng.edit.CompositePattern;
7 import com.thaiopensource.relaxng.edit.DataPattern;
8 import com.thaiopensource.relaxng.edit.DefineComponent;
9 import com.thaiopensource.relaxng.edit.ElementPattern;
10 import com.thaiopensource.relaxng.edit.EmptyPattern;
11 import com.thaiopensource.relaxng.edit.GrammarPattern;
12 import com.thaiopensource.relaxng.edit.GroupPattern;
13 import com.thaiopensource.relaxng.edit.NameNameClass;
14 import com.thaiopensource.relaxng.edit.OneOrMorePattern;
15 import com.thaiopensource.relaxng.edit.OptionalPattern;
16 import com.thaiopensource.relaxng.edit.Pattern;
17 import com.thaiopensource.relaxng.edit.RefPattern;
18 import com.thaiopensource.relaxng.edit.SchemaCollection;
19 import com.thaiopensource.relaxng.edit.SchemaDocument;
20 import com.thaiopensource.relaxng.edit.TextPattern;
21 import com.thaiopensource.relaxng.edit.ZeroOrMorePattern;
22 import com.thaiopensource.resolver.Resolver;
23 import com.thaiopensource.resolver.xml.sax.SAXResolver;
24 import com.thaiopensource.xml.infer.AttributeDecl;
25 import com.thaiopensource.xml.infer.ChoiceParticle;
26 import com.thaiopensource.xml.infer.ElementDecl;
27 import com.thaiopensource.xml.infer.ElementParticle;
28 import com.thaiopensource.xml.infer.EmptyParticle;
29 import com.thaiopensource.xml.infer.InferHandler;
30 import com.thaiopensource.xml.infer.OneOrMoreParticle;
31 import com.thaiopensource.xml.infer.Particle;
32 import com.thaiopensource.xml.infer.ParticleVisitor;
33 import com.thaiopensource.xml.infer.Schema;
34 import com.thaiopensource.xml.infer.SequenceParticle;
35 import com.thaiopensource.xml.infer.TextParticle;
36 import com.thaiopensource.xml.util.Name;
37 import org.xml.sax.ErrorHandler;
38 import org.xml.sax.InputSource;
39 import org.xml.sax.SAXException;
40 import org.xml.sax.XMLReader;
41
42 import java.io.IOException;
43 import java.util.Collections;
44 import java.util.Comparator;
45 import java.util.HashSet;
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.Set;
50 import java.util.Vector;
51
52 class Inferrer {
53 private final Schema schema;
54 private final Set<Name> multiplyReferencedElementNames = new HashSet<Name>();
55 private final GrammarPattern grammar;
56 private final ParticleConverter particleConverter = new ParticleConverter();
57 private final List<Name> outputQueue = new Vector<Name>();
58 private final Set<Name> queued = new HashSet<Name>();
59 private String prefixSeparator;
60
61 private static final String SEPARATORS = ".-_";
62
63 static class Options {
64 String encoding;
65 Resolver resolver;
66 }
67
68 private static class PatternComparator implements Comparator<Pattern> {
69 private static final Class<?>[] classOrder = {
70 TextPattern.class, RefPattern.class, ElementPattern.class
71 };
72
73 public int compare(Pattern p1, Pattern p2) {
74 if (p1.getClass() != p2.getClass())
75 return classIndex(p1.getClass()) - classIndex(p2.getClass());
76 if (p1 instanceof RefPattern)
77 return ((RefPattern)p1).getName().compareTo(((RefPattern)p2).getName());
78 if (p1 instanceof ElementPattern)
79 return Name.compare(extractElementName(p1), extractElementName(p2));
80 return 0;
81 }
82
83 private static Name extractElementName(Object o) {
84 NameNameClass nnc = (NameNameClass)((ElementPattern)o).getNameClass();
85 return new Name(nnc.getNamespaceUri(), nnc.getLocalName());
86 }
87
88 private static int classIndex(Class<? extends Pattern> aClass) {
89 for (int i = 0; i < classOrder.length; i++)
90 if (aClass == classOrder[i])
91 return i;
92 return classOrder.length;
93 }
94 }
95
96 private class ParticleConverter extends PatternComparator implements ParticleVisitor {
97 public Object visitElement(ElementParticle p) {
98 Name name = p.getName();
99 if (multiplyReferencedElementNames.contains(name)) {
100 if (!queued.contains(name)) {
101 queued.add(name);
102 outputQueue.add(name);
103 }
104 return new RefPattern(getDefineName(name));
105 }
106 else
107 return createElementPattern(name);
108 }
109
110 public Object visitChoice(ChoiceParticle p) {
111 ChoicePattern cp = new ChoicePattern();
112 List<Pattern> children = cp.getChildren();
113 addChoices(children, p.getChild1());
114 addChoices(children, p.getChild2());
115 Collections.sort(children, this);
116 for (Iterator<Pattern> iter = children.iterator(); iter.hasNext();)
117 if (iter.next() instanceof EmptyPattern) {
118 iter.remove();
119 return makeOptional(cp);
120 }
121 return cp;
122 }
123
124 private Object makeOptional(ChoicePattern cp) {
125 List<Pattern> children = cp.getChildren();
126 boolean done = false;
127 for (int i = 0, len = children.size(); i < len; i++) {
128 Pattern child = children.get(i);
129 if (child instanceof OneOrMorePattern) {
130 children.set(i, new ZeroOrMorePattern(((OneOrMorePattern)child).getChild()));
131 done = true;
132 }
133 }
134 if (done)
135 return normalize(cp);
136 return new OptionalPattern(normalize(cp));
137 }
138
139
140 private void addChoices(List<Pattern> children, Particle child) {
141 Pattern pattern = convert(child);
142 if (pattern instanceof ChoicePattern)
143 children.addAll(((ChoicePattern)pattern).getChildren());
144 else
145 children.add(pattern);
146 }
147
148 public Object visitSequence(SequenceParticle p) {
149 GroupPattern gp = new GroupPattern();
150 addGroup(gp.getChildren(), p.getChild1());
151 addGroup(gp.getChildren(), p.getChild2());
152 return gp;
153 }
154
155 private void addGroup(List<Pattern> children, Particle child) {
156 Pattern pattern = convert(child);
157 if (pattern instanceof GroupPattern)
158 children.addAll(((GroupPattern)pattern).getChildren());
159 else
160 children.add(pattern);
161 }
162
163 public Object visitEmpty(EmptyParticle p) {
164 return new EmptyPattern();
165 }
166
167 public Object visitText(TextParticle p) {
168 return new TextPattern();
169 }
170
171 public Object visitOneOrMore(OneOrMoreParticle p) {
172 return new OneOrMorePattern(convert(p.getChild()));
173 }
174
175 public Pattern convert(Particle particle) {
176 return (Pattern)particle.accept(this);
177 }
178 }
179
180 private class ReferenceFinder implements ParticleVisitor {
181 private final Set<Name> referencedElementNames = new HashSet<Name>();
182
183 public Object visitElement(ElementParticle p) {
184 Name name = p.getName();
185 if (referencedElementNames.contains(name))
186 multiplyReferencedElementNames.add(name);
187 else
188 referencedElementNames.add(name);
189 return null;
190 }
191
192 public Object visitChoice(ChoiceParticle p) {
193 p.getChild1().accept(this);
194 p.getChild2().accept(this);
195 return null;
196 }
197
198 public Object visitSequence(SequenceParticle p) {
199 p.getChild1().accept(this);
200 p.getChild2().accept(this);
201 return null;
202 }
203
204 public Object visitEmpty(EmptyParticle p) {
205 return null;
206 }
207
208 public Object visitText(TextParticle p) {
209 return null;
210 }
211
212 public Object visitOneOrMore(OneOrMoreParticle p) {
213 return p.getChild().accept(this);
214 }
215 }
216
217 static SchemaCollection infer(String[] args, Options options, ErrorHandler eh) throws SAXException, IOException {
218 InferHandler handler = new InferHandler(new DatatypeLibraryLoader());
219 XMLReader xr = new SAXResolver(options.resolver).createXMLReader();
220 xr.setErrorHandler(eh);
221 xr.setContentHandler(handler);
222 for (int i = 0; i < args.length; i++) {
223 InputSource in = new InputSource(args[i]);
224 if (options.encoding != null)
225 in.setEncoding(options.encoding);
226 xr.parse(in);
227 }
228 SchemaCollection sc = new SchemaCollection();
229 sc.setMainUri(args[0]);
230 SchemaDocument sd = new SchemaDocument(new Inferrer(handler.getSchema()).grammar);
231 sc.getSchemaDocumentMap().put(sc.getMainUri(), sd);
232 return sc;
233 }
234
235 private Inferrer(Schema schema) {
236 this.schema = schema;
237 this.grammar = new GrammarPattern();
238 findMultiplyReferencedElements();
239 choosePrefixSeparator();
240 grammar.getComponents().add(new DefineComponent(DefineComponent.START,
241 particleConverter.convert(schema.getStart())));
242 // Names can get added to outputQueue during this loop,
243 // so don't change this to a for each.
244 for (int i = 0; i < outputQueue.size(); i++) {
245 Name elementName = outputQueue.get(i);
246 grammar.getComponents().add(new DefineComponent(getDefineName(elementName),
247 createElementPattern(elementName)));
248 }
249 }
250
251 private void findMultiplyReferencedElements() {
252 ReferenceFinder finder = new ReferenceFinder();
253 schema.getStart().accept(finder);
254 for (ElementDecl decl : schema.getElementDecls().values()) {
255 Particle particle = decl.getContentModel();
256 if (particle != null)
257 particle.accept(finder);
258 }
259 }
260
261
262 private void choosePrefixSeparator() {
263 Map<String, String> prefixMap = schema.getPrefixMap();
264 Set<String> namespacesInDefines = new HashSet<String>();
265 for (Name name : multiplyReferencedElementNames)
266 namespacesInDefines.add(name.getNamespaceUri());
267 if (namespacesInDefines.size() <= 1)
268 return; // don't need to use prefixes in defines
269 // define additional prefixes if necessary
270 namespacesInDefines.removeAll(prefixMap.keySet());
271 if (namespacesInDefines.size() > 1) {
272 namespacesInDefines.remove("");
273 int n = 1;
274 for (String ns : namespacesInDefines) {
275 for (; ;) {
276 String prefix = "ns" + Integer.toString(n++);
277 if (!prefixMap.containsKey(prefix)) {
278 prefixMap.put(ns, prefix);
279 break;
280 }
281 }
282 }
283 }
284 // choose a prefixSeparator that avoids all collisions
285 StringBuffer buf = new StringBuffer();
286 for (int len = 1;; len++)
287 for (int i = 0; i < SEPARATORS.length(); i++) {
288 char c = SEPARATORS.charAt(i);
289 for (int j = 0; j < len; j++)
290 buf.append(c);
291 prefixSeparator = buf.toString();
292 if (prefixSeparatorOk())
293 return;
294 buf.setLength(0);
295 }
296 }
297
298 private boolean prefixSeparatorOk() {
299 Set<String> names = new HashSet<String>();
300 for (Name elementName : multiplyReferencedElementNames) {
301 String name = getDefineName(elementName);
302 if (names.contains(name))
303 return false;
304 names.add(name);
305 }
306 return true;
307 }
308
309 private Pattern createElementPattern(Name elementName) {
310 ElementDecl elementDecl = schema.getElementDecl(elementName);
311 Pattern contentPattern;
312 Particle particle = elementDecl.getContentModel();
313 if (particle != null)
314 contentPattern = particleConverter.convert(particle);
315 else
316 contentPattern = makeDatatype(elementDecl.getDatatype());
317 Map<Name, AttributeDecl> attributeDecls = elementDecl.getAttributeDecls();
318 if (attributeDecls.size() > 0) {
319 GroupPattern group = new GroupPattern();
320 List<Name> attributeNames = new Vector<Name>();
321 attributeNames.addAll(attributeDecls.keySet());
322 Collections.sort(attributeNames, new Comparator<Name>() {
323 public int compare(Name n1, Name n2) {
324 return Name.compare(n1, n2);
325 }
326 });
327 for (Name attName : attributeNames) {
328 AttributeDecl att = attributeDecls.get(attName);
329 Pattern tem;
330 if (att.getDatatype() == null)
331 tem = new TextPattern();
332 else
333 tem = makeDatatype(att.getDatatype());
334 tem = new AttributePattern(makeNameClass(attName), tem);
335 if (att.isOptional())
336 tem = new OptionalPattern(tem);
337 group.getChildren().add(tem);
338 }
339 if (contentPattern instanceof GroupPattern)
340 group.getChildren().addAll(((GroupPattern)contentPattern).getChildren());
341 else if (!(contentPattern instanceof EmptyPattern))
342 group.getChildren().add(contentPattern);
343 contentPattern = group;
344 }
345 return new ElementPattern(makeNameClass(elementName), contentPattern);
346 }
347
348 private NameNameClass makeNameClass(Name name) {
349 String ns = name.getNamespaceUri();
350 NameNameClass nnc = new NameNameClass(ns, name.getLocalName());
351 if (!ns.equals("")) {
352 String prefix = schema.getPrefixMap().get(ns);
353 if (prefix != null)
354 nnc.setPrefix(prefix);
355 }
356 return nnc;
357 }
358
359 private static DataPattern makeDatatype(Name datatypeName) {
360 return new DataPattern(datatypeName.getNamespaceUri(), datatypeName.getLocalName());
361 }
362
363 private String getDefineName(Name elementName) {
364 if (prefixSeparator != null) {
365 String prefix = schema.getPrefixMap().get(elementName.getNamespaceUri());
366 if (prefix != null)
367 return prefix + prefixSeparator + elementName.getLocalName();
368 }
369 return elementName.getLocalName();
370 }
371
372 private static Pattern normalize(CompositePattern cp) {
373 if (cp.getChildren().size() == 1)
374 return cp.getChildren().get(0);
375 return cp;
376 }
377
378 }