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

Print this page




   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  * Copyright (C) 2004-2011
  27  *
  28  * Permission is hereby granted, free of charge, to any person obtaining a copy
  29  * of this software and associated documentation files (the "Software"), to deal
  30  * in the Software without restriction, including without limitation the rights
  31  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  32  * copies of the Software, and to permit persons to whom the Software is
  33  * furnished to do so, subject to the following conditions:
  34  *
  35  * The above copyright notice and this permission notice shall be included in
  36  * all copies or substantial portions of the Software.
  37  *
  38  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  39  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  40  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  42  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  43  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  44  * THE SOFTWARE.
  45  */
  46 package com.sun.xml.internal.rngom.digested;
  47 
  48 import java.io.File;
  49 import java.io.FileOutputStream;
  50 import java.io.OutputStream;
  51 import java.util.List;
  52 
  53 import javax.xml.namespace.QName;
  54 import javax.xml.stream.XMLOutputFactory;
  55 import javax.xml.stream.XMLStreamException;
  56 import javax.xml.stream.XMLStreamWriter;
  57 
  58 import com.sun.xml.internal.rngom.ast.builder.BuildException;
  59 import com.sun.xml.internal.rngom.ast.builder.SchemaBuilder;
  60 import com.sun.xml.internal.rngom.ast.util.CheckingSchemaBuilder;
  61 import com.sun.xml.internal.rngom.nc.NameClass;
  62 import com.sun.xml.internal.rngom.nc.NameClassVisitor;
  63 import com.sun.xml.internal.rngom.nc.SimpleNameClass;
  64 import com.sun.xml.internal.rngom.parse.Parseable;
  65 import com.sun.xml.internal.rngom.parse.compact.CompactParseable;
  66 import com.sun.xml.internal.rngom.parse.xml.SAXParseable;
  67 import com.sun.xml.internal.rngom.xml.util.WellKnownNamespaces;
  68 import org.w3c.dom.Element;


 516 
 517         public Void visitName(QName name) {
 518             start("name");
 519             if (!name.getPrefix().equals("")) {
 520                 body(name.getPrefix() + ":");
 521             }
 522             body(name.getLocalPart());
 523             end();
 524             return null;
 525         }
 526 
 527         public Void visitNull() {
 528             throw new UnsupportedOperationException("visitNull");
 529         }
 530     }
 531 
 532     public static void main(String[] args) throws Exception {
 533         Parseable p;
 534 
 535         ErrorHandler eh = new DefaultHandler() {
 536             @Override
 537             public void error(SAXParseException e) throws SAXException {
 538                 throw e;
 539             }
 540         };
 541 
 542         // the error handler passed to Parseable will receive parsing errors.
 543         String path = new File(args[0]).toURL().toString();
 544         if (args[0].endsWith(".rng")) {
 545             p = new SAXParseable(new InputSource(path), eh);
 546         } else {
 547             p = new CompactParseable(new InputSource(path), eh);
 548         }
 549 
 550         // the error handler passed to CheckingSchemaBuilder will receive additional
 551         // errors found during the RELAX NG restrictions check.
 552         // typically you'd want to pass in the same error handler,
 553         // as there's really no distinction between those two kinds of errors.
 554         SchemaBuilder sb = new CheckingSchemaBuilder(new DSchemaBuilderImpl(), eh);
 555         try {
 556             // run the parser
 557             DGrammarPattern grammar = (DGrammarPattern) p.parse(sb);
 558             OutputStream out = new FileOutputStream(args[1]);
 559             XMLOutputFactory factory = XMLOutputFactory.newInstance();
 560             factory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE);
 561             XMLStreamWriter output = factory.createXMLStreamWriter(out);
 562             DXMLPrinter printer = new DXMLPrinter(output);
 563             printer.printDocument(grammar);
 564             output.close();
 565             out.close();
 566         } catch (BuildException e) {
 567             if (e.getCause() instanceof SAXParseException) {
 568                 SAXParseException se = (SAXParseException) e.getCause();
 569                 System.out.println("("
 570                     + se.getLineNumber()
 571                     + ","
 572                     + se.getColumnNumber()
 573                     + "): "
 574                     + se.getMessage());
 575                 return;
 576             } else
 577                 // I found that Crimson doesn't show the proper stack trace
 578                 // when a RuntimeException happens inside a SchemaBuilder.
 579                 // the following code shows the actual exception that happened.
 580                 if (e.getCause() instanceof SAXException) {


   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.xml.internal.rngom.digested;
  27 

  28 import java.io.FileOutputStream;
  29 import java.io.OutputStream;
  30 import java.util.List;
  31 
  32 import javax.xml.namespace.QName;
  33 import javax.xml.stream.XMLOutputFactory;
  34 import javax.xml.stream.XMLStreamException;
  35 import javax.xml.stream.XMLStreamWriter;
  36 
  37 import com.sun.xml.internal.rngom.ast.builder.BuildException;
  38 import com.sun.xml.internal.rngom.ast.builder.SchemaBuilder;
  39 import com.sun.xml.internal.rngom.ast.util.CheckingSchemaBuilder;
  40 import com.sun.xml.internal.rngom.nc.NameClass;
  41 import com.sun.xml.internal.rngom.nc.NameClassVisitor;
  42 import com.sun.xml.internal.rngom.nc.SimpleNameClass;
  43 import com.sun.xml.internal.rngom.parse.Parseable;
  44 import com.sun.xml.internal.rngom.parse.compact.CompactParseable;
  45 import com.sun.xml.internal.rngom.parse.xml.SAXParseable;
  46 import com.sun.xml.internal.rngom.xml.util.WellKnownNamespaces;
  47 import org.w3c.dom.Element;


 495 
 496         public Void visitName(QName name) {
 497             start("name");
 498             if (!name.getPrefix().equals("")) {
 499                 body(name.getPrefix() + ":");
 500             }
 501             body(name.getLocalPart());
 502             end();
 503             return null;
 504         }
 505 
 506         public Void visitNull() {
 507             throw new UnsupportedOperationException("visitNull");
 508         }
 509     }
 510 
 511     public static void main(String[] args) throws Exception {
 512         Parseable p;
 513 
 514         ErrorHandler eh = new DefaultHandler() {

 515             public void error(SAXParseException e) throws SAXException {
 516                 throw e;
 517             }
 518         };
 519 
 520         // the error handler passed to Parseable will receive parsing errors.

 521         if (args[0].endsWith(".rng")) {
 522             p = new SAXParseable(new InputSource(args[0]), eh);
 523         } else {
 524             p = new CompactParseable(new InputSource(args[0]), eh);
 525         }
 526 
 527         // the error handler passed to CheckingSchemaBuilder will receive additional
 528         // errors found during the RELAX NG restrictions check.
 529         // typically you'd want to pass in the same error handler,
 530         // as there's really no distinction between those two kinds of errors.
 531         SchemaBuilder sb = new CheckingSchemaBuilder(new DSchemaBuilderImpl(), eh);
 532         try {
 533             // run the parser
 534             DGrammarPattern grammar = (DGrammarPattern) p.parse(sb);
 535             OutputStream out = new FileOutputStream(args[1]);
 536             XMLOutputFactory factory = XMLOutputFactory.newInstance();

 537             XMLStreamWriter output = factory.createXMLStreamWriter(out);
 538             DXMLPrinter printer = new DXMLPrinter(output);
 539             printer.printDocument(grammar);
 540             output.close();
 541             out.close();
 542         } catch (BuildException e) {
 543             if (e.getCause() instanceof SAXParseException) {
 544                 SAXParseException se = (SAXParseException) e.getCause();
 545                 System.out.println("("
 546                     + se.getLineNumber()
 547                     + ","
 548                     + se.getColumnNumber()
 549                     + "): "
 550                     + se.getMessage());
 551                 return;
 552             } else
 553                 // I found that Crimson doesn't show the proper stack trace
 554                 // when a RuntimeException happens inside a SchemaBuilder.
 555                 // the following code shows the actual exception that happened.
 556                 if (e.getCause() instanceof SAXException) {