001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.tools;
040:
041: import com.sun.xml.fastinfoset.Decoder;
042: import com.sun.xml.fastinfoset.dom.DOMDocumentParser;
043: import java.io.BufferedInputStream;
044: import java.io.InputStream;
045: import java.io.OutputStream;
046: import javax.xml.parsers.DocumentBuilder;
047: import javax.xml.parsers.DocumentBuilderFactory;
048: import javax.xml.transform.Transformer;
049: import javax.xml.transform.TransformerFactory;
050: import javax.xml.transform.dom.DOMSource;
051: import javax.xml.transform.sax.SAXResult;
052: import org.w3c.dom.Document;
053:
054: public class FI_DOM_Or_XML_DOM_SAX_SAXEvent extends
055: TransformInputOutput {
056:
057: public void parse(InputStream document, OutputStream events,
058: String workingDirectory) throws Exception {
059: if (!document.markSupported()) {
060: document = new BufferedInputStream(document);
061: }
062:
063: document.mark(4);
064: boolean isFastInfosetDocument = Decoder
065: .isFastInfosetDocument(document);
066: document.reset();
067:
068: DocumentBuilderFactory dbf = DocumentBuilderFactory
069: .newInstance();
070: dbf.setNamespaceAware(true);
071: DocumentBuilder db = dbf.newDocumentBuilder();
072:
073: Document d;
074: if (isFastInfosetDocument) {
075: d = db.newDocument();
076: DOMDocumentParser ddp = new DOMDocumentParser();
077: ddp.parse(d, document);
078: } else {
079: if (workingDirectory != null) {
080: db
081: .setEntityResolver(createRelativePathResolver(workingDirectory));
082: }
083: d = db.parse(document);
084: }
085:
086: SAXEventSerializer ses = new SAXEventSerializer(events);
087:
088: TransformerFactory tf = TransformerFactory.newInstance();
089: Transformer t = tf.newTransformer();
090: t.transform(new DOMSource(d), new SAXResult(ses));
091: }
092:
093: public void parse(InputStream document, OutputStream events)
094: throws Exception {
095: parse(document, events, null);
096: }
097:
098: public static void main(String[] args) throws Exception {
099: FI_DOM_Or_XML_DOM_SAX_SAXEvent p = new FI_DOM_Or_XML_DOM_SAX_SAXEvent();
100: p.parse(args);
101: }
102:
103: }
|