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.File;
019: import java.io.FileOutputStream;
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import java.io.OutputStreamWriter;
023: import java.io.PrintWriter;
024: import java.util.Enumeration;
025:
026: import uk.co.wilson.xml.MinML;
027:
028: /**
029: * DocumentWriter
030: *
031: * dp.parse( new InputStreamReader(new BufferedInputStream(System.in, 1024));
032: *
033: * No soporta datos mezclados (elementos + texto) y solo procesa UTF-8
034: * <p>$Date: 2006/09/14 08:14:17 $</p>
035: * @version $Revision: 1.3 $
036: * @author Carlos Silva
037: */
038: public class DocumentWriter extends MinML {
039: PrintWriter out;
040: Node e;
041:
042: public DocumentWriter(Node e) {
043: this .e = e;
044: }
045:
046: public void write(File outFile) throws IOException {
047: FileOutputStream fos = new FileOutputStream(outFile);
048: write(fos);
049: fos.close();
050: }
051:
052: public void write(OutputStream os) throws IOException {
053: OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
054: PrintWriter pw = new PrintWriter(osw);
055: write(pw);
056: pw.flush();
057: osw.flush();
058: }
059:
060: public void write(PrintWriter pw) {
061: out = pw;
062: out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
063: write(e);
064: }
065:
066: void write(Node node) {
067: out.print("<");
068: out.print(node.name);
069: if (node.attributes != null)
070: for (Enumeration i = node.attributes.keys(); i
071: .hasMoreElements();) {
072: String key = (String) i.nextElement();
073: String value = (String) node.attributes.get(key);
074: out.print(" ");
075: out.print(key);
076: out.print("=\"");
077: out.print(xmlize(value));
078: out.print("\"");
079: }
080: if ((node.childs == null) && (node.getValue() == null))
081: out.println("/>");
082: else {
083: out.print(">");
084: if (node.childs != null) {
085: out.println();
086: for (int i = 0; i < node.childs.size(); i++) {
087: Node c = (Node) node.childs.get(i);
088: write(c);
089: }
090: } else
091: out.print(xmlize(node.getValue()));
092: out.print("</");
093: out.print(node.name);
094: out.println(">");
095: }
096: }
097:
098: static String xs = "&<>";
099: static String xss[] = { "&", "<", ">" };
100:
101: static String xmlize(String s) {
102: StringBuffer sb = new StringBuffer();
103: for (int i = 0; i < s.length(); i++) {
104: char c = s.charAt(i);
105: int p = xs.indexOf(c);
106: if (p >= 0) {
107: sb.append(xss[p]);
108: } else
109: sb.append(c);
110: }
111: return sb.toString();
112: }
113: }
|