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.sax.SAXDocumentSerializer;
042: import java.io.InputStream;
043: import java.io.OutputStream;
044: import javax.xml.parsers.SAXParser;
045: import javax.xml.parsers.SAXParserFactory;
046: import java.io.Reader;
047: import org.xml.sax.InputSource;
048: import org.xml.sax.XMLReader;
049:
050: public class XML_SAX_FI extends TransformInputOutput {
051:
052: public XML_SAX_FI() {
053: }
054:
055: public void parse(InputStream xml, OutputStream finf,
056: String workingDirectory) throws Exception {
057: SAXParser saxParser = getParser();
058: SAXDocumentSerializer documentSerializer = getSerializer(finf);
059:
060: XMLReader reader = saxParser.getXMLReader();
061: reader.setProperty(
062: "http://xml.org/sax/properties/lexical-handler",
063: documentSerializer);
064: reader.setContentHandler(documentSerializer);
065:
066: if (workingDirectory != null) {
067: reader
068: .setEntityResolver(createRelativePathResolver(workingDirectory));
069: }
070: reader.parse(new InputSource(xml));
071: }
072:
073: public void parse(InputStream xml, OutputStream finf)
074: throws Exception {
075: parse(xml, finf, null);
076: }
077:
078: public void convert(Reader reader, OutputStream finf)
079: throws Exception {
080: InputSource is = new InputSource(reader);
081:
082: SAXParser saxParser = getParser();
083: SAXDocumentSerializer documentSerializer = getSerializer(finf);
084:
085: saxParser.setProperty(
086: "http://xml.org/sax/properties/lexical-handler",
087: documentSerializer);
088: saxParser.parse(is, documentSerializer);
089: }
090:
091: private SAXParser getParser() {
092: SAXParserFactory saxParserFactory = SAXParserFactory
093: .newInstance();
094: saxParserFactory.setNamespaceAware(true);
095: try {
096: return saxParserFactory.newSAXParser();
097: } catch (Exception e) {
098: return null;
099: }
100: }
101:
102: private SAXDocumentSerializer getSerializer(OutputStream finf) {
103: SAXDocumentSerializer documentSerializer = new SAXDocumentSerializer();
104: documentSerializer.setOutputStream(finf);
105: return documentSerializer;
106: }
107:
108: public static void main(String[] args) throws Exception {
109: XML_SAX_FI s = new XML_SAX_FI();
110: s.parse(args);
111: }
112: }
|