xmlinput.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 217 行
JAVA
217 行
/**
* $Id:XMLInput.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfdom;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.net.URL;
import com.jfimagine.utils.commonutil.CommonUtil;
import com.jfimagine.utils.log.*;
/**
* XMLInput class. A class used to input an XML document.
*
* @author CookieMaker
*
* @version $Revision: 1.01 $
*/
public class XMLInput{
//a text content got from a text file
private String m_content="";
//current reading position on the text content.
private int m_pos=0;
/**an internal log utility*/
private JFLogger m_logger =JFLogManager.getLogger(this.getClass());
/**
* Read text content from a text file.
* @param fileName name of the text file.
* @return text content of a text file.
*/
public void readTextFile(String fileName){
StringBuffer buf =new StringBuffer();
try{
InputStream in=null;
if (CommonUtil.isURLFile(fileName)){
URL url =new URL(fileName);
in =url.openStream();
url =null;
}else{
in=new FileInputStream(fileName);
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while (true){
String text = reader.readLine();
if (text==null)
break;
buf.append(text);
}
reader.close();
in.close();
reader =null;
in =null;
m_content =new String(buf.toString());
buf.setLength(0);
buf=null;
}catch(Exception e){
m_logger.error("read text file: "+e);
m_content =null;
}
}
/**
* load document from xml file.
* @param fileName an xml file name
* @return An XML Document contains the xml content.
*/
public Document loadDocument(String fileName){
if (fileName==null || fileName.length()==0)
return null;
readTextFile(fileName);
return loadDocumentFromText();
}
/**
* load document from xml text.
* @param xml Plain xml text.
* @return An XML Document contains the xml content.
*/
public Document loadDocumentFromText(String xml){
m_content =xml;
return loadDocumentFromText();
}
/**
* load document from local xml text content.
* @return An XML Document contains the xml content.
*/
private Document loadDocumentFromText(){
if (m_content==null || m_content.length()==0)
return null;
m_pos =0;
//remove xml head.
int headEnd =m_content.indexOf("?>");
if (headEnd<0)
return null;
m_pos =headEnd+2;
Element root =getElement();
//clear content
m_content="";
if (root!=null)
return new Document(root);
else
return null;
}
/**
* Get element from current text content and pos.
*/
private Element getElement(){
if (m_pos>m_content.length()-1)
return null;
boolean tagStart =false;
boolean tagEnd =false;
StringBuffer buf =new StringBuffer();
while (m_pos<m_content.length()){
char ch =m_content.charAt(m_pos);
if (!tagStart){
if (ch==XMLConst.XML_STARTTAG)
tagStart =true;
}else if (!tagEnd){
if (ch==XMLConst.XML_ENDTAG){
tagEnd =true;
}else{
buf.append(ch);
}
}
m_pos++;
if (tagEnd){
Element element= new Element();
element.setTag(buf.toString());
fetchElements(element);
return element;
}
}
return null;
}
/**
* Fetch sub elements from current text content and pos.
* @param element An element to add sub elements.
*/
private void fetchElements(Element element){
String tag=element.getTag();
StringBuffer buf=new StringBuffer();
boolean hasChildren=false;
buf.append("");
while (m_pos<m_content.length()){
char ch =m_content.charAt(m_pos);
if (ch==XMLConst.XML_STARTTAG){
if (m_content.indexOf(XMLConst.XML_ENDSTARTTAG+tag,m_pos)==m_pos){
//found the end of this element:
if (!hasChildren){
element.setValue(buf.toString().trim());
}
int tagEnd =m_content.indexOf(XMLConst.XML_ENDTAG,m_pos);
if (tagEnd<0)
//broken xml
m_pos =m_content.length();
else
m_pos =tagEnd+1;
return;
}else{
//spawn new sub element:
hasChildren =true;
Element subElement =getElement();
if (subElement!=null)
element.addChild(subElement);
}
}else if (ch==XMLConst.XML_ENDTAG){
//invalid tag.
m_pos =m_content.length();
return;
}else {
if (!hasChildren)
buf.append(ch);
}
m_pos++;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?