001: /**
002: * Copyright 2004 Carlos Silva A.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */package com.csa.lib.xml.dom;
017:
018: import java.io.BufferedReader;
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024:
025: import org.xml.sax.AttributeList;
026: import org.xml.sax.SAXException;
027: import org.xml.sax.SAXParseException;
028:
029: import uk.co.wilson.xml.MinML;
030:
031: /**
032: * DocumentParser
033: *
034: * dp.parse( new InputStreamReader(new BufferedInputStream(System.in, 1024));
035: *
036: * No soporta datos mezclados (elementos + texto) y solo procesa UTF-8
037: * <p>$Date: 2004/12/05 04:06:07 $</p>
038: * @version $Revision: 1.1 $
039: * @author Carlos Silva
040: */
041: public class DocumentParser extends MinML {
042: Node mainElement;
043: Node currentElement;
044:
045: public void parse(File f) throws SAXException, IOException {
046: FileInputStream fis = new FileInputStream(f);
047: parse(fis);
048: fis.close();
049: }
050:
051: public void parse(InputStream fis) throws SAXException, IOException {
052: InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
053: BufferedReader br = new BufferedReader(isr);
054: parse(br);
055: br.close();
056: isr.close();
057: }
058:
059: public Node getMainElement() {
060: return mainElement;
061: }
062:
063: public void startDocument() {
064: currentElement = mainElement = new Node("DOCUMENT");
065:
066: }
067:
068: public void startElement(String name, AttributeList attributes) {
069: Node e = new Node(name, attributes);
070: currentElement.appendChild(e);
071: currentElement = e;
072: }
073:
074: public void endElement(String name) {
075: currentElement = currentElement.parent;
076: }
077:
078: public void characters(char ch[], int start, int length) {
079: currentElement.addValue(new String(ch, start, length));
080: }
081:
082: public void fatalError(SAXParseException e) throws SAXException {
083: throw e;
084: }
085:
086: /**
087: * Test lee y escribe un xml
088: * @param args
089: * @throws Exception
090: */
091: public static void main(String args[]) throws Exception {
092: DocumentParser dp = new DocumentParser();
093: dp.parse(new File("c:\\project.xml"));
094:
095: Node main = dp.getMainElement();
096:
097: DocumentWriter dw = new DocumentWriter(main);
098: dw.write(new File("c:\\project2.xml"));
099: }
100: }
|