KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Run > SpecFile


1 /* $Id: SpecFile.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Run;
3 import java.io.FileOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 import javax.xml.parsers.DocumentBuilder JavaDoc;
7 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
8 import javax.xml.parsers.ParserConfigurationException JavaDoc;
9 import javax.xml.transform.Transformer JavaDoc;
10 import javax.xml.transform.TransformerConfigurationException JavaDoc;
11 import javax.xml.transform.TransformerException JavaDoc;
12 import javax.xml.transform.TransformerFactory JavaDoc;
13 import javax.xml.transform.dom.DOMSource JavaDoc;
14 import javax.xml.transform.stream.StreamResult JavaDoc;
15
16 import org.w3c.dom.Document JavaDoc;
17 import org.w3c.dom.Element JavaDoc;
18 import org.w3c.dom.Node JavaDoc;
19 import org.w3c.dom.NodeList JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21
22 /** Representation of the SOFA specfile.
23   *
24   * @author Petr Hnetynka
25   */

26 public class SpecFile {
27   
28   /** Root element of the specfile */
29   protected Element JavaDoc element;
30   /** Implementation version */
31   protected String JavaDoc releaseVersion;
32   /** Full application name */
33   protected String JavaDoc fullApplName;
34   /** Full architecture name */
35   protected String JavaDoc fullArchName;
36   
37   /** Constructor
38     *
39     * @param elem root element
40     */

41   public SpecFile(Element JavaDoc elem) {
42     element = elem;
43     element.normalize();
44     initialize();
45     //debugPrint(element, "");
46
}
47
48   /** Initializing */
49   private void initialize() {
50     NodeList JavaDoc nl = element.getChildNodes();
51     for (int i=0; i<nl.getLength(); i++) {
52       Node JavaDoc 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 JavaDoc archName = n.getFirstChild().getNodeValue();
57         fullArchName = archName;
58         fullApplName = archName.substring(0, archName.indexOf('?'));
59       }
60     }
61     fullApplName = fullApplName+"["+releaseVersion+"]";
62   }
63
64   /** Creates Specfile from deployment descriptor (!! it destroys dd).
65     *
66     * @param dd root element of depldescr
67     * @return new specfile
68     */

69   public static SpecFile getSpecFileFromDeplDescriptorXMLDoc(Element JavaDoc dd) {
70     removeDDSpecificElements(dd);
71     return new SpecFile(dd);
72   }
73
74   public static SpecFile getSpecFileFromFile(String JavaDoc fname) throws java.io.IOException JavaDoc, ParserConfigurationException JavaDoc, SAXException JavaDoc {
75     java.io.File JavaDoc file = new java.io.File JavaDoc(fname);
76     if (!file.exists()) {
77       throw new java.io.IOException JavaDoc("File \""+fname+"\" doesn't exist.");
78     }
79
80     DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
81     DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
82     Document JavaDoc document = builder.parse(file);
83     Element JavaDoc element = document.getDocumentElement();
84     element.normalize();
85     return new SpecFile(element);
86   }
87
88   private static void removeDDSpecificElements(Node JavaDoc node) {
89     NodeList JavaDoc nl = node.getChildNodes();
90     java.util.ArrayList JavaDoc arr = new java.util.ArrayList JavaDoc();
91     for (int i=0; i<nl.getLength(); i++) {
92       Node JavaDoc 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       // node.removeChild(n);
96
} 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         //node.removeChild(n);
102
}
103     }
104     for (int i=0; i<arr.size(); i++) {
105       node.removeChild((Node JavaDoc)arr.get(i));
106     }
107     node.normalize();
108   }
109
110   /** Writes specfile to file.
111     *
112     * @param name name of the file
113     * @throws IOException io exception
114     */

115   public void toFile(String JavaDoc name) throws IOException JavaDoc, TransformerConfigurationException JavaDoc, TransformerException JavaDoc {
116     TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
117     Transformer JavaDoc transformer = tFactory.newTransformer();
118     transformer.setOutputProperty("indent", "yes");
119     transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
120     DOMSource JavaDoc source = new DOMSource JavaDoc(element);
121     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(name);
122     StreamResult JavaDoc result = new StreamResult JavaDoc(out);
123     transformer.transform(source, result);
124     out.close();
125   }
126
127   public void debugPrint(Node JavaDoc nd, String JavaDoc indent) {
128     System.out.println(indent+nd.getNodeName()+" "+nodeType(nd));
129     NodeList JavaDoc nl = nd.getChildNodes();
130     for (int i=0; i<nl.getLength(); i++) {
131       Node JavaDoc n = nl.item(i);
132       debugPrint(n, indent+" ");
133     }
134   }
135
136   private String JavaDoc nodeType(Node JavaDoc 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   /** Returns root element of the document.
145     *
146     * @return root element
147     */

148   public Element JavaDoc getRootElement() {
149     return element;
150   }
151
152   /** Returns full name of the architecture.
153     *
154     * @return full name of the architecture
155     */

156   public String JavaDoc getFullArchName() {
157     return fullArchName;
158   }
159
160   /** Returns implementation version.
161     *
162     * @return implementation version
163     */

164   public String JavaDoc getImplVersion() {
165     return releaseVersion;
166   }
167
168   public static String JavaDoc getFullApplName(Element JavaDoc element) {
169     String JavaDoc releaseVersion = null;
170     String JavaDoc fullApplName = null;
171     NodeList JavaDoc nl = element.getChildNodes();
172     for (int i=0; i<nl.getLength(); i++) {
173       Node JavaDoc 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 JavaDoc archName = n.getFirstChild().getNodeValue();
178         fullApplName = archName.substring(0, archName.indexOf('?'));
179       }
180     }
181     return fullApplName+"["+releaseVersion+"]";
182   }
183
184   public static String JavaDoc getFullArchName(Element JavaDoc element) {
185     NodeList JavaDoc nl = element.getChildNodes();
186     for (int i=0; i<nl.getLength(); i++) {
187       Node JavaDoc 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