package com.smtscript.lib.xml; import java.util.ArrayList; import java.util.List; public class JSXMLModifyNode extends JSXMLModifyAbstract { protected int _keyPos = -1; protected List _children = new ArrayList(); protected List _attrs = new ArrayList(); @Override public String toString() { return "node : " + (_keyPos >= 0 ? _listXmlText.get(_keyPos).substring(1) : ""); } public void addChildren(JSXMLModifyAbstract node) { node._parent = this; node._listXmlText = this._listXmlText; _children.add(node); } public void addAttr(JSXMLModifyAttr attr) { attr._parent = this; attr._listXmlText = this._listXmlText; _attrs.add(attr); } public String getKey() { return _listXmlText.get(_keyPos).substring(1); } public JSXMLModifyNode getFirstXmlNode(String path) { JSXMLModifyNode xmlNode = this; String[] spPath = path.split("/"); for(int i = 0; i < spPath.length; i ++) { String subPath = spPath[i]; if(subPath.length() == 0) continue; boolean found = false; for(JSXMLModifyAbstract xmlChildNode : xmlNode._children) { if(!(xmlChildNode instanceof JSXMLModifyNode)) continue; JSXMLModifyNode childNode = (JSXMLModifyNode)xmlChildNode; if(subPath.equals(childNode.getKey())) { xmlNode = childNode; found = true; break; } } if(!found) return null; } return xmlNode; } public boolean setFirstXmlAttr(String path, String attr, String value) { JSXMLModifyNode xmlNode = this; String[] spPath = path.split("/"); for(int i = 0; i < spPath.length; i ++) { String subPath = spPath[i]; if(subPath.length() == 0) continue; boolean found = false; for(JSXMLModifyAbstract xmlChildNode : xmlNode._children) { if(!(xmlChildNode instanceof JSXMLModifyNode)) continue; JSXMLModifyNode childNode = (JSXMLModifyNode)xmlChildNode; if(subPath.equals(childNode.getKey())) { xmlNode = childNode; found = true; break; } } if(!found) return false; } for(JSXMLModifyAttr xmlAttr : xmlNode._attrs) { if(attr.equals(xmlAttr.getKey())) { xmlAttr.setValue(value); return true; } } return false; } public boolean setFirstXmlBody(String path, String text) { JSXMLModifyNode xmlNode = this; String[] spPath = path.split("/"); for(int i = 0; i < spPath.length; i ++) { String subPath = spPath[i]; if(subPath.length() == 0) continue; boolean found = false; for(JSXMLModifyAbstract xmlChildNode : xmlNode._children) { if(!(xmlChildNode instanceof JSXMLModifyNode)) continue; JSXMLModifyNode childNode = (JSXMLModifyNode)xmlChildNode; if(subPath.equals(childNode.getKey())) { xmlNode = childNode; found = true; break; } } if(!found) return false; } for(JSXMLModifyAbstract xmlChildNode : xmlNode._children) { if(!(xmlChildNode instanceof JSXMLModifyBody)) continue; JSXMLModifyBody childBody = (JSXMLModifyBody)xmlChildNode; childBody.setBody(text); return true; } return false; } public String getFirstXmlBody(String path) { JSXMLModifyNode xmlNode = this; String[] spPath = path.split("/"); for(int i = 0; i < spPath.length; i ++) { String subPath = spPath[i]; if(subPath.length() == 0) continue; boolean found = false; for(JSXMLModifyAbstract xmlChildNode : xmlNode._children) { if(!(xmlChildNode instanceof JSXMLModifyNode)) continue; JSXMLModifyNode childNode = (JSXMLModifyNode)xmlChildNode; if(subPath.equals(childNode.getKey())) { xmlNode = childNode; found = true; break; } } if(!found) return null; } for(JSXMLModifyAbstract xmlChildNode : xmlNode._children) { if(!(xmlChildNode instanceof JSXMLModifyBody)) continue; JSXMLModifyBody childBody = (JSXMLModifyBody)xmlChildNode; return childBody.getBody(); } return null; } public String getXmlBodyText() { StringBuilder sb = new StringBuilder(); for(JSXMLModifyAbstract xmlChildNode : this._children) { if(!(xmlChildNode instanceof JSXMLModifyBody)) continue; JSXMLModifyBody childBody = (JSXMLModifyBody)xmlChildNode; sb.append(childBody.getBody()); } return sb.toString(); } @Override public String getType() { return "NODE"; } }