xmloutput.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 165 行
JAVA
165 行
/**
* $Id:XMLOutput.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfdom;
import java.util.Iterator;
import java.util.List;
import java.io.PrintWriter;
import java.io.FileWriter;
import com.jfimagine.utils.log.*;
/**
* XMLOutput class. A class used to output xml document.
*
* @author CookieMaker
*
* @version $Revision: 1.01 $
*/
public class XMLOutput{
/**an internal log utility*/
private static JFLogger m_logger =JFLogManager.getLogger("com.jfimagine.jfdom.XMLOutPut");
/**
* Write text content to a text file,replace existent content in the file.
*
* @param fileName name of the text file.
* @param content content of the text file.
*/
public static void writeTextFile(String fileName,String content){
try{
PrintWriter out = new PrintWriter( new FileWriter(fileName) );
out.print(content);
out.close();
out =null;
}catch(Exception e){
m_logger.error("WriteTextFile: "+e);
}
}
/**
* save document to xml file.
* @param fileName an xml file name
* @param doc an XML Document contains the xml content.
*/
public void saveDocument(String fileName,Document doc){
if (fileName==null || fileName.length()==0 || doc==null)
return;
String xml =getDocumentText(doc);
writeTextFile(fileName,xml);
}
/**
* get document as xml text.
* @param doc an XML Document contains the xml content.
* @return xml text.
*/
public String getDocumentText(Document doc){
StringBuffer buf =new StringBuffer();
writeXMLHead(buf);
int level =0;
Element root =doc.getRoot();
writeElement(buf,root,level);
return buf.toString();
}
/**
* write xml head
* @param buf A string buffer to write xml head
*/
private void writeXMLHead(StringBuffer buf){
buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
buf.append(XMLConst.XML_NEWLINE);
}
/**
* write a specified number of spaces
* @param buf A string buffer to write spaces
* @param num number of spaces
*/
private void writeSpace(StringBuffer buf, int num){
while (num>0){
buf.append(' ');
num--;
}
}
/**
* for special long text, e.g. image data, we
* need to seperate them by NEWLINE character.
*/
private String adjustText(String text){
if (text==null || text.equals(""))
return "";
//if has newLine/space character or total length less than a specified value,
//just return it with no change.
if (text.indexOf(XMLConst.XML_NEWLINE)>=0 || text.indexOf(' ')>=0 || text.length()<500)
return text;
StringBuffer buf =new StringBuffer();
for (int i=0; i<text.length(); i++){
buf.append(text.charAt(i));
if (i>0 && i%79==0)
buf.append(XMLConst.XML_NEWLINE);
}
return buf.toString();
}
/**
* write element
* @param buf A string buffer to write element content
* @param element An element to write to buffer
* @param level Level of this element.
*/
private void writeElement(StringBuffer buf, Element element,int level){
//leading spaces
int num =level * 2;
writeSpace(buf,num);
//head tag of an element
buf.append(XMLConst.XML_STARTTAG);
buf.append(element.getTag());
buf.append(XMLConst.XML_ENDTAG);
if (element.hasChildren()){
buf.append(XMLConst.XML_NEWLINE);
writeElements(buf,element.getChildren(),level+1);
//tail tag of an element
writeSpace(buf,num);
}else{
buf.append(adjustText(element.getValue()));
}
buf.append(XMLConst.XML_ENDSTARTTAG);
buf.append(element.getTag());
buf.append(XMLConst.XML_ENDTAG);
buf.append(XMLConst.XML_NEWLINE);
}
/**
* write sub elements
* @param buf A string buffer to write element content
* @param elementList Sub element list.
* @param level Level of this element list.
*/
private void writeElements(StringBuffer buf, List elementList,int level){
Iterator it =elementList.iterator();
while(it!=null && it.hasNext()){
Element element =(Element)it.next();
writeElement(buf,element,level);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?