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.sax.SAXDocumentParser;
043: import com.sun.xml.fastinfoset.sax.Properties;
044: import java.io.BufferedInputStream;
045: import java.io.InputStream;
046: import java.io.OutputStream;
047: import javax.xml.parsers.SAXParser;
048: import javax.xml.parsers.SAXParserFactory;
049: import org.xml.sax.InputSource;
050: import org.xml.sax.XMLReader;
051:
052: public class FI_SAX_Or_XML_SAX_SAXEvent extends TransformInputOutput {
053:
054: public FI_SAX_Or_XML_SAX_SAXEvent() {
055: }
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: if (isFastInfosetDocument) {
069: SAXDocumentParser parser = new SAXDocumentParser();
070: SAXEventSerializer ses = new SAXEventSerializer(events);
071: parser.setContentHandler(ses);
072: parser
073: .setProperty(Properties.LEXICAL_HANDLER_PROPERTY,
074: ses);
075: parser.parse(document);
076: } else {
077: SAXParserFactory parserFactory = SAXParserFactory
078: .newInstance();
079: parserFactory.setNamespaceAware(true);
080: SAXParser parser = parserFactory.newSAXParser();
081: SAXEventSerializer ses = new SAXEventSerializer(events);
082:
083: XMLReader reader = parser.getXMLReader();
084: reader.setProperty(
085: "http://xml.org/sax/properties/lexical-handler",
086: ses);
087: reader.setContentHandler(ses);
088: if (workingDirectory != null) {
089: reader
090: .setEntityResolver(createRelativePathResolver(workingDirectory));
091: }
092: reader.parse(new InputSource(document));
093: }
094: }
095:
096: public void parse(InputStream document, OutputStream events)
097: throws Exception {
098: parse(document, events, null);
099: }
100:
101: public static void main(String[] args) throws Exception {
102: FI_SAX_Or_XML_SAX_SAXEvent p = new FI_SAX_Or_XML_SAX_SAXEvent();
103: p.parse(args);
104: }
105: }
|