1 6 package cve.util; 7 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.io.*; 11 import java.util.*; 12 13 import org.jdom.*; 14 import org.jdom.Document; 15 import org.jdom.Element; 16 import org.jdom.JDOMException; 17 import org.jdom.input.SAXBuilder; 18 import org.jdom.output.XMLOutputter; 19 20 import org.apache.log4j.Logger; 21 22 import cve.staticLayout.Cve; 23 24 30 public class ElementXML { 31 32 private Document document; 33 private Element root = null; 34 private boolean primoAccesso = true; 35 private boolean trovato = false; 36 private String appoParameter = null; 37 38 43 public ElementXML (String fileXML) { 44 try { 45 Cve.errLog.debug(""); 46 SAXBuilder builder = new SAXBuilder(false); 48 document = builder.build(new File (fileXML)); 49 } catch (JDOMException e) { 50 if (e.getRootCause() != null) { 51 Cve.errLog.error(e.toString()); 52 } 54 e.printStackTrace(); 55 } catch (Exception e) { 56 Cve.errLog.error(e.toString()); 57 } 58 root=document.getRootElement(); 60 } 61 62 66 public Element getRootDocument() { 67 Cve.errLog.debug(""); 68 return root; 69 }; 70 71 79 public String getParameter(Element rootElement, String element, String parameter, String condAtt, String condValue){ 80 81 boolean avviaRicerca = false; 82 appoParameter = null; 83 trovato = false; 84 85 Cve.errLog.debug(""); 87 java.util.List setElement = rootElement.getChildren(); 88 for (int j=0; j<setElement.size();j++){ 89 Element elementFind=(Element)(setElement.get(j)); 90 91 String nomeElement = elementFind.getName(); 92 nomeElement = nomeElement.trim(); 93 94 96 if( nomeElement.equals(element)){ 98 99 java.util.List attList = elementFind.getAttributes(); 101 102 if (condAtt != null){ 104 105 for (int i=0; i<attList.size();i++){ 107 Attribute appoAttr = (Attribute)attList.get(i); 108 109 String appoAttrName = appoAttr.getName(); 110 appoAttrName = appoAttrName.trim(); 111 112 String appoAttrValue = appoAttr.getValue(); 113 appoAttrValue = appoAttrValue.trim(); 114 115 117 if (appoAttrName.equals(condAtt)){ 119 120 if (appoAttrValue.equals(condValue)){ 122 avviaRicerca = true; 123 } else { 124 avviaRicerca = false; 125 } 126 } 127 } 128 } else { 129 avviaRicerca = true; 130 } 131 if (avviaRicerca){ 133 for (int i=0; i<attList.size();i++){ 135 136 Attribute appoAttr = (Attribute)attList.get(i); 137 138 String appoAttrName = appoAttr.getName(); 139 appoAttrName = appoAttrName.trim(); 140 141 String appoAttrValue = appoAttr.getValue(); 142 appoAttrValue = appoAttrValue.trim(); 143 144 146 if (appoAttrName.equals(parameter)){ 148 trovato = true; 150 appoParameter = appoAttrValue; 151 } 152 } 154 } 156 } 158 if (trovato == false){ 159 getParameter(elementFind,element, parameter, condAtt, condValue); 161 } 162 } return appoParameter; 164 } 166 public static void main(String [] args){ 168 String fileXML="D:/CVE_HOME/VisualLanguage/SpecificheWippog/AssFileSem_Sent_PetriLanguageSemASS.xml"; 169 System.out.println(""); 170 System.out.println(" SIMULAZIONE RICERCA VALORE PARAMETRO IN FILE XML"); 171 System.out.println(""); 172 ElementXML obj=new ElementXML(fileXML); 173 System.out.println("VALORE TROVATO = " + obj.getParameter(obj.getRootDocument(),"SemanticFile","path",null,null)); 174 System.out.println("VALORE TROVATO = " + obj.getParameter(obj.getRootDocument(),"SyntaxFile","path",null,null)); 175 176 } 177 178 179 } 180 | Popular Tags |