KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > metamodel_xml > io > Import


1 /*
2  * Created on 6 oct. 2003
3  *
4  * To change the template for this generated file go to
5  * Window - Preferences - Java - Code Generation - Code and Comments
6  */

7 package org.objectweb.modfact.metamodel_xml.io;
8
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.util.Collection JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 import javax.jmi.reflect.RefObject;
15 import javax.jmi.reflect.RefPackage;
16 import javax.jmi.xmi.MalformedXMIException;
17 import javax.jmi.xmi.XmiReader;
18 import javax.xml.parsers.DocumentBuilder JavaDoc;
19 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
20 import javax.xml.parsers.ParserConfigurationException JavaDoc;
21
22 import org.objectweb.modfact.jmi.reflect.RefPackageImpl;
23 import org.objectweb.modfact.jmi.xmi.XmiReaderImpl;
24 import org.w3c.dom.Attr JavaDoc;
25 import org.w3c.dom.CDATASection JavaDoc;
26 import org.w3c.dom.Comment JavaDoc;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.DocumentType JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.NamedNodeMap JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.NodeList JavaDoc;
33 import org.w3c.dom.ProcessingInstruction JavaDoc;
34 import org.w3c.dom.Text JavaDoc;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /**
38  * @author Xavier
39  *
40  * To change the template for this generated type comment go to
41  * Window - Preferences - Java - Code Generation - Code and Comments
42  */

43 public class Import {
44
45     RefPackage model;
46     RefObject document = null;
47     boolean setRoot=false;
48
49     public RefPackage parse(InputStream JavaDoc inputFile)
50         throws
51             SAXException JavaDoc,
52             ParserConfigurationException JavaDoc,
53             IOException JavaDoc,
54             MalformedXMIException {
55
56         //Build repositories
57
RefPackage m2 = RefPackageImpl.getM2Repository();
58
59         XmiReader reader = new XmiReaderImpl();
60
61         reader.read("http://modfact.lip6.fr/ModFactWeb/resources/metamodel_xml.xml", m2);
62
63         RefObject packageMetaObject = null;
64         Iterator JavaDoc it = m2.refClass("Package").refAllOfClass().iterator();
65         while (it.hasNext()) {
66             RefObject o = (RefObject) it.next();
67             if (o.refGetValue("name").equals("XMLModel")) {
68                 packageMetaObject = o;
69                 break;
70             }
71
72         }
73         return parse(inputFile, packageMetaObject);
74
75     }
76
77     public RefPackage parse(InputStream JavaDoc inputFile, RefObject packageMetaObject)
78         throws
79             SAXException JavaDoc,
80             ParserConfigurationException JavaDoc,
81             IOException JavaDoc,
82             MalformedXMIException {
83
84         model = RefPackageImpl.getM1Repository(packageMetaObject);
85         
86         //XMLModelPackage modelTailored = (XMLModelPackage) model;
87

88         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
89         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
90
91         // Get Document
92
Document JavaDoc doc = builder.parse(inputFile);
93
94         nodeTemplate(doc);
95
96         return model;
97     }
98
99     public RefObject documentTemplate(Document JavaDoc doc) {
100         if (doc != null) {
101             RefObject documentModel =
102                 model.refClass("Document").refCreateInstance(null);
103             
104             return documentModel;
105         } else
106             return null;
107     }
108
109     public RefObject documentTypeTemplate(DocumentType JavaDoc doctype) {
110         if (doctype != null) {
111             RefObject documentTypeModel =
112                 model.refClass("DocumentType").refCreateInstance(null);
113             documentTypeModel.refSetValue("name", doctype.getName());
114             documentTypeModel.refSetValue("publicId", doctype.getPublicId());
115             documentTypeModel.refSetValue("systemId", doctype.getSystemId());
116             documentTypeModel.refSetValue(
117                 "internalSubset",
118                 doctype.getInternalSubset());
119             return documentTypeModel;
120         } else
121             return null;
122
123     }
124
125     public RefObject nodeTemplate(Node JavaDoc node) {
126         //Check the kind
127
// System.out.println("node:"+node.getNodeType()+" "+node.getNodeName());
128
RefObject element = null;
129         switch (node.getNodeType()) {
130             case Node.DOCUMENT_NODE :
131                 element = documentTemplate((Document JavaDoc) node);
132                 element.refSetValue("nodeType","DOCUMENT_NODE");
133                 document = element;
134                 break;
135             case Node.DOCUMENT_TYPE_NODE :
136                 element = documentTypeTemplate((DocumentType JavaDoc)node);
137                 element.refSetValue("nodeType","DOCUMENT_TYPE_NODE");
138                 document.refSetValue("doctype", element);
139                 break;
140             case Node.ELEMENT_NODE :
141                 element = elementTemplate((Element JavaDoc) node);
142                 element.refSetValue("nodeType","ELEMENT_NODE");
143                 break;
144             case Node.ATTRIBUTE_NODE :
145                 element = attributeTemplate((Attr JavaDoc) node);
146             element.refSetValue("nodeType","ATTRIBUTE_NODE");
147                 break;
148             case Node.COMMENT_NODE :
149                 element = commentTemplate((Comment JavaDoc) node);
150             element.refSetValue("nodeType","COMMENT_NODE");
151                 break;
152             case Node.TEXT_NODE :
153                 element = textTemplate((Text JavaDoc) node);
154                 element.refSetValue("nodeType","TEXT_NODE");
155                 break;
156             case Node.CDATA_SECTION_NODE :
157                 element = cdataSectionTemplate((CDATASection JavaDoc) node);
158             element.refSetValue("nodeType","CDATA_SECTION_NODE");
159                 break;
160             case Node.PROCESSING_INSTRUCTION_NODE :
161                 element =
162                     processingInstructionTemplate((ProcessingInstruction JavaDoc) node);
163                 element.refSetValue("nodeType","PROCESSING_INSTRUCTION_NODE");
164                 break;
165         }
166         if (element != null) {
167             //Child
168
if (node.hasChildNodes()) {
169                 RefObject nodeListModel =
170                     model.refClass("NodeList").refCreateInstance(null);
171                 //fr.lip6.xml.xmlmodel.NodeList nodeListCast = (fr.lip6.xml.xmlmodel.NodeList) nodeListModel;
172
Collection JavaDoc items =
173                     (Collection JavaDoc) nodeListModel.refGetValue("items");
174                 NodeList JavaDoc nl = node.getChildNodes();
175                 for (int i = 0; i < nl.getLength(); i++) {
176                     RefObject child = nodeTemplate(nl.item(i));
177                     items.add(child);
178                     child.refSetValue("parent", element);
179                 }
180                 element.refSetValue("childNodes", nodeListModel);
181             }
182             //Attribute
183
//if (node.hasAttributes()) {
184
// Vector items = new Vector();
185
// NamedNodeMap nl = node.getAttributes();
186
// for (int i = 0; i < nl.getLength(); i++) {
187
// RefObject child = nodeTemplate(nl.item(i));
188
// items.addElement(child);
189
// child.refSetValue("parent", element);
190
// }
191
// RefObject namedNodeMapModel =
192
// model.refClass("NamedNodeMap").refCreateInstance(null);
193
// namedNodeMapModel.refSetValue("items", items);
194
// element.refSetValue("nodeAttributes", namedNodeMapModel);
195
//}
196
if (document != null)
197                 element.refSetValue("ownerDocument", document);
198         }
199         return element;
200     }
201
202     public RefObject elementTemplate(Element JavaDoc element) {
203         if (element != null) {
204             RefObject elementModel =
205                 model.refClass("Element").refCreateInstance(null);
206             if (element.hasAttributes()) {
207                 RefObject namedNodeMapModel =
208                     model.refClass("NamedNodeMap").refCreateInstance(null);
209                 Collection JavaDoc items =
210                     (Collection JavaDoc) namedNodeMapModel.refGetValue("items");
211                 NamedNodeMap JavaDoc nl = element.getAttributes();
212                 for (int i = 0; i < nl.getLength(); i++) {
213                     RefObject child = nodeTemplate(nl.item(i));
214                     items.add(child);
215                     child.refSetValue("ownerElement", elementModel);
216                 }
217                 elementModel.refSetValue("attributes", namedNodeMapModel);
218             }
219             elementModel.refSetValue("tagName", element.getTagName());
220             if (!setRoot && document!=null) {document.refSetValue("rootNode" , elementModel);setRoot=true;}
221             return elementModel;
222         }
223         return null;
224     }
225
226     public RefObject attributeTemplate(Attr JavaDoc attribute) {
227         RefObject attributeModel = null;
228         if (attribute != null) {
229             attributeModel = model.refClass("Attr").refCreateInstance(null);
230             attributeModel.refSetValue("name", attribute.getName());
231             attributeModel.refSetValue(
232                 "specified",
233                 new Boolean JavaDoc(attribute.getSpecified()));
234             attributeModel.refSetValue("value", attribute.getValue());
235         }
236         return attributeModel;
237     }
238
239     public RefObject commentTemplate(Comment JavaDoc comment) {
240         RefObject commentModel = null;
241         if (comment != null) {
242             commentModel = model.refClass("Comment").refCreateInstance(null);
243             commentModel.refSetValue("data" , comment.getData());
244         }
245         return commentModel;
246     }
247
248     public RefObject textTemplate(Text JavaDoc text) {
249         RefObject textModel = null;
250         if (text != null) {
251             textModel = model.refClass("Text").refCreateInstance(null);
252             textModel.refSetValue("data", text.getData());
253         }
254         return textModel;
255     }
256
257     public RefObject cdataSectionTemplate(CDATASection JavaDoc cdata) {
258         RefObject cdataModel = null;
259         if (cdata != null) {
260             cdataModel = model.refClass("CDATASection").refCreateInstance(null);
261             cdataModel.refSetValue("data", cdata.getData());
262         }
263         return cdataModel;
264     }
265
266     public RefObject processingInstructionTemplate(ProcessingInstruction JavaDoc instruction) {
267         RefObject instructionModel = null;
268         if (instruction != null) {
269             instructionModel =
270                 model.refClass("CDATASection").refCreateInstance(null);
271             instructionModel.refSetValue("data", instruction.getData());
272             instructionModel.refSetValue("target", instruction.getTarget());
273         }
274         return instructionModel;
275     }
276 }
277
Popular Tags