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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial