1 7 package org.objectweb.modfact.metamodel_xml.io; 8 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.util.Collection ; 12 import java.util.Iterator ; 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 ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import javax.xml.parsers.ParserConfigurationException ; 21 22 import org.objectweb.modfact.jmi.reflect.RefPackageImpl; 23 import org.objectweb.modfact.jmi.xmi.XmiReaderImpl; 24 import org.w3c.dom.Attr ; 25 import org.w3c.dom.CDATASection ; 26 import org.w3c.dom.Comment ; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.DocumentType ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.NamedNodeMap ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 import org.w3c.dom.ProcessingInstruction ; 34 import org.w3c.dom.Text ; 35 import org.xml.sax.SAXException ; 36 37 43 public class Import { 44 45 RefPackage model; 46 RefObject document = null; 47 boolean setRoot=false; 48 49 public RefPackage parse(InputStream inputFile) 50 throws 51 SAXException , 52 ParserConfigurationException , 53 IOException , 54 MalformedXMIException { 55 56 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 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 inputFile, RefObject packageMetaObject) 78 throws 79 SAXException , 80 ParserConfigurationException , 81 IOException , 82 MalformedXMIException { 83 84 model = RefPackageImpl.getM1Repository(packageMetaObject); 85 86 88 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 89 DocumentBuilder builder = factory.newDocumentBuilder(); 90 91 Document doc = builder.parse(inputFile); 93 94 nodeTemplate(doc); 95 96 return model; 97 } 98 99 public RefObject documentTemplate(Document 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 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 node) { 126 RefObject element = null; 129 switch (node.getNodeType()) { 130 case Node.DOCUMENT_NODE : 131 element = documentTemplate((Document ) node); 132 element.refSetValue("nodeType","DOCUMENT_NODE"); 133 document = element; 134 break; 135 case Node.DOCUMENT_TYPE_NODE : 136 element = documentTypeTemplate((DocumentType )node); 137 element.refSetValue("nodeType","DOCUMENT_TYPE_NODE"); 138 document.refSetValue("doctype", element); 139 break; 140 case Node.ELEMENT_NODE : 141 element = elementTemplate((Element ) node); 142 element.refSetValue("nodeType","ELEMENT_NODE"); 143 break; 144 case Node.ATTRIBUTE_NODE : 145 element = attributeTemplate((Attr ) node); 146 element.refSetValue("nodeType","ATTRIBUTE_NODE"); 147 break; 148 case Node.COMMENT_NODE : 149 element = commentTemplate((Comment ) node); 150 element.refSetValue("nodeType","COMMENT_NODE"); 151 break; 152 case Node.TEXT_NODE : 153 element = textTemplate((Text ) node); 154 element.refSetValue("nodeType","TEXT_NODE"); 155 break; 156 case Node.CDATA_SECTION_NODE : 157 element = cdataSectionTemplate((CDATASection ) node); 158 element.refSetValue("nodeType","CDATA_SECTION_NODE"); 159 break; 160 case Node.PROCESSING_INSTRUCTION_NODE : 161 element = 162 processingInstructionTemplate((ProcessingInstruction ) node); 163 element.refSetValue("nodeType","PROCESSING_INSTRUCTION_NODE"); 164 break; 165 } 166 if (element != null) { 167 if (node.hasChildNodes()) { 169 RefObject nodeListModel = 170 model.refClass("NodeList").refCreateInstance(null); 171 Collection items = 173 (Collection ) nodeListModel.refGetValue("items"); 174 NodeList 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 if (document != null) 197 element.refSetValue("ownerDocument", document); 198 } 199 return element; 200 } 201 202 public RefObject elementTemplate(Element 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 items = 210 (Collection ) namedNodeMapModel.refGetValue("items"); 211 NamedNodeMap 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 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 (attribute.getSpecified())); 234 attributeModel.refSetValue("value", attribute.getValue()); 235 } 236 return attributeModel; 237 } 238 239 public RefObject commentTemplate(Comment 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 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 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 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 |