KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * copyright 2002 2004 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.jmi.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  * An abstract implementation of an XMI Import.
29  * This class provides common functions for all XMI importers,
30  * and generic functions for parsing files thanks to the XMIImportUtils inheritance
31  */

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

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

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

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

114     public java.lang.Object JavaDoc xmiContentTemplate(
115         org.w3c.dom.Element JavaDoc _package_element) {
116         java.lang.Object JavaDoc _package = null;
117         org.w3c.dom.NodeList JavaDoc _contents = _package_element.getChildNodes();
118
119         for (int i = 0; i < _contents.getLength(); i++) {
120             org.w3c.dom.Element JavaDoc _element = (org.w3c.dom.Element JavaDoc) _contents.item(i);
121             // The package
122
_package = rootPackageTemplate(_element);
123         }
124         completeImport();
125         return _package;
126     }
127
128     /**
129      * The root package processing.
130      * @param _root_package_element The XML element representing the package.
131      */

132     public abstract java.lang.Object JavaDoc rootPackageTemplate(
133         org.w3c.dom.Element JavaDoc _root_package_element);
134
135     /**
136      * This method is called at the end of the XMI importation.
137      */

138     public abstract void completeImport ();
139         
140     /**
141      * Delete the nodes with empty text.
142      * @param node The root node.
143      */

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