1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
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;
27
28 import java.io.IOException;
29 import java.io.StringReader;
30
31 import com.sun.codemodel.internal.JCodeModel;
32 import com.sun.tools.internal.xjc.model.Model;
33 import com.sun.tools.internal.xjc.reader.Const;
34 import com.sun.tools.internal.xjc.reader.ExtensionBindingChecker;
35 import com.sun.tools.internal.xjc.reader.dtd.TDTDReader;
36 import com.sun.tools.internal.xjc.reader.internalizer.DOMForest;
37 import com.sun.tools.internal.xjc.reader.internalizer.DOMForestScanner;
38 import com.sun.tools.internal.xjc.reader.internalizer.InternalizationLogic;
39 import com.sun.tools.internal.xjc.reader.internalizer.SCDBasedBindingSet;
40 import com.sun.tools.internal.xjc.reader.internalizer.VersionChecker;
41 import com.sun.tools.internal.xjc.reader.relaxng.RELAXNGCompiler;
42 import com.sun.tools.internal.xjc.reader.relaxng.RELAXNGInternalizationLogic;
43 import com.sun.tools.internal.xjc.reader.xmlschema.BGMBuilder;
44 import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.AnnotationParserFactoryImpl;
45 import com.sun.tools.internal.xjc.reader.xmlschema.parser.CustomizationContextChecker;
46 import com.sun.tools.internal.xjc.reader.xmlschema.parser.IncorrectNamespaceURIChecker;
47 import com.sun.tools.internal.xjc.reader.xmlschema.parser.SchemaConstraintChecker;
48 import com.sun.tools.internal.xjc.reader.xmlschema.parser.XMLSchemaInternalizationLogic;
49 import com.sun.tools.internal.xjc.util.ErrorReceiverFilter;
50 import com.sun.xml.internal.bind.v2.util.XmlFactory;
51 import com.sun.xml.internal.xsom.XSSchemaSet;
52 import com.sun.xml.internal.xsom.parser.JAXPParser;
53 import com.sun.xml.internal.xsom.parser.XMLParser;
54 import com.sun.xml.internal.xsom.parser.XSOMParser;
55 import javax.xml.XMLConstants;
56
57 import com.sun.xml.internal.rngom.ast.builder.SchemaBuilder;
58 import com.sun.xml.internal.rngom.ast.util.CheckingSchemaBuilder;
59 import com.sun.xml.internal.rngom.digested.DPattern;
60 import com.sun.xml.internal.rngom.digested.DSchemaBuilderImpl;
61 import com.sun.xml.internal.rngom.parse.IllegalSchemaException;
62 import com.sun.xml.internal.rngom.parse.Parseable;
63 import com.sun.xml.internal.rngom.parse.compact.CompactParseable;
64 import com.sun.xml.internal.rngom.parse.xml.SAXParseable;
65 import com.sun.xml.internal.rngom.xml.sax.XMLReaderCreator;
66 import org.w3c.dom.Document;
67 import org.w3c.dom.Element;
68 import org.w3c.dom.NodeList;
69 import org.xml.sax.Attributes;
70 import org.xml.sax.ContentHandler;
71 import org.xml.sax.EntityResolver;
72 import org.xml.sax.ErrorHandler;
73 import org.xml.sax.InputSource;
74 import org.xml.sax.SAXException;
75 import org.xml.sax.SAXParseException;
76 import org.xml.sax.XMLFilter;
77 import org.xml.sax.XMLReader;
78 import org.xml.sax.helpers.XMLFilterImpl;
79
80 /**
81 * Builds a {@link Model} object.
82 *
83 * This is an utility class that makes it easy to load a grammar object
84 * from various sources.
85 *
86 * @author
87 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
88 */
89 public final class ModelLoader {
90
91 private final Options opt;
92 private final ErrorReceiverFilter errorReceiver;
93 private final JCodeModel codeModel;
94 /**
95 * {@link DOMForest#transform(boolean)} creates this on the side.
96 */
97 private SCDBasedBindingSet scdBasedBindingSet;
124 case DTD :
125 // TODO: make sure that bindFiles,size()<=1
126 InputSource bindFile = null;
127 if (opt.getBindFiles().length > 0)
128 bindFile = opt.getBindFiles()[0];
129 // if there is no binding file, make a dummy one.
130 if (bindFile == null) {
131 // if no binding information is specified, provide a default
132 bindFile =
133 new InputSource(
134 new StringReader(
135 "<?xml version='1.0'?><xml-java-binding-schema><options package='"
136 + (opt.defaultPackage==null?"generated":opt.defaultPackage)
137 + "'/></xml-java-binding-schema>"));
138 }
139
140 checkTooManySchemaErrors();
141 grammar = loadDTD(opt.getGrammars()[0], bindFile );
142 break;
143
144 case RELAXNG :
145 checkTooManySchemaErrors();
146 grammar = loadRELAXNG();
147 break;
148
149 case RELAXNG_COMPACT :
150 checkTooManySchemaErrors();
151 grammar = loadRELAXNGCompact();
152 break;
153
154 case WSDL:
155 grammar = annotateXMLSchema( loadWSDL() );
156 break;
157
158 case XMLSCHEMA:
159 grammar = annotateXMLSchema( loadXMLSchema() );
160 break;
161
162 default :
163 throw new AssertionError(); // assertion failed
164 }
165
166 if (errorReceiver.hadError()) {
167 grammar = null;
168 } else {
169 grammar.setPackageLevelAnnotations(opt.packageLevelAnnotations);
170 }
171
172 return grammar;
173
189 // error should have been reported already, since this is requested by the error receiver
190 return null;
191 }
192 }
193
194
195
196 /**
197 * Do some extra checking and return false if the compilation
198 * should abort.
199 */
200 private boolean sanityCheck() {
201 if( opt.getSchemaLanguage()==Language.XMLSCHEMA ) {
202 Language guess = opt.guessSchemaLanguage();
203
204 String[] msg = null;
205 switch(guess) {
206 case DTD:
207 msg = new String[]{"DTD","-dtd"};
208 break;
209 case RELAXNG:
210 msg = new String[]{"RELAX NG","-relaxng"};
211 break;
212 case RELAXNG_COMPACT:
213 msg = new String[]{"RELAX NG compact syntax","-relaxng-compact"};
214 break;
215 case WSDL:
216 msg = new String[]{"WSDL","-wsdl"};
217 break;
218 }
219 if( msg!=null )
220 errorReceiver.warning( null,
221 Messages.format(
222 Messages.EXPERIMENTAL_LANGUAGE_WARNING,
223 msg[0], msg[1] ));
224 }
225 return true;
226 }
227
228
229 /**
230 * {@link XMLParser} implementation that adds additional processors into the chain.
231 *
232 * <p>
233 * This parser will parse a DOM forest as:
234 * DOMForestParser -->
511 // set up other parameters to XSOMParser
512 XSOMParser reader = createXSOMParser(forest);
513
514 // re-parse the transformed schemas
515 for (String systemId : forest.getRootDocuments()) {
516 errorReceiver.pollAbort();
517 Document dom = forest.get(systemId);
518 if (!dom.getDocumentElement().getNamespaceURI().equals(Const.JAXB_NSURI)) {
519 reader.parse(systemId);
520 }
521 }
522
523 XSSchemaSet result = reader.getResult();
524
525 if(result!=null)
526 scdBasedBindingSet.apply(result,errorReceiver);
527
528 return result;
529 }
530
531 /**
532 * Parses a RELAX NG grammar into an annotated grammar.
533 */
534 private Model loadRELAXNG() throws SAXException {
535
536 // build DOM forest
537 final DOMForest forest = buildDOMForest( new RELAXNGInternalizationLogic() );
538
539 // use JAXP masquerading to validate the input document.
540 // DOMForest -> ExtensionBindingChecker -> RNGOM
541
542 XMLReaderCreator xrc = new XMLReaderCreator() {
543 public XMLReader createXMLReader() {
544
545 // foreset parser cannot change the receivers while it's working,
546 // so we need to have one XMLFilter that works as a buffer
547 XMLFilter buffer = new XMLFilterImpl() {
548 @Override
549 public void parse(InputSource source) throws IOException, SAXException {
550 forest.createParser().parse( source, this, this, this );
551 }
552 };
553
554 XMLFilter f = new ExtensionBindingChecker(Const.RELAXNG_URI,opt,errorReceiver);
555 f.setParent(buffer);
556
557 f.setEntityResolver(opt.entityResolver);
558
559 return f;
560 }
561 };
562
563 Parseable p = new SAXParseable( opt.getGrammars()[0], errorReceiver, xrc );
564
565 return loadRELAXNG(p);
566
567 }
568
569 /**
570 * Loads RELAX NG compact syntax
571 */
572 private Model loadRELAXNGCompact() {
573 if(opt.getBindFiles().length>0)
574 errorReceiver.error(new SAXParseException(
575 Messages.format(Messages.ERR_BINDING_FILE_NOT_SUPPORTED_FOR_RNC),null));
576
577 // TODO: entity resolver?
578 Parseable p = new CompactParseable( opt.getGrammars()[0], errorReceiver );
579
580 return loadRELAXNG(p);
581
582 }
583
584 /**
585 * Common part between the XML syntax and the compact syntax.
586 */
587 private Model loadRELAXNG(Parseable p) {
588 SchemaBuilder sb = new CheckingSchemaBuilder(new DSchemaBuilderImpl(),errorReceiver);
589
590 try {
591 DPattern out = (DPattern)p.parse(sb);
592 return RELAXNGCompiler.build(out,codeModel,opt);
593 } catch (IllegalSchemaException e) {
594 errorReceiver.error(e.getMessage(),e);
595 return null;
596 }
597 }
598 }
|
1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
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;
27
28 import java.io.IOException;
29 import java.io.StringReader;
30
31 import com.sun.codemodel.internal.JCodeModel;
32 import com.sun.tools.internal.xjc.model.Model;
33 import com.sun.tools.internal.xjc.reader.Const;
34 import com.sun.tools.internal.xjc.reader.ExtensionBindingChecker;
35 import com.sun.tools.internal.xjc.reader.dtd.TDTDReader;
36 import com.sun.tools.internal.xjc.reader.internalizer.DOMForest;
37 import com.sun.tools.internal.xjc.reader.internalizer.DOMForestScanner;
38 import com.sun.tools.internal.xjc.reader.internalizer.InternalizationLogic;
39 import com.sun.tools.internal.xjc.reader.internalizer.SCDBasedBindingSet;
40 import com.sun.tools.internal.xjc.reader.internalizer.VersionChecker;
41 import com.sun.tools.internal.xjc.reader.xmlschema.BGMBuilder;
42 import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.AnnotationParserFactoryImpl;
43 import com.sun.tools.internal.xjc.reader.xmlschema.parser.CustomizationContextChecker;
44 import com.sun.tools.internal.xjc.reader.xmlschema.parser.IncorrectNamespaceURIChecker;
45 import com.sun.tools.internal.xjc.reader.xmlschema.parser.SchemaConstraintChecker;
46 import com.sun.tools.internal.xjc.reader.xmlschema.parser.XMLSchemaInternalizationLogic;
47 import com.sun.tools.internal.xjc.util.ErrorReceiverFilter;
48 import com.sun.xml.internal.bind.v2.util.XmlFactory;
49 import com.sun.xml.internal.xsom.XSSchemaSet;
50 import com.sun.xml.internal.xsom.parser.JAXPParser;
51 import com.sun.xml.internal.xsom.parser.XMLParser;
52 import com.sun.xml.internal.xsom.parser.XSOMParser;
53 import javax.xml.XMLConstants;
54
55 import org.w3c.dom.Document;
56 import org.w3c.dom.Element;
57 import org.w3c.dom.NodeList;
58 import org.xml.sax.Attributes;
59 import org.xml.sax.ContentHandler;
60 import org.xml.sax.EntityResolver;
61 import org.xml.sax.ErrorHandler;
62 import org.xml.sax.InputSource;
63 import org.xml.sax.SAXException;
64 import org.xml.sax.SAXParseException;
65 import org.xml.sax.helpers.XMLFilterImpl;
66
67 /**
68 * Builds a {@link Model} object.
69 *
70 * This is an utility class that makes it easy to load a grammar object
71 * from various sources.
72 *
73 * @author
74 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
75 */
76 public final class ModelLoader {
77
78 private final Options opt;
79 private final ErrorReceiverFilter errorReceiver;
80 private final JCodeModel codeModel;
81 /**
82 * {@link DOMForest#transform(boolean)} creates this on the side.
83 */
84 private SCDBasedBindingSet scdBasedBindingSet;
111 case DTD :
112 // TODO: make sure that bindFiles,size()<=1
113 InputSource bindFile = null;
114 if (opt.getBindFiles().length > 0)
115 bindFile = opt.getBindFiles()[0];
116 // if there is no binding file, make a dummy one.
117 if (bindFile == null) {
118 // if no binding information is specified, provide a default
119 bindFile =
120 new InputSource(
121 new StringReader(
122 "<?xml version='1.0'?><xml-java-binding-schema><options package='"
123 + (opt.defaultPackage==null?"generated":opt.defaultPackage)
124 + "'/></xml-java-binding-schema>"));
125 }
126
127 checkTooManySchemaErrors();
128 grammar = loadDTD(opt.getGrammars()[0], bindFile );
129 break;
130
131 case WSDL:
132 grammar = annotateXMLSchema( loadWSDL() );
133 break;
134
135 case XMLSCHEMA:
136 grammar = annotateXMLSchema( loadXMLSchema() );
137 break;
138
139 default :
140 throw new AssertionError(); // assertion failed
141 }
142
143 if (errorReceiver.hadError()) {
144 grammar = null;
145 } else {
146 grammar.setPackageLevelAnnotations(opt.packageLevelAnnotations);
147 }
148
149 return grammar;
150
166 // error should have been reported already, since this is requested by the error receiver
167 return null;
168 }
169 }
170
171
172
173 /**
174 * Do some extra checking and return false if the compilation
175 * should abort.
176 */
177 private boolean sanityCheck() {
178 if( opt.getSchemaLanguage()==Language.XMLSCHEMA ) {
179 Language guess = opt.guessSchemaLanguage();
180
181 String[] msg = null;
182 switch(guess) {
183 case DTD:
184 msg = new String[]{"DTD","-dtd"};
185 break;
186 case WSDL:
187 msg = new String[]{"WSDL","-wsdl"};
188 break;
189 }
190 if( msg!=null )
191 errorReceiver.warning( null,
192 Messages.format(
193 Messages.EXPERIMENTAL_LANGUAGE_WARNING,
194 msg[0], msg[1] ));
195 }
196 return true;
197 }
198
199
200 /**
201 * {@link XMLParser} implementation that adds additional processors into the chain.
202 *
203 * <p>
204 * This parser will parse a DOM forest as:
205 * DOMForestParser -->
482 // set up other parameters to XSOMParser
483 XSOMParser reader = createXSOMParser(forest);
484
485 // re-parse the transformed schemas
486 for (String systemId : forest.getRootDocuments()) {
487 errorReceiver.pollAbort();
488 Document dom = forest.get(systemId);
489 if (!dom.getDocumentElement().getNamespaceURI().equals(Const.JAXB_NSURI)) {
490 reader.parse(systemId);
491 }
492 }
493
494 XSSchemaSet result = reader.getResult();
495
496 if(result!=null)
497 scdBasedBindingSet.apply(result,errorReceiver);
498
499 return result;
500 }
501
502 }
|