package com.repositoryclient.xml;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.StringBufferInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.repositoryclient.treeview.FileNode;
public class RepositoryConstructionOption {
//-------------------------------------------------------------------------------------
private DocumentBuilderFactory factory;
private Element node;
private Element model;
private DocumentBuilder documentBuilder;
private Document document;
private String filePath="./xml/Test1.xml";
private String nodeType="Node";
private String modelType="Model";
private List wishList=new ArrayList<>();
private StringBufferInputStream inputStream;
private String xmlFileContent;
//-------------------------------------------------------------------------------------
public RepositoryConstructionOption(String xmlFileContent){
this.xmlFileContent=xmlFileContent;
setUpDomFactory();
}
private void setUpDomFactory(){
factory=DocumentBuilderFactory.newInstance();
try{
inputStream=new StringBufferInputStream(xmlFileContent);
factory.setIgnoringElementContentWhitespace(true);
documentBuilder=factory.newDocumentBuilder();
document=documentBuilder.parse(inputStream);
}catch(Exception e){
e.printStackTrace();
}
}
//-----------------------------------------------------------------------------------
public void addNode(String targetNodePath,String newNodeName){
Element newChild=document.createElement(newNodeName);
newChild.setTextContent("\n");
newChild.setAttribute("type", "Node");
newChild.setAttribute("NodeName", newNodeName);
newChild.setAttribute("NodePath", targetNodePath);
newChild.setAttribute("NodeAuther", "Administrator");
newChild.setAttribute("NodeSize", String.valueOf());
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
newChild.setAttribute("CreateData", date.format(new Date()));
Element targetElement=(Element) selectSingleNode(targetNodePath, document);
targetElement.appendChild(newChild);
saveXml("./xml/Test1.xml", document);
}
public void deleteNode(String targetNodePath){
Element targetElement=(Element) selectSingleNode(targetNodePath, document);
targetElement.getParentNode().removeChild(targetElement);
saveXml("./xml/Test1.xml", document);
}
public void renameNode(String targetNodePath,String newNodeName){
Node targetNode=selectSingleNode(targetNodePath, document);
document.renameNode(targetNode, null, newNodeName);
saveXml("./xml/Test1.xml", document);
}
//-------------------------------------------------------------------------------------
public void addModel(String targetNodePath,FileNode newModel,String[] Tags){
Element newChild=document.createElement(newModel.getFileName());
//设置Model的属性信息
newChild.setAttribute("type", "Model");
newChild.setAttribute("NodeName", newModel.getFileName());
newChild.setAttribute("NodePath", newModel.getPath());
newChild.setAttribute("NodeAuther", newModel.getAuthor());
newChild.setAttribute("NodeSize", String.valueOf(newModel.getSize()));
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
newChild.setAttribute("CreateData", date.format(newModel.getDate()));
//设置Model的Tag(标签),以后可能会添加通过Tag搜索的功能时会用到
newChild.setAttribute("Tag0", Tags[]);
newChild.setAttribute("Tag1", Tags[]);
newChild.setAttribute("Tag2", Tags[]);
newChild.setNodeValue("Model");
Element targetElement=(Element) selectSingleNode(targetNodePath, document);
//添加Model到xml
targetElement.appendChild(newChild);
saveXml("./xml/Test1.xml", document);
}
public void deleteModel(String modelName){
Element targetElement=(Element) selectSingleNode(modelName, document);
targetElement.getParentNode().removeChild(targetElement);
saveXml("./xml/Test1.xml", document);
}
//-------------------------------------------------------------------------------------
public List getTheTree(String rootNodeName){
List resultList=new ArrayList();
Element targetNode=(Element) document.getElementsByTagName(rootNodeName).item();
FileNode fatherNode=new FileNode("root", "Repository", , "Administrator", , new Date(), null, "Node");
searchTheTree(targetNode,fatherNode);
resultList.add(fatherNode);
return resultList;
}
public void searchTheTree(Node rootNode,FileNode fatherNode){
try {
List childList=new ArrayList();
Node node;
NodeList childNodesList=rootNode.getChildNodes();
for(int i=;i<childNodesList.getLength();i++){
node=childNodesList.item(i);
if(node.getNodeName()!="#text"){
//System.out.println("here: "+node.getAttributes().item(0));
NamedNodeMap nodeMap=node.getAttributes();
if(nodeMap.getNamedItem("type").getNodeValue().equals(nodeType)){
//String property[]=new String[8];
String property[]=handleModelProperty(node);
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
FileNode subNode;
subNode = new FileNode(property[], property[], , property[], Long.valueOf(property[]),date.parse(property[]) , null, "Node");
searchTheTree(node, subNode);
childList.add(subNode);
}else{
//String property[]=new String[8];
String property[]=handleModelProperty(node);
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
FileNode subNode=new FileNode(property[], property[], , property[], Long.valueOf(property[]),date.parse(property[]) , null, "Model");
childList.add(subNode);
}
}
}
fatherNode.setChildren(childList);
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[] handleModelProperty(Node node){
String modelProperty[] = new String[];;
NamedNodeMap nodeMap=node.getAttributes();
//System.out.println(nodeMap.getNamedItem("NodeName").getNodeValue());
modelProperty[]=nodeMap.getNamedItem("NodeName").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("NodePath").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("NodeAuther").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("NodeSize").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("CreateData").getNodeValue();
if(nodeMap.getNamedItem("type").getNodeValue().equals(modelType))
{
modelProperty[]=nodeMap.getNamedItem("Tag0").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("Tag1").getNodeValue();
modelProperty[]=nodeMap.getNamedItem("Tag2").getNodeValue();
}
return modelProperty;
}
public List searchModelByKeyWords(String rootNodeName,String keywords){
Element targetNode=(Element) document.getElementsByTagName(rootNodeName).item();
wishList.clear();
getTheList(targetNode,,keywords);
return wishList;
}
public List getUsersOwnedFiles(String rootNodeName,String author){
Element targetNode=(Element) document.getElementsByTagName(rootNodeName).item();
wishList.clear();
getTheList(targetNode,-,author);
return wishList;
}
/**
*
* @param rootNode
* @param key (-1:getUsersOwnedFiles 0:searchModelByKeyWords)
*/
public void getTheList(Node rootNode,int key,String requirement){
try{
Node node;
NodeList childNodesList=rootNode.getChildNodes();
for(int i=;i<childNodesList.getLength();i++){
node=childNodesList.item(i);
if(node.getNodeName()!="#text"){
NamedNodeMap nodeMap=node.getAttributes();
if(nodeMap.getNamedItem("type").getNodeValue().equals(nodeType)){
getTheList(node, key,requirement);
}else{
String property[]=handleModelProperty(node);
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
FileNode subNode=new FileNode(property[], property[], , property[], Long.valueOf(property[]),date.parse(property[]) , null, "Model");
switch (key) {
case -://getUsersOwnedFiles
if(requirement.equals(subNode.getAuthor())){
wishList.add(subNode);
}
break;
default://searchModelByKeyWords
if(requirement.contains(property[]) || requirement.contains(property[]) || requirement.contains(property[]) || property[].contains(requirement) || property[].contains(requirement) || property[].contains(requirement)){
wishList.add(subNode);
}
break;
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
}
//-------------------------------------------------------------------------------------
public void saveXml(String fileName, Document doc) {//将Document输出到文件
TransformerFactory transFactory=TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
DOMSource source=new DOMSource();
source.setNode(doc);
StreamResult result=new StreamResult();
result.setOutputStream(new FileOutputStream(fileName));
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Node selectSingleNode(String express, Object source) {//查找节点,并返回第一个符合条件节点
Node result=null;
XPathFactory xpathFactory=XPathFactory.newInstance();
XPath xpath=xpathFactory.newXPath();
try {
result=(Node) xpath.evaluate(express, source, XPathConstants.NODE);
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return result;
}
//-------------------------------------------------------------------------------------
}