KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > xmiio > importer > XMIImportParser


1 /**
2  * copyright 2002 2003 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.corba.xmiio.importer;
21
22 import java.io.InputStream JavaDoc;
23
24 import javax.xml.parsers.DocumentBuilder JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26
27
28 /**
29  * An abstract implementation of an XMI Import.
30  * This class provides common functions for all XMI importers,
31  * and generic functions for parsing files thanks to the XMIImportUtils inheritance
32  */

33 public abstract class XMIImportParser extends XMIImport {
34
35     /** The XMI Document. */
36     protected org.w3c.dom.Element JavaDoc _document;
37
38     /**
39      * Parse the XMI File.
40      * @param file The XMI Filename.
41      * @return The package read.
42      */

43     public java.lang.Object JavaDoc parse(String JavaDoc file)
44         throws
45             org.xml.sax.SAXException JavaDoc,
46             javax.xml.parsers.ParserConfigurationException JavaDoc,
47             java.io.FileNotFoundException JavaDoc,
48             java.io.IOException JavaDoc,
49             org.omg.mof.Reflective.MofError {
50         // Load the document
51
java.io.FileInputStream JavaDoc _xml_input_file = new java.io.FileInputStream JavaDoc(file);
52         return parse(_xml_input_file);
53     }
54
55     /**
56      * Parse the XMI File.
57      * @param _xml_input_file The Input Stream of XMI File.
58      * @return The package read.
59      */

60     public java.lang.Object JavaDoc parse(InputStream JavaDoc xml_input_file)
61         throws
62             org.xml.sax.SAXException JavaDoc,
63             javax.xml.parsers.ParserConfigurationException JavaDoc,
64             java.io.FileNotFoundException JavaDoc,
65             java.io.IOException JavaDoc,
66             org.omg.mof.Reflective.MofError {
67
68         // The package
69
java.lang.Object JavaDoc _package = null;
70
71         DocumentBuilderFactory JavaDoc _factory = DocumentBuilderFactory.newInstance();
72         _factory.setNamespaceAware(true);
73         _factory.setIgnoringComments(true);
74         _factory.setIgnoringElementContentWhitespace(true);
75         _factory.setCoalescing(true);
76
77         DocumentBuilder JavaDoc _builder = _factory.newDocumentBuilder();
78
79         // Load the document
80
org.w3c.dom.Document JavaDoc doc = _builder.parse(xml_input_file);
81
82         // Parse the document
83
org.w3c.dom.Element JavaDoc _model = doc.getDocumentElement();
84         _document = _model;
85         // Delete spaces
86
delSpaces(_model);
87
88         // Get contents
89
org.w3c.dom.NodeList JavaDoc _contents = _model.getChildNodes();
90         for (int i = 0; i < _contents.getLength(); i++) {
91             org.w3c.dom.Element JavaDoc _element = (org.w3c.dom.Element JavaDoc) _contents.item(i);
92             String JavaDoc _name = _element.getNodeName().trim();
93             // The package
94
if (_name.endsWith("XMI.header")) {
95                 xmiHeaderTemplate(_element);
96             } else if (_name.endsWith("XMI.content")) {
97                 _package = xmiContentTemplate(_element);
98             } else {
99                 System.err.println("Unknown : " + _name);
100             }
101         }
102         return _package;
103     }
104
105     /**
106      * Parse the header of the XMI File.
107      * @param _package_element The XML element representing the root package.
108      */

109     public void xmiHeaderTemplate(org.w3c.dom.Element JavaDoc _package_element) {
110         // The XMI header is not parsed.
111
}
112
113     /**
114      * Parse the content of the XMI File.
115      * @param _package_element The XML element representing the root package.
116      */

117     public org.omg.CORBA.portable.IDLEntity JavaDoc xmiContentTemplate(
118         org.w3c.dom.Element JavaDoc _package_element)
119         throws org.omg.mof.Reflective.MofError {
120         org.omg.CORBA.portable.IDLEntity JavaDoc _package = null;
121         org.w3c.dom.NodeList JavaDoc _contents = _package_element.getChildNodes();
122
123         for (int i = 0; i < _contents.getLength(); i++) {
124             org.w3c.dom.Element JavaDoc _element = (org.w3c.dom.Element JavaDoc) _contents.item(i);
125             // The package
126
_package = rootPackageTemplate(_element);
127         }
128         completeImport();
129         return _package;
130     }
131
132     /**
133      * The root package processing.
134      * @param _root_package_element The XML element representing the package.
135      */

136     public abstract org.omg.CORBA.portable.IDLEntity JavaDoc rootPackageTemplate(
137         org.w3c.dom.Element JavaDoc _root_package_element)
138         throws org.omg.mof.Reflective.MofError ;
139
140     /**
141      * This method is called at the end of the XMI importation.
142      */

143     public abstract void completeImport ()
144         throws org.omg.mof.Reflective.MofError;
145         
146     /**
147      * Delete the nodes with empty text.
148      * @param node The root node.
149      */

150     public void delSpaces(org.w3c.dom.Node JavaDoc node) {
151         try {
152             org.w3c.dom.NodeList JavaDoc _child = node.getChildNodes();
153             int i = 0;
154             while (i < _child.getLength()) {
155                 org.w3c.dom.Node JavaDoc _temp = _child.item(i);
156                 if (_temp.getNodeType() == 3) {
157                     String JavaDoc _temp1 = _temp.getNodeValue();
158                     if (_temp1.trim().length() == 0)
159                         node.removeChild(_temp);
160                     else
161                         i++;
162                 } else {
163                     delSpaces(_temp);
164                     i++;
165                 }
166             }
167         } catch (org.w3c.dom.DOMException JavaDoc domException) {
168             switch (domException.code) {
169                 case org.w3c.dom.DOMException.NO_MODIFICATION_ALLOWED_ERR :
170                     System.err.println(
171                         "You attempt to modify an object where modifications are not allowed.");
172                     break;
173                 case org.w3c.dom.DOMException.NOT_FOUND_ERR :
174                     System.err.println(
175                         "You attempt to reference a node in a context where it does not exist.");
176                     break;
177             }
178         }
179     }
180
181 }
182
Popular Tags