1 4 5 package com.icesoft.jsfmeta.eclipse; 6 7 import java.io.File ; 8 import java.io.IOException ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 import java.util.TreeMap ; 12 import javax.xml.parsers.DocumentBuilder ; 13 import javax.xml.parsers.DocumentBuilderFactory ; 14 import javax.xml.parsers.ParserConfigurationException ; 15 import org.w3c.dom.Document ; 16 import org.w3c.dom.Element ; 17 import org.w3c.dom.NamedNodeMap ; 18 import org.w3c.dom.Node ; 19 import org.xml.sax.ErrorHandler ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.SAXParseException ; 22 23 public class CMParserHelper { 24 25 private TreeMap propertyTypeTreeMap = new TreeMap (); 26 27 public CMParserHelper() { 28 } 29 30 31 public void getInfo(){ 32 try { 33 34 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 35 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 36 37 documentBuilder.setErrorHandler(new ErrorHandler () { 38 public void error(SAXParseException e) { 39 e.printStackTrace(); 40 } 41 42 public void fatalError(SAXParseException e) throws SAXException { 43 e.printStackTrace(); 44 } 45 46 public void warning(SAXParseException e) { 47 e.printStackTrace(); 48 } 49 }); 50 51 Document document = documentBuilder.parse(new File ("./src/main/resources/conf/eclipse.schema/jsfhtml.xml")); 52 53 Element element = document.getDocumentElement(); 54 traversal(element, "\t"); 55 56 for(Iterator iterator = propertyTypeTreeMap.entrySet().iterator(); iterator.hasNext();){ 57 Map.Entry entry = (Map.Entry )iterator.next(); 58 System.out.println("key="+entry.getKey()+" value="+ entry.getValue()); 59 } 60 } catch (SAXException ex) { 61 ex.printStackTrace(); 62 } catch (ParserConfigurationException ex) { 63 ex.printStackTrace(); 64 } catch (IOException ex) { 65 ex.printStackTrace(); 66 } 67 } 68 69 public void traversal(Element element, String indentString){ 70 71 for(Node firstChild = element.getFirstChild();firstChild != null; 72 firstChild = firstChild.getNextSibling()){ 73 74 int nodeType = firstChild.getNodeType(); 75 switch(nodeType){ 76 case Node.ELEMENT_NODE:{ 77 NamedNodeMap map = firstChild.getAttributes(); 78 Object objectValue = map.getNamedItem("type"); 79 if(objectValue != null && objectValue instanceof Node ){ 80 Node attrImpl = (Node )objectValue; 81 propertyTypeTreeMap.put(attrImpl.getNodeValue(), attrImpl.getNodeValue()+"_type"); 82 } 83 traversal((Element )firstChild, indentString); 84 } 85 case Node.TEXT_NODE:{ 86 87 } 88 } 89 } 90 } 91 92 } 93
| Popular Tags
|