1 20 package org.objectweb.modfact.jmi.xmiio.importer; 21 22 import java.io.InputStream ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 27 32 public abstract class XMIImportParser extends XMIImport { 33 34 35 protected org.w3c.dom.Element _document; 36 37 42 public java.lang.Object parse(String file) 43 throws 44 org.xml.sax.SAXException , 45 javax.xml.parsers.ParserConfigurationException , 46 java.io.FileNotFoundException , 47 java.io.IOException { 48 java.io.FileInputStream _xml_input_file = new java.io.FileInputStream (file); 50 return parse(_xml_input_file); 51 } 52 53 58 public java.lang.Object parse(InputStream xml_input_file) 59 throws 60 org.xml.sax.SAXException , 61 javax.xml.parsers.ParserConfigurationException , 62 java.io.FileNotFoundException , 63 java.io.IOException { 64 65 java.lang.Object _package = null; 67 68 DocumentBuilderFactory _factory = DocumentBuilderFactory.newInstance(); 69 _factory.setNamespaceAware(true); 70 _factory.setIgnoringComments(true); 71 _factory.setIgnoringElementContentWhitespace(true); 72 _factory.setCoalescing(true); 73 74 DocumentBuilder _builder = _factory.newDocumentBuilder(); 75 76 org.w3c.dom.Document doc = _builder.parse(xml_input_file); 78 79 org.w3c.dom.Element _model = doc.getDocumentElement(); 81 _document = _model; 82 delSpaces(_model); 84 85 org.w3c.dom.NodeList _contents = _model.getChildNodes(); 87 for (int i = 0; i < _contents.getLength(); i++) { 88 org.w3c.dom.Element _element = (org.w3c.dom.Element ) _contents.item(i); 89 String _name = _element.getNodeName().trim(); 90 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 106 public void xmiHeaderTemplate(org.w3c.dom.Element _package_element) { 107 } 109 110 114 public java.lang.Object xmiContentTemplate( 115 org.w3c.dom.Element _package_element) { 116 java.lang.Object _package = null; 117 org.w3c.dom.NodeList _contents = _package_element.getChildNodes(); 118 119 for (int i = 0; i < _contents.getLength(); i++) { 120 org.w3c.dom.Element _element = (org.w3c.dom.Element ) _contents.item(i); 121 _package = rootPackageTemplate(_element); 123 } 124 completeImport(); 125 return _package; 126 } 127 128 132 public abstract java.lang.Object rootPackageTemplate( 133 org.w3c.dom.Element _root_package_element); 134 135 138 public abstract void completeImport (); 139 140 144 public void delSpaces(org.w3c.dom.Node node) { 145 try { 146 org.w3c.dom.NodeList _child = node.getChildNodes(); 147 int i = 0; 148 while (i < _child.getLength()) { 149 org.w3c.dom.Node _temp = _child.item(i); 150 if (_temp.getNodeType() == 3) { 151 String _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 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 |