import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
*/
/**
* 文件名称:XMLProcess.java
*
*
* 创建日期:2010-5-3
*
* 修改历史:
*/
public class XMLProcess
{
private String filename=null;
private Document doc;
public XMLProcess(String filename)
{
this.filename=filename;
final File file=new File(filename);
DocumentBuilderFactory factory2=DocumentBuilderFactory.newInstance();
factory2.setNamespaceAware(true);
try {
DocumentBuilder builder=factory2.newDocumentBuilder();
doc=builder.parse(file);
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static final String workDir="D://";
/**
* 开始下载xml文件中的图片,图片会分别放到workDir+username的目录中
* 并且会更新内存中的xml数据,将图片的url替换成本地地址
*/
public void downFiles()
{
org.w3c.dom.Element rootElement = doc.getDocumentElement();
NodeList modelList = rootElement.getElementsByTagName("uml:Model");
org.w3c.dom.Element modelElement = (org.w3c.dom.Element)modelList.item(0);
NodeList classes=modelElement.getElementsByTagName("packagedElement");
//获取所有的packagedElement,这里是所有的user
for(int i=0;i<classes.getLength();++i)
{
Element user=(Element) classes.item(i);
if(!user.getAttribute("xmi:type").equals("uml:Class"))
break;
//username
String usernameString=user.getAttribute("name");
File file=new File(workDir+usernameString);
file.mkdirs();
DownFile.setDir(workDir+usernameString+"//");
System.out.println(usernameString);
NodeList propertys=user.getElementsByTagName("ownedAttribute");
//for each pic
for(int j=0;j<propertys.getLength();++j)
{
Element picURL=(Element) propertys.item(j);
//pic name
String filename=picURL.getAttribute("name");
System.out.println(filename);
NodeList comments=picURL.getElementsByTagName("ownedComment");
Element uRLElement=(Element) comments.item(0);
//pic URL
String URL=uRLElement.getAttribute("body");
System.out.println(URL);
if(DownFile.savetofile(URL))
uRLElement.setAttribute("body", DownFile.getFileName(URL));
}
}
}
/**
* 写回到原XML文件中
* @return 返回是否成功
*/
public boolean writeBackXML()
{
System.out.println("开始写回XML");
try {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
// 设置输出的encoding为改变gb2312
transformer.setOutputProperty("encoding", "gb2312");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(filename);
transformer.transform(source, result);
} catch (javax.xml.transform.TransformerConfigurationException e) {
e.printStackTrace();
System.out.println(e.getMessage());
return false;
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
System.out.println("写回"+filename+"成功");
return true;// 写文件错误
}
/**
* 返回对应userName的节点
* @param userName
* @return
*/
protected Element getUser(String userName)
{
org.w3c.dom.Element rootElement = doc.getDocumentElement();
NodeList modelList = rootElement.getElementsByTagName("uml:Model");
org.w3c.dom.Element modelElement = (org.w3c.dom.Element)modelList.item(0);
NodeList classes=modelElement.getElementsByTagName("packagedElement");
//获取所有的packagedElement,这里是所有的user
for(int i=0;i<classes.getLength();++i)
{
Element user=(Element) classes.item(i);
if(!user.getAttribute("xmi:type").equals("uml:Class"))
break;
//username
String usernameString=user.getAttribute("name");
if(usernameString.equals(userName))
{
return user;
}
}
return null;
}
/**
* 获取对应username的所有的pic
* 如果没有对应的username
* 那么返回空
* @param username
* @return
*/
public List<String> getPics(String username)
{
List<String> testsList=null;
Element user=getUser(username);
if(user!=null)
{
testsList=new LinkedList<String>();
NodeList propertys=user.getElementsByTagName("ownedAttribute");
//for each pic
for(int j=0;j<propertys.getLength();++j)
{
Element picURL=(Element) propertys.item(j);
NodeList comments=picURL.getElementsByTagName("ownedComment");
Element uRLElement=(Element) comments.item(0);
//pic URL
String URL=uRLElement.getAttribute("body");
testsList.add(URL);
}
return testsList;
}
return testsList;
}
/**
* 如果想要删除一个节点
* 可以使用Element * .removeChild(oldChild)
* @param args
*/
public static void main(String[] args)
{
XMLProcess thisProcess=new XMLProcess("D://test.xml");
thisProcess.downFiles();
thisProcess.writeBackXML();
}
}