src/share/jaxws_classes/com/sun/xml/internal/rngom/digested/DXMLPrinter.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25 /*
ohair@286 26 * Copyright (C) 2004-2011
ohair@286 27 *
ohair@286 28 * Permission is hereby granted, free of charge, to any person obtaining a copy
ohair@286 29 * of this software and associated documentation files (the "Software"), to deal
ohair@286 30 * in the Software without restriction, including without limitation the rights
ohair@286 31 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ohair@286 32 * copies of the Software, and to permit persons to whom the Software is
ohair@286 33 * furnished to do so, subject to the following conditions:
ohair@286 34 *
ohair@286 35 * The above copyright notice and this permission notice shall be included in
ohair@286 36 * all copies or substantial portions of the Software.
ohair@286 37 *
ohair@286 38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ohair@286 39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ohair@286 40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ohair@286 41 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ohair@286 42 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ohair@286 43 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ohair@286 44 * THE SOFTWARE.
ohair@286 45 */
ohair@286 46 package com.sun.xml.internal.rngom.digested;
ohair@286 47
ohair@286 48 import java.io.File;
ohair@286 49 import java.io.FileOutputStream;
ohair@286 50 import java.io.OutputStream;
ohair@286 51 import java.util.List;
ohair@286 52
ohair@286 53 import javax.xml.namespace.QName;
ohair@286 54 import javax.xml.stream.XMLOutputFactory;
ohair@286 55 import javax.xml.stream.XMLStreamException;
ohair@286 56 import javax.xml.stream.XMLStreamWriter;
ohair@286 57
ohair@286 58 import com.sun.xml.internal.rngom.ast.builder.BuildException;
ohair@286 59 import com.sun.xml.internal.rngom.ast.builder.SchemaBuilder;
ohair@286 60 import com.sun.xml.internal.rngom.ast.util.CheckingSchemaBuilder;
ohair@286 61 import com.sun.xml.internal.rngom.nc.NameClass;
ohair@286 62 import com.sun.xml.internal.rngom.nc.NameClassVisitor;
ohair@286 63 import com.sun.xml.internal.rngom.nc.SimpleNameClass;
ohair@286 64 import com.sun.xml.internal.rngom.parse.Parseable;
ohair@286 65 import com.sun.xml.internal.rngom.parse.compact.CompactParseable;
ohair@286 66 import com.sun.xml.internal.rngom.parse.xml.SAXParseable;
ohair@286 67 import com.sun.xml.internal.rngom.xml.util.WellKnownNamespaces;
ohair@286 68 import org.w3c.dom.Element;
ohair@286 69 import org.w3c.dom.Node;
ohair@286 70 import org.xml.sax.ErrorHandler;
ohair@286 71 import org.xml.sax.InputSource;
ohair@286 72 import org.xml.sax.SAXException;
ohair@286 73 import org.xml.sax.SAXParseException;
ohair@286 74 import org.xml.sax.helpers.DefaultHandler;
ohair@286 75
ohair@286 76 /**
ohair@286 77 * Printer of RELAX NG digested model to XML using StAX {@link XMLStreamWriter}.
ohair@286 78 *
ohair@286 79 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
ohair@286 80 */
ohair@286 81 public class DXMLPrinter {
ohair@286 82 protected XMLStreamWriter out;
ohair@286 83 protected String indentStep = "\t";
ohair@286 84 protected String newLine = System.getProperty("line.separator");
ohair@286 85 protected int indent;
ohair@286 86 protected boolean afterEnd = false;
ohair@286 87 protected DXMLPrinterVisitor visitor;
ohair@286 88 protected NameClassXMLPrinterVisitor ncVisitor;
ohair@286 89 protected DOMPrinter domPrinter;
ohair@286 90
ohair@286 91 /**
ohair@286 92 * @param out Output stream.
ohair@286 93 */
ohair@286 94 public DXMLPrinter(XMLStreamWriter out) {
ohair@286 95 this.out = out;
ohair@286 96 this.visitor = new DXMLPrinterVisitor();
ohair@286 97 this.ncVisitor = new NameClassXMLPrinterVisitor();
ohair@286 98 this.domPrinter = new DOMPrinter(out);
ohair@286 99 }
ohair@286 100
ohair@286 101 /**
ohair@286 102 * Prints grammar enclosed by start/end document.
ohair@286 103 *
ohair@286 104 * @param grammar
ohair@286 105 * @throws XMLStreamException
ohair@286 106 */
ohair@286 107 public void printDocument(DGrammarPattern grammar) throws XMLStreamException {
ohair@286 108 try {
ohair@286 109 visitor.startDocument();
ohair@286 110 visitor.on(grammar);
ohair@286 111 visitor.endDocument();
ohair@286 112 } catch (XMLWriterException e) {
mkos@408 113 if (e.getCause() instanceof XMLStreamException) {
mkos@408 114 throw (XMLStreamException) e.getCause();
mkos@408 115 } else {
mkos@408 116 throw new XMLStreamException(e);
mkos@408 117 }
ohair@286 118 }
ohair@286 119 }
ohair@286 120
ohair@286 121 /**
ohair@286 122 * Prints XML fragment for the given pattern.
ohair@286 123 *
ohair@286 124 * @throws XMLStreamException
ohair@286 125 */
ohair@286 126 public void print(DPattern pattern) throws XMLStreamException {
ohair@286 127 try {
ohair@286 128 pattern.accept(visitor);
ohair@286 129 } catch (XMLWriterException e) {
mkos@408 130 if (e.getCause() instanceof XMLStreamException) {
mkos@408 131 throw (XMLStreamException) e.getCause();
mkos@408 132 } else {
mkos@408 133 throw new XMLStreamException(e);
mkos@408 134 }
ohair@286 135 }
ohair@286 136 }
ohair@286 137
ohair@286 138 /**
ohair@286 139 * Prints XML fragment for the given name class.
ohair@286 140 *
ohair@286 141 * @throws XMLStreamException
ohair@286 142 */
ohair@286 143 public void print(NameClass nc) throws XMLStreamException {
ohair@286 144 try {
ohair@286 145 nc.accept(ncVisitor);
ohair@286 146 } catch (XMLWriterException e) {
mkos@408 147 if (e.getCause() instanceof XMLStreamException) {
mkos@408 148 throw (XMLStreamException) e.getCause();
mkos@408 149 } else {
mkos@408 150 throw new XMLStreamException(e);
mkos@408 151 }
ohair@286 152 }
ohair@286 153 }
ohair@286 154
ohair@286 155 public void print(Node node) throws XMLStreamException {
ohair@286 156 domPrinter.print(node);
ohair@286 157 }
ohair@286 158
ohair@286 159 protected class XMLWriterException extends RuntimeException {
ohair@286 160 protected XMLWriterException(Throwable cause) {
ohair@286 161 super(cause);
ohair@286 162 }
ohair@286 163 }
ohair@286 164
ohair@286 165 protected class XMLWriter {
ohair@286 166 protected void newLine() {
ohair@286 167 try {
ohair@286 168 out.writeCharacters(newLine);
ohair@286 169 } catch (XMLStreamException e) {
ohair@286 170 throw new XMLWriterException(e);
ohair@286 171 }
ohair@286 172 }
ohair@286 173
ohair@286 174 protected void indent() {
ohair@286 175 try {
ohair@286 176 for (int i = 0; i < indent; i++) {
ohair@286 177 out.writeCharacters(indentStep);
ohair@286 178 }
ohair@286 179 } catch (XMLStreamException e) {
ohair@286 180 throw new XMLWriterException(e);
ohair@286 181 }
ohair@286 182 }
ohair@286 183
ohair@286 184 public void startDocument() {
ohair@286 185 try {
ohair@286 186 out.writeStartDocument();
ohair@286 187 } catch (XMLStreamException e) {
ohair@286 188 throw new XMLWriterException(e);
ohair@286 189 }
ohair@286 190 }
ohair@286 191
ohair@286 192 public void endDocument() {
ohair@286 193 try {
ohair@286 194 out.writeEndDocument();
ohair@286 195 } catch (XMLStreamException e) {
ohair@286 196 throw new XMLWriterException(e);
ohair@286 197 }
ohair@286 198 }
ohair@286 199
ohair@286 200 public final void start(String element) {
ohair@286 201 try {
ohair@286 202 newLine();
ohair@286 203 indent();
ohair@286 204 out.writeStartElement(element);
ohair@286 205 indent++;
ohair@286 206 afterEnd = false;
ohair@286 207 } catch (XMLStreamException e) {
ohair@286 208 throw new XMLWriterException(e);
ohair@286 209 }
ohair@286 210 }
ohair@286 211
ohair@286 212 public void end() {
ohair@286 213 try {
ohair@286 214 indent--;
ohair@286 215 if (afterEnd) {
ohair@286 216 newLine();
ohair@286 217 indent();
ohair@286 218 }
ohair@286 219 out.writeEndElement();
ohair@286 220 afterEnd = true;
ohair@286 221 } catch (XMLStreamException e) {
ohair@286 222 throw new XMLWriterException(e);
ohair@286 223 }
ohair@286 224 }
ohair@286 225
ohair@286 226 public void attr(String prefix, String ns, String name, String value) {
ohair@286 227 try {
ohair@286 228 out.writeAttribute(prefix, ns, name, value);
ohair@286 229 } catch (XMLStreamException e) {
ohair@286 230 throw new XMLWriterException(e);
ohair@286 231 }
ohair@286 232 }
ohair@286 233
ohair@286 234 public void attr(String name, String value) {
ohair@286 235 try {
ohair@286 236 out.writeAttribute(name, value);
ohair@286 237 } catch (XMLStreamException e) {
ohair@286 238 throw new XMLWriterException(e);
ohair@286 239 }
ohair@286 240 }
ohair@286 241
ohair@286 242 public void ns(String prefix, String uri) {
ohair@286 243 try {
ohair@286 244 out.writeNamespace(prefix, uri);
ohair@286 245 } catch (XMLStreamException e) {
ohair@286 246 throw new XMLWriterException(e);
ohair@286 247 }
ohair@286 248 }
ohair@286 249
ohair@286 250 public void body(String text) {
ohair@286 251 try {
ohair@286 252 out.writeCharacters(text);
ohair@286 253 afterEnd = false;
ohair@286 254 } catch (XMLStreamException e) {
ohair@286 255 throw new XMLWriterException(e);
ohair@286 256 }
ohair@286 257 }
ohair@286 258 }
ohair@286 259
ohair@286 260 protected class DXMLPrinterVisitor extends XMLWriter implements DPatternVisitor<Void> {
ohair@286 261 protected void on(DPattern p) {
ohair@286 262 p.accept(this);
ohair@286 263 }
ohair@286 264
ohair@286 265 protected void unwrapGroup(DPattern p) {
ohair@286 266 if (p instanceof DGroupPattern && p.getAnnotation() == DAnnotation.EMPTY) {
ohair@286 267 for (DPattern d : (DGroupPattern) p) {
ohair@286 268 on(d);
ohair@286 269 }
ohair@286 270 } else {
ohair@286 271 on(p);
ohair@286 272 }
ohair@286 273 }
ohair@286 274
ohair@286 275 protected void unwrapChoice(DPattern p) {
ohair@286 276 if (p instanceof DChoicePattern && p.getAnnotation() == DAnnotation.EMPTY) {
ohair@286 277 for (DPattern d : (DChoicePattern) p) {
ohair@286 278 on(d);
ohair@286 279 }
ohair@286 280 } else {
ohair@286 281 on(p);
ohair@286 282 }
ohair@286 283 }
ohair@286 284
ohair@286 285 protected void on(NameClass nc) {
ohair@286 286 if (nc instanceof SimpleNameClass) {
ohair@286 287 QName qname = ((SimpleNameClass) nc).name;
ohair@286 288 String name = qname.getLocalPart();
ohair@286 289 if (!qname.getPrefix().equals("")) name = qname.getPrefix() + ":";
ohair@286 290 attr("name", name);
ohair@286 291 } else {
ohair@286 292 nc.accept(ncVisitor);
ohair@286 293 }
ohair@286 294 }
ohair@286 295
ohair@286 296 protected void on(DAnnotation ann) {
ohair@286 297 if (ann == DAnnotation.EMPTY) return;
ohair@286 298 for (DAnnotation.Attribute attr : ann.getAttributes().values()) {
ohair@286 299 attr(attr.getPrefix(), attr.getNs(), attr.getLocalName(), attr.getValue());
ohair@286 300 }
ohair@286 301 for (Element elem : ann.getChildren()) {
ohair@286 302 try {
ohair@286 303 newLine();
ohair@286 304 indent();
ohair@286 305 print(elem);
ohair@286 306 }
ohair@286 307 catch (XMLStreamException e) {
ohair@286 308 throw new XMLWriterException(e);
ohair@286 309 }
ohair@286 310 }
ohair@286 311 }
ohair@286 312
ohair@286 313 public Void onAttribute(DAttributePattern p) {
ohair@286 314 start("attribute");
ohair@286 315 on(p.getName());
ohair@286 316 on(p.getAnnotation());
ohair@286 317 DPattern child = p.getChild();
ohair@286 318 // do not print default value
ohair@286 319 if (!(child instanceof DTextPattern)) {
ohair@286 320 on(p.getChild());
ohair@286 321 }
ohair@286 322 end();
ohair@286 323 return null;
ohair@286 324 }
ohair@286 325
ohair@286 326 public Void onChoice(DChoicePattern p) {
ohair@286 327 start("choice");
ohair@286 328 on(p.getAnnotation());
ohair@286 329 for (DPattern d : p) {
ohair@286 330 on(d);
ohair@286 331 }
ohair@286 332 end();
ohair@286 333 return null;
ohair@286 334 }
ohair@286 335
ohair@286 336 public Void onData(DDataPattern p) {
ohair@286 337 List<DDataPattern.Param> params = p.getParams();
ohair@286 338 DPattern except = p.getExcept();
ohair@286 339 start("data");
ohair@286 340 attr("datatypeLibrary", p.getDatatypeLibrary());
ohair@286 341 attr("type", p.getType());
ohair@286 342 on(p.getAnnotation());
ohair@286 343 for (DDataPattern.Param param : params) {
ohair@286 344 start("param");
ohair@286 345 attr("ns", param.getNs());
ohair@286 346 attr("name", param.getName());
ohair@286 347 body(param.getValue());
ohair@286 348 end();
ohair@286 349 }
ohair@286 350 if (except != null) {
ohair@286 351 start("except");
ohair@286 352 unwrapChoice(except);
ohair@286 353 end();
ohair@286 354 }
ohair@286 355 end();
ohair@286 356 return null;
ohair@286 357 }
ohair@286 358
ohair@286 359 public Void onElement(DElementPattern p) {
ohair@286 360 start("element");
ohair@286 361 on(p.getName());
ohair@286 362 on(p.getAnnotation());
ohair@286 363 unwrapGroup(p.getChild());
ohair@286 364 end();
ohair@286 365 return null;
ohair@286 366 }
ohair@286 367
ohair@286 368 public Void onEmpty(DEmptyPattern p) {
ohair@286 369 start("empty");
ohair@286 370 on(p.getAnnotation());
ohair@286 371 end();
ohair@286 372 return null;
ohair@286 373 }
ohair@286 374
ohair@286 375 public Void onGrammar(DGrammarPattern p) {
ohair@286 376 start("grammar");
ohair@286 377 ns(null, WellKnownNamespaces.RELAX_NG);
ohair@286 378 on(p.getAnnotation());
ohair@286 379 start("start");
ohair@286 380 on(p.getStart());
ohair@286 381 end();
ohair@286 382 for (DDefine d : p) {
ohair@286 383 start("define");
ohair@286 384 attr("name", d.getName());
ohair@286 385 on(d.getAnnotation());
ohair@286 386 unwrapGroup(d.getPattern());
ohair@286 387 end();
ohair@286 388 }
ohair@286 389 end();
ohair@286 390 return null;
ohair@286 391 }
ohair@286 392
ohair@286 393 public Void onGroup(DGroupPattern p) {
ohair@286 394 start("group");
ohair@286 395 on(p.getAnnotation());
ohair@286 396 for (DPattern d : p) {
ohair@286 397 on(d);
ohair@286 398 }
ohair@286 399 end();
ohair@286 400 return null;
ohair@286 401 }
ohair@286 402
ohair@286 403 public Void onInterleave(DInterleavePattern p) {
ohair@286 404 start("interleave");
ohair@286 405 on(p.getAnnotation());
ohair@286 406 for (DPattern d : p) {
ohair@286 407 on(d);
ohair@286 408 }
ohair@286 409 end();
ohair@286 410 return null;
ohair@286 411 }
ohair@286 412
ohair@286 413 public Void onList(DListPattern p) {
ohair@286 414 start("list");
ohair@286 415 on(p.getAnnotation());
ohair@286 416 unwrapGroup(p.getChild());
ohair@286 417 end();
ohair@286 418 return null;
ohair@286 419 }
ohair@286 420
ohair@286 421 public Void onMixed(DMixedPattern p) {
ohair@286 422 start("mixed");
ohair@286 423 on(p.getAnnotation());
ohair@286 424 unwrapGroup(p.getChild());
ohair@286 425 end();
ohair@286 426 return null;
ohair@286 427 }
ohair@286 428
ohair@286 429 public Void onNotAllowed(DNotAllowedPattern p) {
ohair@286 430 start("notAllowed");
ohair@286 431 on(p.getAnnotation());
ohair@286 432 end();
ohair@286 433 return null;
ohair@286 434 }
ohair@286 435
ohair@286 436 public Void onOneOrMore(DOneOrMorePattern p) {
ohair@286 437 start("oneOrMore");
ohair@286 438 on(p.getAnnotation());
ohair@286 439 unwrapGroup(p.getChild());
ohair@286 440 end();
ohair@286 441 return null;
ohair@286 442 }
ohair@286 443
ohair@286 444 public Void onOptional(DOptionalPattern p) {
ohair@286 445 start("optional");
ohair@286 446 on(p.getAnnotation());
ohair@286 447 unwrapGroup(p.getChild());
ohair@286 448 end();
ohair@286 449 return null;
ohair@286 450 }
ohair@286 451
ohair@286 452 public Void onRef(DRefPattern p) {
ohair@286 453 start("ref");
ohair@286 454 attr("name", p.getName());
ohair@286 455 on(p.getAnnotation());
ohair@286 456 end();
ohair@286 457 return null;
ohair@286 458 }
ohair@286 459
ohair@286 460 public Void onText(DTextPattern p) {
ohair@286 461 start("text");
ohair@286 462 on(p.getAnnotation());
ohair@286 463 end();
ohair@286 464 return null;
ohair@286 465 }
ohair@286 466
ohair@286 467 public Void onValue(DValuePattern p) {
ohair@286 468 start("value");
ohair@286 469 if (!p.getNs().equals("")) attr("ns", p.getNs());
ohair@286 470 attr("datatypeLibrary", p.getDatatypeLibrary());
ohair@286 471 attr("type", p.getType());
ohair@286 472 on(p.getAnnotation());
ohair@286 473 body(p.getValue());
ohair@286 474 end();
ohair@286 475 return null;
ohair@286 476 }
ohair@286 477
ohair@286 478 public Void onZeroOrMore(DZeroOrMorePattern p) {
ohair@286 479 start("zeroOrMore");
ohair@286 480 on(p.getAnnotation());
ohair@286 481 unwrapGroup(p.getChild());
ohair@286 482 end();
ohair@286 483 return null;
ohair@286 484 }
ohair@286 485 }
ohair@286 486
ohair@286 487 protected class NameClassXMLPrinterVisitor extends XMLWriter implements NameClassVisitor<Void> {
ohair@286 488 public Void visitChoice(NameClass nc1, NameClass nc2) {
ohair@286 489 // TODO: flatten nested choices
ohair@286 490 start("choice");
ohair@286 491 nc1.accept(this);
ohair@286 492 nc2.accept(this);
ohair@286 493 end();
ohair@286 494 return null;
ohair@286 495 }
ohair@286 496
ohair@286 497 public Void visitNsName(String ns) {
ohair@286 498 start("nsName");
ohair@286 499 attr("ns", ns);
ohair@286 500 end();
ohair@286 501 return null;
ohair@286 502 }
ohair@286 503
ohair@286 504 public Void visitNsNameExcept(String ns, NameClass nc) {
ohair@286 505 start("nsName");
ohair@286 506 attr("ns", ns);
ohair@286 507 start("except");
ohair@286 508 nc.accept(this);
ohair@286 509 end();
ohair@286 510 end();
ohair@286 511 return null;
ohair@286 512 }
ohair@286 513
ohair@286 514 public Void visitAnyName() {
ohair@286 515 start("anyName");
ohair@286 516 end();
ohair@286 517 return null;
ohair@286 518 }
ohair@286 519
ohair@286 520 public Void visitAnyNameExcept(NameClass nc) {
ohair@286 521 start("anyName");
ohair@286 522 start("except");
ohair@286 523 nc.accept(this);
ohair@286 524 end();
ohair@286 525 end();
ohair@286 526 return null;
ohair@286 527 }
ohair@286 528
ohair@286 529 public Void visitName(QName name) {
ohair@286 530 start("name");
ohair@286 531 if (!name.getPrefix().equals("")) {
ohair@286 532 body(name.getPrefix() + ":");
ohair@286 533 }
ohair@286 534 body(name.getLocalPart());
ohair@286 535 end();
ohair@286 536 return null;
ohair@286 537 }
ohair@286 538
ohair@286 539 public Void visitNull() {
ohair@286 540 throw new UnsupportedOperationException("visitNull");
ohair@286 541 }
ohair@286 542 }
ohair@286 543
ohair@286 544 public static void main(String[] args) throws Exception {
ohair@286 545 Parseable p;
ohair@286 546
ohair@286 547 ErrorHandler eh = new DefaultHandler() {
ohair@286 548 @Override
ohair@286 549 public void error(SAXParseException e) throws SAXException {
ohair@286 550 throw e;
ohair@286 551 }
ohair@286 552 };
ohair@286 553
ohair@286 554 // the error handler passed to Parseable will receive parsing errors.
ohair@286 555 String path = new File(args[0]).toURL().toString();
ohair@286 556 if (args[0].endsWith(".rng")) {
ohair@286 557 p = new SAXParseable(new InputSource(path), eh);
ohair@286 558 } else {
ohair@286 559 p = new CompactParseable(new InputSource(path), eh);
ohair@286 560 }
ohair@286 561
ohair@286 562 // the error handler passed to CheckingSchemaBuilder will receive additional
ohair@286 563 // errors found during the RELAX NG restrictions check.
ohair@286 564 // typically you'd want to pass in the same error handler,
ohair@286 565 // as there's really no distinction between those two kinds of errors.
ohair@286 566 SchemaBuilder sb = new CheckingSchemaBuilder(new DSchemaBuilderImpl(), eh);
ohair@286 567 try {
ohair@286 568 // run the parser
ohair@286 569 DGrammarPattern grammar = (DGrammarPattern) p.parse(sb);
ohair@286 570 OutputStream out = new FileOutputStream(args[1]);
ohair@286 571 XMLOutputFactory factory = XMLOutputFactory.newInstance();
ohair@286 572 factory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE);
ohair@286 573 XMLStreamWriter output = factory.createXMLStreamWriter(out);
ohair@286 574 DXMLPrinter printer = new DXMLPrinter(output);
ohair@286 575 printer.printDocument(grammar);
ohair@286 576 output.close();
ohair@286 577 out.close();
ohair@286 578 } catch (BuildException e) {
ohair@286 579 if (e.getCause() instanceof SAXParseException) {
ohair@286 580 SAXParseException se = (SAXParseException) e.getCause();
ohair@286 581 System.out.println("("
ohair@286 582 + se.getLineNumber()
ohair@286 583 + ","
ohair@286 584 + se.getColumnNumber()
ohair@286 585 + "): "
ohair@286 586 + se.getMessage());
ohair@286 587 return;
ohair@286 588 } else
ohair@286 589 // I found that Crimson doesn't show the proper stack trace
ohair@286 590 // when a RuntimeException happens inside a SchemaBuilder.
ohair@286 591 // the following code shows the actual exception that happened.
ohair@286 592 if (e.getCause() instanceof SAXException) {
ohair@286 593 SAXException se = (SAXException) e.getCause();
ohair@286 594 if (se.getException() != null)
ohair@286 595 se.getException().printStackTrace();
ohair@286 596 }
ohair@286 597 throw e;
ohair@286 598 }
ohair@286 599 }
ohair@286 600 }

mercurial