Tue, 06 Mar 2012 16:09:35 -0800
7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom
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) { |
ohair@286 | 113 | throw (XMLStreamException) e.getCause(); |
ohair@286 | 114 | } |
ohair@286 | 115 | } |
ohair@286 | 116 | |
ohair@286 | 117 | /** |
ohair@286 | 118 | * Prints XML fragment for the given pattern. |
ohair@286 | 119 | * |
ohair@286 | 120 | * @throws XMLStreamException |
ohair@286 | 121 | */ |
ohair@286 | 122 | public void print(DPattern pattern) throws XMLStreamException { |
ohair@286 | 123 | try { |
ohair@286 | 124 | pattern.accept(visitor); |
ohair@286 | 125 | } catch (XMLWriterException e) { |
ohair@286 | 126 | throw (XMLStreamException) e.getCause(); |
ohair@286 | 127 | } |
ohair@286 | 128 | } |
ohair@286 | 129 | |
ohair@286 | 130 | /** |
ohair@286 | 131 | * Prints XML fragment for the given name class. |
ohair@286 | 132 | * |
ohair@286 | 133 | * @throws XMLStreamException |
ohair@286 | 134 | */ |
ohair@286 | 135 | public void print(NameClass nc) throws XMLStreamException { |
ohair@286 | 136 | try { |
ohair@286 | 137 | nc.accept(ncVisitor); |
ohair@286 | 138 | } catch (XMLWriterException e) { |
ohair@286 | 139 | throw (XMLStreamException) e.getCause(); |
ohair@286 | 140 | } |
ohair@286 | 141 | } |
ohair@286 | 142 | |
ohair@286 | 143 | public void print(Node node) throws XMLStreamException { |
ohair@286 | 144 | domPrinter.print(node); |
ohair@286 | 145 | } |
ohair@286 | 146 | |
ohair@286 | 147 | protected class XMLWriterException extends RuntimeException { |
ohair@286 | 148 | protected XMLWriterException(Throwable cause) { |
ohair@286 | 149 | super(cause); |
ohair@286 | 150 | } |
ohair@286 | 151 | } |
ohair@286 | 152 | |
ohair@286 | 153 | protected class XMLWriter { |
ohair@286 | 154 | protected void newLine() { |
ohair@286 | 155 | try { |
ohair@286 | 156 | out.writeCharacters(newLine); |
ohair@286 | 157 | } catch (XMLStreamException e) { |
ohair@286 | 158 | throw new XMLWriterException(e); |
ohair@286 | 159 | } |
ohair@286 | 160 | } |
ohair@286 | 161 | |
ohair@286 | 162 | protected void indent() { |
ohair@286 | 163 | try { |
ohair@286 | 164 | for (int i = 0; i < indent; i++) { |
ohair@286 | 165 | out.writeCharacters(indentStep); |
ohair@286 | 166 | } |
ohair@286 | 167 | } catch (XMLStreamException e) { |
ohair@286 | 168 | throw new XMLWriterException(e); |
ohair@286 | 169 | } |
ohair@286 | 170 | } |
ohair@286 | 171 | |
ohair@286 | 172 | public void startDocument() { |
ohair@286 | 173 | try { |
ohair@286 | 174 | out.writeStartDocument(); |
ohair@286 | 175 | } catch (XMLStreamException e) { |
ohair@286 | 176 | throw new XMLWriterException(e); |
ohair@286 | 177 | } |
ohair@286 | 178 | } |
ohair@286 | 179 | |
ohair@286 | 180 | public void endDocument() { |
ohair@286 | 181 | try { |
ohair@286 | 182 | out.writeEndDocument(); |
ohair@286 | 183 | } catch (XMLStreamException e) { |
ohair@286 | 184 | throw new XMLWriterException(e); |
ohair@286 | 185 | } |
ohair@286 | 186 | } |
ohair@286 | 187 | |
ohair@286 | 188 | public final void start(String element) { |
ohair@286 | 189 | try { |
ohair@286 | 190 | newLine(); |
ohair@286 | 191 | indent(); |
ohair@286 | 192 | out.writeStartElement(element); |
ohair@286 | 193 | indent++; |
ohair@286 | 194 | afterEnd = false; |
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 void end() { |
ohair@286 | 201 | try { |
ohair@286 | 202 | indent--; |
ohair@286 | 203 | if (afterEnd) { |
ohair@286 | 204 | newLine(); |
ohair@286 | 205 | indent(); |
ohair@286 | 206 | } |
ohair@286 | 207 | out.writeEndElement(); |
ohair@286 | 208 | afterEnd = true; |
ohair@286 | 209 | } catch (XMLStreamException e) { |
ohair@286 | 210 | throw new XMLWriterException(e); |
ohair@286 | 211 | } |
ohair@286 | 212 | } |
ohair@286 | 213 | |
ohair@286 | 214 | public void attr(String prefix, String ns, String name, String value) { |
ohair@286 | 215 | try { |
ohair@286 | 216 | out.writeAttribute(prefix, ns, name, value); |
ohair@286 | 217 | } catch (XMLStreamException e) { |
ohair@286 | 218 | throw new XMLWriterException(e); |
ohair@286 | 219 | } |
ohair@286 | 220 | } |
ohair@286 | 221 | |
ohair@286 | 222 | public void attr(String name, String value) { |
ohair@286 | 223 | try { |
ohair@286 | 224 | out.writeAttribute(name, value); |
ohair@286 | 225 | } catch (XMLStreamException e) { |
ohair@286 | 226 | throw new XMLWriterException(e); |
ohair@286 | 227 | } |
ohair@286 | 228 | } |
ohair@286 | 229 | |
ohair@286 | 230 | public void ns(String prefix, String uri) { |
ohair@286 | 231 | try { |
ohair@286 | 232 | out.writeNamespace(prefix, uri); |
ohair@286 | 233 | } catch (XMLStreamException e) { |
ohair@286 | 234 | throw new XMLWriterException(e); |
ohair@286 | 235 | } |
ohair@286 | 236 | } |
ohair@286 | 237 | |
ohair@286 | 238 | public void body(String text) { |
ohair@286 | 239 | try { |
ohair@286 | 240 | out.writeCharacters(text); |
ohair@286 | 241 | afterEnd = false; |
ohair@286 | 242 | } catch (XMLStreamException e) { |
ohair@286 | 243 | throw new XMLWriterException(e); |
ohair@286 | 244 | } |
ohair@286 | 245 | } |
ohair@286 | 246 | } |
ohair@286 | 247 | |
ohair@286 | 248 | protected class DXMLPrinterVisitor extends XMLWriter implements DPatternVisitor<Void> { |
ohair@286 | 249 | protected void on(DPattern p) { |
ohair@286 | 250 | p.accept(this); |
ohair@286 | 251 | } |
ohair@286 | 252 | |
ohair@286 | 253 | protected void unwrapGroup(DPattern p) { |
ohair@286 | 254 | if (p instanceof DGroupPattern && p.getAnnotation() == DAnnotation.EMPTY) { |
ohair@286 | 255 | for (DPattern d : (DGroupPattern) p) { |
ohair@286 | 256 | on(d); |
ohair@286 | 257 | } |
ohair@286 | 258 | } else { |
ohair@286 | 259 | on(p); |
ohair@286 | 260 | } |
ohair@286 | 261 | } |
ohair@286 | 262 | |
ohair@286 | 263 | protected void unwrapChoice(DPattern p) { |
ohair@286 | 264 | if (p instanceof DChoicePattern && p.getAnnotation() == DAnnotation.EMPTY) { |
ohair@286 | 265 | for (DPattern d : (DChoicePattern) p) { |
ohair@286 | 266 | on(d); |
ohair@286 | 267 | } |
ohair@286 | 268 | } else { |
ohair@286 | 269 | on(p); |
ohair@286 | 270 | } |
ohair@286 | 271 | } |
ohair@286 | 272 | |
ohair@286 | 273 | protected void on(NameClass nc) { |
ohair@286 | 274 | if (nc instanceof SimpleNameClass) { |
ohair@286 | 275 | QName qname = ((SimpleNameClass) nc).name; |
ohair@286 | 276 | String name = qname.getLocalPart(); |
ohair@286 | 277 | if (!qname.getPrefix().equals("")) name = qname.getPrefix() + ":"; |
ohair@286 | 278 | attr("name", name); |
ohair@286 | 279 | } else { |
ohair@286 | 280 | nc.accept(ncVisitor); |
ohair@286 | 281 | } |
ohair@286 | 282 | } |
ohair@286 | 283 | |
ohair@286 | 284 | protected void on(DAnnotation ann) { |
ohair@286 | 285 | if (ann == DAnnotation.EMPTY) return; |
ohair@286 | 286 | for (DAnnotation.Attribute attr : ann.getAttributes().values()) { |
ohair@286 | 287 | attr(attr.getPrefix(), attr.getNs(), attr.getLocalName(), attr.getValue()); |
ohair@286 | 288 | } |
ohair@286 | 289 | for (Element elem : ann.getChildren()) { |
ohair@286 | 290 | try { |
ohair@286 | 291 | newLine(); |
ohair@286 | 292 | indent(); |
ohair@286 | 293 | print(elem); |
ohair@286 | 294 | } |
ohair@286 | 295 | catch (XMLStreamException e) { |
ohair@286 | 296 | throw new XMLWriterException(e); |
ohair@286 | 297 | } |
ohair@286 | 298 | } |
ohair@286 | 299 | } |
ohair@286 | 300 | |
ohair@286 | 301 | public Void onAttribute(DAttributePattern p) { |
ohair@286 | 302 | start("attribute"); |
ohair@286 | 303 | on(p.getName()); |
ohair@286 | 304 | on(p.getAnnotation()); |
ohair@286 | 305 | DPattern child = p.getChild(); |
ohair@286 | 306 | // do not print default value |
ohair@286 | 307 | if (!(child instanceof DTextPattern)) { |
ohair@286 | 308 | on(p.getChild()); |
ohair@286 | 309 | } |
ohair@286 | 310 | end(); |
ohair@286 | 311 | return null; |
ohair@286 | 312 | } |
ohair@286 | 313 | |
ohair@286 | 314 | public Void onChoice(DChoicePattern p) { |
ohair@286 | 315 | start("choice"); |
ohair@286 | 316 | on(p.getAnnotation()); |
ohair@286 | 317 | for (DPattern d : p) { |
ohair@286 | 318 | on(d); |
ohair@286 | 319 | } |
ohair@286 | 320 | end(); |
ohair@286 | 321 | return null; |
ohair@286 | 322 | } |
ohair@286 | 323 | |
ohair@286 | 324 | public Void onData(DDataPattern p) { |
ohair@286 | 325 | List<DDataPattern.Param> params = p.getParams(); |
ohair@286 | 326 | DPattern except = p.getExcept(); |
ohair@286 | 327 | start("data"); |
ohair@286 | 328 | attr("datatypeLibrary", p.getDatatypeLibrary()); |
ohair@286 | 329 | attr("type", p.getType()); |
ohair@286 | 330 | on(p.getAnnotation()); |
ohair@286 | 331 | for (DDataPattern.Param param : params) { |
ohair@286 | 332 | start("param"); |
ohair@286 | 333 | attr("ns", param.getNs()); |
ohair@286 | 334 | attr("name", param.getName()); |
ohair@286 | 335 | body(param.getValue()); |
ohair@286 | 336 | end(); |
ohair@286 | 337 | } |
ohair@286 | 338 | if (except != null) { |
ohair@286 | 339 | start("except"); |
ohair@286 | 340 | unwrapChoice(except); |
ohair@286 | 341 | end(); |
ohair@286 | 342 | } |
ohair@286 | 343 | end(); |
ohair@286 | 344 | return null; |
ohair@286 | 345 | } |
ohair@286 | 346 | |
ohair@286 | 347 | public Void onElement(DElementPattern p) { |
ohair@286 | 348 | start("element"); |
ohair@286 | 349 | on(p.getName()); |
ohair@286 | 350 | on(p.getAnnotation()); |
ohair@286 | 351 | unwrapGroup(p.getChild()); |
ohair@286 | 352 | end(); |
ohair@286 | 353 | return null; |
ohair@286 | 354 | } |
ohair@286 | 355 | |
ohair@286 | 356 | public Void onEmpty(DEmptyPattern p) { |
ohair@286 | 357 | start("empty"); |
ohair@286 | 358 | on(p.getAnnotation()); |
ohair@286 | 359 | end(); |
ohair@286 | 360 | return null; |
ohair@286 | 361 | } |
ohair@286 | 362 | |
ohair@286 | 363 | public Void onGrammar(DGrammarPattern p) { |
ohair@286 | 364 | start("grammar"); |
ohair@286 | 365 | ns(null, WellKnownNamespaces.RELAX_NG); |
ohair@286 | 366 | on(p.getAnnotation()); |
ohair@286 | 367 | start("start"); |
ohair@286 | 368 | on(p.getStart()); |
ohair@286 | 369 | end(); |
ohair@286 | 370 | for (DDefine d : p) { |
ohair@286 | 371 | start("define"); |
ohair@286 | 372 | attr("name", d.getName()); |
ohair@286 | 373 | on(d.getAnnotation()); |
ohair@286 | 374 | unwrapGroup(d.getPattern()); |
ohair@286 | 375 | end(); |
ohair@286 | 376 | } |
ohair@286 | 377 | end(); |
ohair@286 | 378 | return null; |
ohair@286 | 379 | } |
ohair@286 | 380 | |
ohair@286 | 381 | public Void onGroup(DGroupPattern p) { |
ohair@286 | 382 | start("group"); |
ohair@286 | 383 | on(p.getAnnotation()); |
ohair@286 | 384 | for (DPattern d : p) { |
ohair@286 | 385 | on(d); |
ohair@286 | 386 | } |
ohair@286 | 387 | end(); |
ohair@286 | 388 | return null; |
ohair@286 | 389 | } |
ohair@286 | 390 | |
ohair@286 | 391 | public Void onInterleave(DInterleavePattern p) { |
ohair@286 | 392 | start("interleave"); |
ohair@286 | 393 | on(p.getAnnotation()); |
ohair@286 | 394 | for (DPattern d : p) { |
ohair@286 | 395 | on(d); |
ohair@286 | 396 | } |
ohair@286 | 397 | end(); |
ohair@286 | 398 | return null; |
ohair@286 | 399 | } |
ohair@286 | 400 | |
ohair@286 | 401 | public Void onList(DListPattern p) { |
ohair@286 | 402 | start("list"); |
ohair@286 | 403 | on(p.getAnnotation()); |
ohair@286 | 404 | unwrapGroup(p.getChild()); |
ohair@286 | 405 | end(); |
ohair@286 | 406 | return null; |
ohair@286 | 407 | } |
ohair@286 | 408 | |
ohair@286 | 409 | public Void onMixed(DMixedPattern p) { |
ohair@286 | 410 | start("mixed"); |
ohair@286 | 411 | on(p.getAnnotation()); |
ohair@286 | 412 | unwrapGroup(p.getChild()); |
ohair@286 | 413 | end(); |
ohair@286 | 414 | return null; |
ohair@286 | 415 | } |
ohair@286 | 416 | |
ohair@286 | 417 | public Void onNotAllowed(DNotAllowedPattern p) { |
ohair@286 | 418 | start("notAllowed"); |
ohair@286 | 419 | on(p.getAnnotation()); |
ohair@286 | 420 | end(); |
ohair@286 | 421 | return null; |
ohair@286 | 422 | } |
ohair@286 | 423 | |
ohair@286 | 424 | public Void onOneOrMore(DOneOrMorePattern p) { |
ohair@286 | 425 | start("oneOrMore"); |
ohair@286 | 426 | on(p.getAnnotation()); |
ohair@286 | 427 | unwrapGroup(p.getChild()); |
ohair@286 | 428 | end(); |
ohair@286 | 429 | return null; |
ohair@286 | 430 | } |
ohair@286 | 431 | |
ohair@286 | 432 | public Void onOptional(DOptionalPattern p) { |
ohair@286 | 433 | start("optional"); |
ohair@286 | 434 | on(p.getAnnotation()); |
ohair@286 | 435 | unwrapGroup(p.getChild()); |
ohair@286 | 436 | end(); |
ohair@286 | 437 | return null; |
ohair@286 | 438 | } |
ohair@286 | 439 | |
ohair@286 | 440 | public Void onRef(DRefPattern p) { |
ohair@286 | 441 | start("ref"); |
ohair@286 | 442 | attr("name", p.getName()); |
ohair@286 | 443 | on(p.getAnnotation()); |
ohair@286 | 444 | end(); |
ohair@286 | 445 | return null; |
ohair@286 | 446 | } |
ohair@286 | 447 | |
ohair@286 | 448 | public Void onText(DTextPattern p) { |
ohair@286 | 449 | start("text"); |
ohair@286 | 450 | on(p.getAnnotation()); |
ohair@286 | 451 | end(); |
ohair@286 | 452 | return null; |
ohair@286 | 453 | } |
ohair@286 | 454 | |
ohair@286 | 455 | public Void onValue(DValuePattern p) { |
ohair@286 | 456 | start("value"); |
ohair@286 | 457 | if (!p.getNs().equals("")) attr("ns", p.getNs()); |
ohair@286 | 458 | attr("datatypeLibrary", p.getDatatypeLibrary()); |
ohair@286 | 459 | attr("type", p.getType()); |
ohair@286 | 460 | on(p.getAnnotation()); |
ohair@286 | 461 | body(p.getValue()); |
ohair@286 | 462 | end(); |
ohair@286 | 463 | return null; |
ohair@286 | 464 | } |
ohair@286 | 465 | |
ohair@286 | 466 | public Void onZeroOrMore(DZeroOrMorePattern p) { |
ohair@286 | 467 | start("zeroOrMore"); |
ohair@286 | 468 | on(p.getAnnotation()); |
ohair@286 | 469 | unwrapGroup(p.getChild()); |
ohair@286 | 470 | end(); |
ohair@286 | 471 | return null; |
ohair@286 | 472 | } |
ohair@286 | 473 | } |
ohair@286 | 474 | |
ohair@286 | 475 | protected class NameClassXMLPrinterVisitor extends XMLWriter implements NameClassVisitor<Void> { |
ohair@286 | 476 | public Void visitChoice(NameClass nc1, NameClass nc2) { |
ohair@286 | 477 | // TODO: flatten nested choices |
ohair@286 | 478 | start("choice"); |
ohair@286 | 479 | nc1.accept(this); |
ohair@286 | 480 | nc2.accept(this); |
ohair@286 | 481 | end(); |
ohair@286 | 482 | return null; |
ohair@286 | 483 | } |
ohair@286 | 484 | |
ohair@286 | 485 | public Void visitNsName(String ns) { |
ohair@286 | 486 | start("nsName"); |
ohair@286 | 487 | attr("ns", ns); |
ohair@286 | 488 | end(); |
ohair@286 | 489 | return null; |
ohair@286 | 490 | } |
ohair@286 | 491 | |
ohair@286 | 492 | public Void visitNsNameExcept(String ns, NameClass nc) { |
ohair@286 | 493 | start("nsName"); |
ohair@286 | 494 | attr("ns", ns); |
ohair@286 | 495 | start("except"); |
ohair@286 | 496 | nc.accept(this); |
ohair@286 | 497 | end(); |
ohair@286 | 498 | end(); |
ohair@286 | 499 | return null; |
ohair@286 | 500 | } |
ohair@286 | 501 | |
ohair@286 | 502 | public Void visitAnyName() { |
ohair@286 | 503 | start("anyName"); |
ohair@286 | 504 | end(); |
ohair@286 | 505 | return null; |
ohair@286 | 506 | } |
ohair@286 | 507 | |
ohair@286 | 508 | public Void visitAnyNameExcept(NameClass nc) { |
ohair@286 | 509 | start("anyName"); |
ohair@286 | 510 | start("except"); |
ohair@286 | 511 | nc.accept(this); |
ohair@286 | 512 | end(); |
ohair@286 | 513 | end(); |
ohair@286 | 514 | return null; |
ohair@286 | 515 | } |
ohair@286 | 516 | |
ohair@286 | 517 | public Void visitName(QName name) { |
ohair@286 | 518 | start("name"); |
ohair@286 | 519 | if (!name.getPrefix().equals("")) { |
ohair@286 | 520 | body(name.getPrefix() + ":"); |
ohair@286 | 521 | } |
ohair@286 | 522 | body(name.getLocalPart()); |
ohair@286 | 523 | end(); |
ohair@286 | 524 | return null; |
ohair@286 | 525 | } |
ohair@286 | 526 | |
ohair@286 | 527 | public Void visitNull() { |
ohair@286 | 528 | throw new UnsupportedOperationException("visitNull"); |
ohair@286 | 529 | } |
ohair@286 | 530 | } |
ohair@286 | 531 | |
ohair@286 | 532 | public static void main(String[] args) throws Exception { |
ohair@286 | 533 | Parseable p; |
ohair@286 | 534 | |
ohair@286 | 535 | ErrorHandler eh = new DefaultHandler() { |
ohair@286 | 536 | @Override |
ohair@286 | 537 | public void error(SAXParseException e) throws SAXException { |
ohair@286 | 538 | throw e; |
ohair@286 | 539 | } |
ohair@286 | 540 | }; |
ohair@286 | 541 | |
ohair@286 | 542 | // the error handler passed to Parseable will receive parsing errors. |
ohair@286 | 543 | String path = new File(args[0]).toURL().toString(); |
ohair@286 | 544 | if (args[0].endsWith(".rng")) { |
ohair@286 | 545 | p = new SAXParseable(new InputSource(path), eh); |
ohair@286 | 546 | } else { |
ohair@286 | 547 | p = new CompactParseable(new InputSource(path), eh); |
ohair@286 | 548 | } |
ohair@286 | 549 | |
ohair@286 | 550 | // the error handler passed to CheckingSchemaBuilder will receive additional |
ohair@286 | 551 | // errors found during the RELAX NG restrictions check. |
ohair@286 | 552 | // typically you'd want to pass in the same error handler, |
ohair@286 | 553 | // as there's really no distinction between those two kinds of errors. |
ohair@286 | 554 | SchemaBuilder sb = new CheckingSchemaBuilder(new DSchemaBuilderImpl(), eh); |
ohair@286 | 555 | try { |
ohair@286 | 556 | // run the parser |
ohair@286 | 557 | DGrammarPattern grammar = (DGrammarPattern) p.parse(sb); |
ohair@286 | 558 | OutputStream out = new FileOutputStream(args[1]); |
ohair@286 | 559 | XMLOutputFactory factory = XMLOutputFactory.newInstance(); |
ohair@286 | 560 | factory.setProperty("javax.xml.stream.isRepairingNamespaces", Boolean.TRUE); |
ohair@286 | 561 | XMLStreamWriter output = factory.createXMLStreamWriter(out); |
ohair@286 | 562 | DXMLPrinter printer = new DXMLPrinter(output); |
ohair@286 | 563 | printer.printDocument(grammar); |
ohair@286 | 564 | output.close(); |
ohair@286 | 565 | out.close(); |
ohair@286 | 566 | } catch (BuildException e) { |
ohair@286 | 567 | if (e.getCause() instanceof SAXParseException) { |
ohair@286 | 568 | SAXParseException se = (SAXParseException) e.getCause(); |
ohair@286 | 569 | System.out.println("(" |
ohair@286 | 570 | + se.getLineNumber() |
ohair@286 | 571 | + "," |
ohair@286 | 572 | + se.getColumnNumber() |
ohair@286 | 573 | + "): " |
ohair@286 | 574 | + se.getMessage()); |
ohair@286 | 575 | return; |
ohair@286 | 576 | } else |
ohair@286 | 577 | // I found that Crimson doesn't show the proper stack trace |
ohair@286 | 578 | // when a RuntimeException happens inside a SchemaBuilder. |
ohair@286 | 579 | // the following code shows the actual exception that happened. |
ohair@286 | 580 | if (e.getCause() instanceof SAXException) { |
ohair@286 | 581 | SAXException se = (SAXException) e.getCause(); |
ohair@286 | 582 | if (se.getException() != null) |
ohair@286 | 583 | se.getException().printStackTrace(); |
ohair@286 | 584 | } |
ohair@286 | 585 | throw e; |
ohair@286 | 586 | } |
ohair@286 | 587 | } |
ohair@286 | 588 | } |