📄 documentwriter.java
字号:
/** * Copyright 2004 Carlos Silva A. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.csa.lib.xml.dom;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.util.Enumeration;import uk.co.wilson.xml.MinML;/** * DocumentWriter * * dp.parse( new InputStreamReader(new BufferedInputStream(System.in, 1024)); * * No soporta datos mezclados (elementos + texto) y solo procesa UTF-8 * <p>$Date: 2004/12/05 04:54:34 $</p> * @version $Revision: 1.2 $ * @author Carlos Silva */public class DocumentWriter extends MinML { PrintWriter out; Node e; public DocumentWriter(Node e) { this.e = e; } public void write(File out) throws IOException { FileOutputStream fos = new FileOutputStream(out); write(fos); fos.close(); } public void write(OutputStream os) throws IOException { OutputStreamWriter osw = new OutputStreamWriter(os, "GB2312"); PrintWriter pw = new PrintWriter(osw); write(pw); pw.flush(); osw.flush(); } public void write(PrintWriter pw) { out = pw; out.println("<?xml version=\"1.0\" encoding=\"GB2312\"?>"); write(e); } void write(Node e) { out.print("<"); out.print(e.name); if (e.attributes != null) for (Enumeration i = e.attributes.keys(); i.hasMoreElements();) { // out.println("Begin ---------------"); String key = (String) i.nextElement(); String value = (String) e.attributes.get(key); out.print(" "); out.print(key); out.print("=\""); out.print(xmlize(value)); out.print("\""); // out.println("End ---------------"); } if ((e.childs == null) && (e.getValue() == null)) out.println("/>"); else { out.print(">"); if (e.childs != null) { out.println(); for (int i = 0; i < e.childs.size(); i++) { Node c = (Node) e.childs.get(i); write(c); } } else out.print(xmlize(e.getValue())); out.print("</"); out.print(e.name); out.println(">"); } } static String xs = "&<>"; static String xss[] = {"&","<",">"}; static String xmlize(String s){ StringBuffer sb = new StringBuffer(); for (int i=0; i<s.length();i++) { char c = s.charAt(i); int p = xs.indexOf(c); if (p>=0) { sb.append(xss[p]); }else sb.append(c); } return sb.toString(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -