1 2 package SOFA.SOFAnode.Run; 3 import java.io.FileOutputStream ; 4 import java.io.IOException ; 5 6 import javax.xml.parsers.DocumentBuilder ; 7 import javax.xml.parsers.DocumentBuilderFactory ; 8 import javax.xml.parsers.ParserConfigurationException ; 9 import javax.xml.transform.Transformer ; 10 import javax.xml.transform.TransformerConfigurationException ; 11 import javax.xml.transform.TransformerException ; 12 import javax.xml.transform.TransformerFactory ; 13 import javax.xml.transform.dom.DOMSource ; 14 import javax.xml.transform.stream.StreamResult ; 15 16 import org.w3c.dom.Document ; 17 import org.w3c.dom.Element ; 18 import org.w3c.dom.Node ; 19 import org.w3c.dom.NodeList ; 20 import org.xml.sax.SAXException ; 21 22 26 public class SpecFile { 27 28 29 protected Element element; 30 31 protected String releaseVersion; 32 33 protected String fullApplName; 34 35 protected String fullArchName; 36 37 41 public SpecFile(Element elem) { 42 element = elem; 43 element.normalize(); 44 initialize(); 45 } 47 48 49 private void initialize() { 50 NodeList nl = element.getChildNodes(); 51 for (int i=0; i<nl.getLength(); i++) { 52 Node n = nl.item(i); 53 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("version")==0)) { 54 releaseVersion = n.getFirstChild().getNodeValue(); 55 } else if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) { 56 String archName = n.getFirstChild().getNodeValue(); 57 fullArchName = archName; 58 fullApplName = archName.substring(0, archName.indexOf('?')); 59 } 60 } 61 fullApplName = fullApplName+"["+releaseVersion+"]"; 62 } 63 64 69 public static SpecFile getSpecFileFromDeplDescriptorXMLDoc(Element dd) { 70 removeDDSpecificElements(dd); 71 return new SpecFile(dd); 72 } 73 74 public static SpecFile getSpecFileFromFile(String fname) throws java.io.IOException , ParserConfigurationException , SAXException { 75 java.io.File file = new java.io.File (fname); 76 if (!file.exists()) { 77 throw new java.io.IOException ("File \""+fname+"\" doesn't exist."); 78 } 79 80 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 81 DocumentBuilder builder = factory.newDocumentBuilder(); 82 Document document = builder.parse(file); 83 Element element = document.getDocumentElement(); 84 element.normalize(); 85 return new SpecFile(element); 86 } 87 88 private static void removeDDSpecificElements(Node node) { 89 NodeList nl = node.getChildNodes(); 90 java.util.ArrayList arr = new java.util.ArrayList (); 91 for (int i=0; i<nl.getLength(); i++) { 92 Node n = nl.item(i); 93 if ((n.getNodeType() == Node.ELEMENT_NODE) && ((n.getNodeName().compareTo("cdl_entities")==0) || (n.getNodeName().compareTo("implementation")==0)) ) { 94 arr.add(n); 95 } else if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("sofa_component")==0) ) { 97 removeDDSpecificElements(n); 98 } 99 if (n.getNodeType() == Node.TEXT_NODE) { 100 arr.add(n); 101 } 103 } 104 for (int i=0; i<arr.size(); i++) { 105 node.removeChild((Node )arr.get(i)); 106 } 107 node.normalize(); 108 } 109 110 115 public void toFile(String name) throws IOException , TransformerConfigurationException , TransformerException { 116 TransformerFactory tFactory = TransformerFactory.newInstance(); 117 Transformer transformer = tFactory.newTransformer(); 118 transformer.setOutputProperty("indent", "yes"); 119 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5"); 120 DOMSource source = new DOMSource (element); 121 FileOutputStream out = new FileOutputStream (name); 122 StreamResult result = new StreamResult (out); 123 transformer.transform(source, result); 124 out.close(); 125 } 126 127 public void debugPrint(Node nd, String indent) { 128 System.out.println(indent+nd.getNodeName()+" "+nodeType(nd)); 129 NodeList nl = nd.getChildNodes(); 130 for (int i=0; i<nl.getLength(); i++) { 131 Node n = nl.item(i); 132 debugPrint(n, indent+" "); 133 } 134 } 135 136 private String nodeType(Node nd) { 137 switch (nd.getNodeType()) { 138 case Node.ELEMENT_NODE: return "ELEMENT_NODE"; 139 case Node.TEXT_NODE: return "TEXT_NODE"; 140 default: return "UNKNOWN"; 141 } 142 } 143 144 148 public Element getRootElement() { 149 return element; 150 } 151 152 156 public String getFullArchName() { 157 return fullArchName; 158 } 159 160 164 public String getImplVersion() { 165 return releaseVersion; 166 } 167 168 public static String getFullApplName(Element element) { 169 String releaseVersion = null; 170 String fullApplName = null; 171 NodeList nl = element.getChildNodes(); 172 for (int i=0; i<nl.getLength(); i++) { 173 Node n = nl.item(i); 174 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("version")==0)) { 175 releaseVersion = n.getFirstChild().getNodeValue(); 176 } else if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) { 177 String archName = n.getFirstChild().getNodeValue(); 178 fullApplName = archName.substring(0, archName.indexOf('?')); 179 } 180 } 181 return fullApplName+"["+releaseVersion+"]"; 182 } 183 184 public static String getFullArchName(Element element) { 185 NodeList nl = element.getChildNodes(); 186 for (int i=0; i<nl.getLength(); i++) { 187 Node n = nl.item(i); 188 if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().compareTo("architecture_ref")==0)) { 189 return n.getFirstChild().getNodeValue(); 190 } 191 } 192 return null; 193 } 194 } 195 | Popular Tags |