import java.util.ArrayList; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class XMLWriter extends XMLReader{ protected XMLWriter(){ super(); } protected XMLWriter(String path) throws Exception{ super(path); } public static XMLWriter getInstance(String path) throws Exception{ return new XMLWriter(path); } @Override protected void initialize(String path)throws Exception{ setDocument(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); setFile(path); } public void setValue(String xpath,String value) throws Exception{ ArrayList pathNode = PathSplite(xpath); NodeList nodeList = null; Element element = null; NodeType type = null; for(int i=0 ; i < pathNode.size() ; i++){ type = NodeTypeCheck(pathNode.get(i)); if(type.equals(NodeType.NODE)){ element = NodeCreate(element,nodeList,pathNode.get(i),0,(i == pathNode.size()-1)?value:null); }else if(type.equals(NodeType.LIST)){ String pNodeName = pathNode.get(i); int sPos = pNodeName.indexOf("[")+1; int ePos = pNodeName.indexOf("]"); int listIndex = Integer.valueOf(pNodeName.substring(sPos,ePos)) - 1; pNodeName = pNodeName.substring(0,sPos-1); element = NodeCreate(element,nodeList,pNodeName,listIndex,(i == pathNode.size()-1)?value:null); }else{ if((i == pathNode.size()-1)){ String attrName = pathNode.get(pathNode.size()-1).substring(1); element.removeAttribute(attrName); element.setAttribute(attrName, value); }else{ throw new Exception("Pathの表現が非性格です。- ErrorCode : setValue"); } } } flush(); } protected Element NodeCreate(Element element,NodeList nodeList,String pNodeName,int count,String value){ Element buffer = null; if(element == null){ nodeList = getDocument().getElementsByTagName(pNodeName); }else{ nodeList = element.getElementsByTagName(pNodeName); } int length = nodeList.getLength(); for(int j=0;j<=count;j++){ if(length <= j){ buffer = getDocument().createElement(pNodeName); if(element == null){ getDocument().appendChild(buffer); }else{ element.appendChild(buffer); } } } if(element == null){ nodeList = getDocument().getElementsByTagName(pNodeName); }else{ nodeList = element.getElementsByTagName(pNodeName); } buffer = (Element)nodeList.item(count); if(value != null){ NodeList childnodes = buffer.getChildNodes(); for(int z=0 ; z < childnodes.getLength() ; z++){ if(buffer.getChildNodes().item(z).getNodeType() == Node.TEXT_NODE){ buffer.removeChild(buffer.getChildNodes().item(z)); } } buffer.appendChild(getDocument().createTextNode(value)); } return buffer; } protected ArrayList PathSplite(String path) throws Exception{ String[] pathNode = path.split("/"); if(pathNode.length <= 1){ throw new Exception("Pathの表現が非性格です。- ErrorCode : PathSplite"); } ArrayList ret = new ArrayList(); for(int i=1 ; i < pathNode.length ; i++){ ret.add(pathNode[i]); } return ret; } protected NodeType NodeTypeCheck(String nodeName){ if(nodeName.indexOf("@") >= 0){ return NodeType.ATTRIBUTE; }else if(nodeName.indexOf("[") >= 0 && nodeName.indexOf("]") >= 0){ return NodeType.LIST; }else{ return NodeType.NODE; } } public void flush() throws Exception{ try { TransformerFactory.newInstance().newTransformer().transform(new DOMSource(getDocument()), new StreamResult(getFile())); getDocument().getDocumentElement().normalize(); } catch (Exception e) { throw new Exception("error"); } } }