1 20 package org.objectweb.modfact.corba.xmiio.importer; 21 22 import java.io.InputStream ; 23 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 27 28 33 public abstract class XMIImportParser extends XMIImport { 34 35 36 protected org.w3c.dom.Element _document; 37 38 43 public java.lang.Object parse(String file) 44 throws 45 org.xml.sax.SAXException , 46 javax.xml.parsers.ParserConfigurationException , 47 java.io.FileNotFoundException , 48 java.io.IOException , 49 org.omg.mof.Reflective.MofError { 50 java.io.FileInputStream _xml_input_file = new java.io.FileInputStream (file); 52 return parse(_xml_input_file); 53 } 54 55 60 public java.lang.Object parse(InputStream xml_input_file) 61 throws 62 org.xml.sax.SAXException , 63 javax.xml.parsers.ParserConfigurationException , 64 java.io.FileNotFoundException , 65 java.io.IOException , 66 org.omg.mof.Reflective.MofError { 67 68 java.lang.Object _package = null; 70 71 DocumentBuilderFactory _factory = DocumentBuilderFactory.newInstance(); 72 _factory.setNamespaceAware(true); 73 _factory.setIgnoringComments(true); 74 _factory.setIgnoringElementContentWhitespace(true); 75 _factory.setCoalescing(true); 76 77 DocumentBuilder _builder = _factory.newDocumentBuilder(); 78 79 org.w3c.dom.Document doc = _builder.parse(xml_input_file); 81 82 org.w3c.dom.Element _model = doc.getDocumentElement(); 84 _document = _model; 85 delSpaces(_model); 87 88 org.w3c.dom.NodeList _contents = _model.getChildNodes(); 90 for (int i = 0; i < _contents.getLength(); i++) { 91 org.w3c.dom.Element _element = (org.w3c.dom.Element ) _contents.item(i); 92 String _name = _element.getNodeName().trim(); 93 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 109 public void xmiHeaderTemplate(org.w3c.dom.Element _package_element) { 110 } 112 113 117 public org.omg.CORBA.portable.IDLEntity xmiContentTemplate( 118 org.w3c.dom.Element _package_element) 119 throws org.omg.mof.Reflective.MofError { 120 org.omg.CORBA.portable.IDLEntity _package = null; 121 org.w3c.dom.NodeList _contents = _package_element.getChildNodes(); 122 123 for (int i = 0; i < _contents.getLength(); i++) { 124 org.w3c.dom.Element _element = (org.w3c.dom.Element ) _contents.item(i); 125 _package = rootPackageTemplate(_element); 127 } 128 completeImport(); 129 return _package; 130 } 131 132 136 public abstract org.omg.CORBA.portable.IDLEntity rootPackageTemplate( 137 org.w3c.dom.Element _root_package_element) 138 throws org.omg.mof.Reflective.MofError ; 139 140 143 public abstract void completeImport () 144 throws org.omg.mof.Reflective.MofError; 145 146 150 public void delSpaces(org.w3c.dom.Node node) { 151 try { 152 org.w3c.dom.NodeList _child = node.getChildNodes(); 153 int i = 0; 154 while (i < _child.getLength()) { 155 org.w3c.dom.Node _temp = _child.item(i); 156 if (_temp.getNodeType() == 3) { 157 String _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 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 |