1 6 7 package cve.esecutori.servizioEACBuildSemantic; 8 9 import cve.staticLayout.*; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.*; 14 import java.util.*; 15 16 import org.jdom.*; 17 import org.jdom.Document; 18 import org.jdom.Element; 19 import org.jdom.JDOMException; 20 import org.jdom.input.SAXBuilder; 21 import org.jdom.output.XMLOutputter; 22 import javax.swing.text.html.parser.*; 23 24 29 final public class AssociationSem { 30 31 34 private Document docAlf,docRegSint,docRegSem, docRegAss; 35 private String fileRegSint, fileRegSem, fileRegAss; 36 37 private Element rootRegSint,rootRegSem,rootRegAss; 38 private Element ruleSint, ruleSem, assRule; 39 40 42 43 51 public AssociationSem(String FileRegSint,String FileRegSem, String FileRegAss)throws java.io.IOException { 52 53 this.fileRegSint = FileRegSint; 54 this.fileRegSem = FileRegSem; 55 this.fileRegAss = FileRegAss; 56 57 58 buildDocument(fileRegSint,fileRegSem,fileRegAss); 60 61 rootRegSint=docRegSint.getRootElement(); 62 rootRegSem =docRegSem.getRootElement(); 63 rootRegAss =docRegAss.getRootElement(); 64 65 } 66 67 74 public void setAssociation(String idSint, String idSem){ 75 76 boolean testSelect = true; 77 78 System.out.println(" #E AssociationSem: setAssociation idSint: "+idSint+" idSem: "+idSem); 79 80 ruleSint = getRule(rootRegSint,idSint); 82 83 if (ruleSint == null){ 84 System.out.println(" #E AssociationSem: setAssociation Elemento idSint NON TROVATO!"); 85 testSelect = false; 86 } 87 88 ruleSem = getRule(rootRegSem,idSem); 90 91 if (ruleSem == null){ 92 System.out.println(" #E AssociationSem: setAssociation Elemento idSem NON TROVATO!"); 93 testSelect = false; 94 } 95 96 if (testSelect){ 97 assRule = new Element("Association"); 99 100 setIdAss(assRule); 102 System.out.println ("AssociationSem. Creato ID= " + assRule.getName()); 103 104 setSintRule(assRule); 106 107 setSemRule(assRule); 109 110 rootRegAss.addContent(assRule); 113 114 saveFileXml(fileRegAss); 116 } 117 } 118 119 125 public String getFileAss(){ 126 return fileRegAss; 127 } 128 129 130 132 133 134 142 private void buildDocument(String FileRegSint,String FileRegSem, String FileRegAss){ 143 try { 144 SAXBuilder builder = new SAXBuilder(false); 146 147 docRegSint = builder.build(new File (FileRegSint)); 148 docRegSem = builder.build(new File (FileRegSem)); 149 docRegAss = builder.build(new File (FileRegAss)); 150 151 } catch (JDOMException e) { 153 if (e.getRootCause() != null) { 154 e.getRootCause().printStackTrace(); 155 } 156 e.printStackTrace(); 157 } catch (Exception e) { 158 e.printStackTrace(); 159 } 160 } 161 162 169 private Element getRule(Element root, String idRule){ 170 171 List setTransition = root.getChildren("Transition"); 172 173 for (int j=0; j<setTransition.size();j++){ 174 Element transition=(Element)(setTransition.get(j)); 175 List attList = transition.getAttributes(); 176 177 for (int i=0; i<attList.size();i++){ 180 181 Attribute appoAttr = (Attribute)attList.get(i); 183 184 String appoAttrName = appoAttr.getName(); 185 appoAttrName = appoAttrName.trim(); 186 187 String appoAttrValue = appoAttr.getValue(); 188 appoAttrValue = appoAttrValue.trim(); 189 190 192 if (appoAttrName.equals("id")){ 194 195 if (appoAttrValue.equals(idRule)) return transition; 197 } 198 } 199 } 200 return null; 201 } 202 203 209 private void setIdAss(Element ruleAss){ 210 211 String appoIdAss, idAss; 212 int appoId, id; 213 214 idAss = "idAss"; 215 id = 1; 216 217 List setAssociation = rootRegAss.getChildren("Association"); 219 for (int j=0; j<setAssociation.size();j++){ 220 Element association=(Element)(setAssociation.get(j)); 221 222 List attList = association.getAttributes(); 223 224 for (int i=0; i<attList.size();i++){ 227 228 Attribute appoAttr = (Attribute)attList.get(i); 230 231 String appoAttrName = appoAttr.getName(); 232 appoAttrName = appoAttrName.trim(); 233 234 String appoAttrValue = appoAttr.getValue(); 235 appoAttrValue = appoAttrValue.trim(); 236 237 239 if (appoAttrName.equals("idAss")){ 241 242 appoId = Integer.parseInt(appoAttrValue.substring(5)); 244 245 if (appoId >= id){ 246 id = appoId+1; 247 } 248 } 249 250 } 251 } 252 253 idAss = idAss + id; 255 257 ruleAss.addAttribute("idAss",idAss); 258 ruleAss.addAttribute("description","Association n."+id); 259 } 260 261 269 private void setSintRule(Element ruleAss){ 270 271 String idRule = getIdRule(ruleSint); 273 System.out.println(" #E AssociationSem: setSintRule idSint: "+idRule); 274 275 addSintElement(ruleAss,idRule); 277 } 278 279 280 288 private void setSemRule(Element ruleAss){ 289 290 String idRule = getIdRule(ruleSem); 292 System.out.println(" #E AssociationSem: setSintRule idSint: "+idRule); 293 294 addSemElement(ruleAss,idRule); 296 } 297 298 306 private String getIdRule(Element Rule){ 307 308 List attList = Rule.getAttributes(); 309 310 Attribute appoAttr = (Attribute)attList.get(0); 312 String appoAttrName = appoAttr.getName(); 313 appoAttrName = appoAttrName.trim(); 314 String appoAttrValue = appoAttr.getValue(); 315 appoAttrValue = appoAttrValue.trim(); 316 if (appoAttrName.equals("id")){ 318 return appoAttrValue; 319 } 320 return null; 321 } 322 323 330 private void addSintElement(Element ruleAss, String idRule){ 331 332 Element eleSint = new Element("SyntaxRule"); 334 eleSint.addAttribute("idRule",idRule); 335 ruleAss.addContent(eleSint); 336 } 337 338 345 private void addSemElement(Element ruleAss, String idRule){ 346 347 Element eleSem = new Element("SemanticRule"); 349 eleSem.addAttribute("idRule",idRule); 350 ruleAss.addContent(eleSem); 351 } 352 353 354 356 362 private void saveFileXml(String pathFile) { 363 File fileS = new File (pathFile); 364 try { 365 fileS.createNewFile(); 366 FileWriter outS= new FileWriter (fileS); 367 XMLOutputter fmtS = new XMLOutputter(" ",true); 368 String appS=fmtS.outputString(rootRegAss); 369 outS.write(appS); 370 outS.close(); 371 }catch( IOException e){ 372 System.out.println(" problemi nell'aprire il file Associazione"+"\r\n"); 373 } 374 } 375 376 379 public static void main(String [] args) throws java.io.IOException { 380 381 InputStreamReader ir,ir1,ir2; 382 String fileSint="D:/CVE_HOME/VisualLanguage/SpecificheWippog/Sent_PetriLanguage.xml"; 383 String fileSem = "D:\\CVE_HOME\\VisualLanguage\\SpecificheWippog\\Sent_PetriLanguageSem.xml"; 384 String fileAss = "D:\\CVE_HOME\\VisualLanguage\\SpecificheWippog\\Sent_PetriLanguageSemAss.xml"; 385 386 System.out.println(""); 387 System.out.println(" SIMULAZIONE ASSOCIAZIONE REGOLE SINT e SEM --> FILE ASSSEM. (AssociationSem.class)"); 388 System.out.println(""); 389 AssociationSem spe=new AssociationSem(fileSint,fileSem, fileAss); 390 spe.setAssociation("id3","id1"); 391 } 392 393 } | Popular Tags |