1 19 package org.netbeans.modules.xml.axi.util; 20 21 import java.io.File ; 22 import java.io.FileReader ; 23 import java.net.URL ; 24 import javax.xml.parsers.SAXParser ; 25 import javax.xml.parsers.SAXParserFactory ; 26 import org.netbeans.modules.xml.axi.AXIComponent; 27 import org.netbeans.modules.xml.axi.AXIComponentFactory; 28 import org.netbeans.modules.xml.axi.AXIDocument; 29 import org.netbeans.modules.xml.axi.AXIModel; 30 import org.netbeans.modules.xml.axi.Attribute; 31 import org.netbeans.modules.xml.axi.Compositor; 32 import org.netbeans.modules.xml.axi.Element; 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.InputSource ; 35 import org.xml.sax.helpers.DefaultHandler ; 36 37 41 public class FileUtil { 42 43 private FileReader fileReader; 44 45 48 private FileUtil() { 49 } 50 51 public static FileUtil getInstance() { 52 return new FileUtil(); 53 } 54 55 InputSource openFile(URL url) throws Exception { 56 File file = new File (url.toURI()); 57 if(file == null || !file.exists()) { 58 return null; 59 } 60 fileReader = new FileReader (file); 61 InputSource inputSource = new InputSource (fileReader); 62 if(inputSource == null) { 63 return null; 64 } 65 66 return inputSource; 67 } 68 69 void closeFile() { 70 try { 71 if(fileReader != null) 72 fileReader.close(); 73 } catch(Exception ex) { 74 ex.printStackTrace(); 75 } 76 } 77 78 82 public static void parseXMLAndPopulateAXIModel(URL url, AXIModel model) { 83 FileUtil util = FileUtil.getInstance(); 84 MyContentHandler handler = new MyContentHandler(model); 85 try { 86 model.startTransaction(); 87 InputSource inputSource = util.openFile(url); 88 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); 89 saxParser.parse(inputSource, handler); 90 } catch (Exception ex) { 91 ex.printStackTrace(); 92 } finally { 93 try { 94 model.endTransaction(); 95 util.closeFile(); 96 } catch (Exception ex) { 97 } 99 } 100 } 101 102 public static class MyContentHandler extends DefaultHandler { 103 private AXIComponent root; 104 private java.util.Stack <AXIComponent> stack; 105 private AXIModel model; 106 private AXIComponentFactory factory; 107 108 MyContentHandler(AXIModel model) { 109 this.model = model; 110 this.factory = model.getComponentFactory(); 111 this.stack = new java.util.Stack <AXIComponent>(); 112 } 113 114 public void startElement(String uri, String localName, 115 String qName, Attributes atts) { 116 117 AXIComponent newChild = createCompositor(qName); 118 if(newChild == null) { 119 Element element = factory.createElement(); 120 element.setName(qName); 121 for(int i=0; i<atts.getLength(); i++) { 122 Attribute attr = factory.createAttribute(); 123 attr.setName(atts.getQName(i)); 124 element.addAttribute(attr); 125 } 126 newChild = element; 127 } 128 addChild(newChild); 129 } 130 131 public void endElement(String uri, String localName, 132 String qName) { 133 AXIComponent component = stack.peek(); 134 if(component instanceof Compositor) { 135 Compositor c = (Compositor)component; 136 if(c.toString().equals(qName)) { 137 stack.pop(); 138 } 139 } 140 if(component instanceof Element) { 141 Element e = (Element)component; 142 if(e.getName().equals(qName)) { 143 stack.pop(); 144 } 145 } 146 } 147 148 private Compositor createCompositor(String qName) { 149 if(qName.equals("Sequence")) { 150 return factory.createSequence(); 151 } 152 if(qName.equals("Choice")) { 153 return factory.createChoice(); 154 } 155 if(qName.equals("All")) { 156 return factory.createAll(); 157 } 158 159 return null; 160 } 161 162 private void addChild(AXIComponent child) { 163 AXIComponent parent = null; 164 if(stack.empty()) { 165 parent = model.getRoot(); 166 addChild(parent, child); 167 stack.push(parent); 168 } else { 169 parent = stack.peek(); 170 addChild(parent, child); 171 } 172 stack.push(child); 173 } 174 175 private void addChild(AXIComponent parent, AXIComponent child) { 176 if(parent instanceof AXIDocument) { 177 AXIDocument doc = (AXIDocument)parent; 178 if(child instanceof Element) doc.addElement((Element)child); 179 } 180 181 if(parent instanceof Element) { 182 Element element = (Element)parent; 183 if(child instanceof Attribute) element.addAttribute((Attribute)child); 184 if(child instanceof Compositor) element.addCompositor((Compositor)child); 185 if(child instanceof Element) element.addElement((Element)child); 186 } 187 188 if(parent instanceof Compositor) { 189 Compositor compositor = (Compositor)parent; 190 if(child instanceof Compositor) compositor.addCompositor((Compositor)child); 191 if(child instanceof Element) compositor.addElement((Element)child); 192 } 193 } 194 195 } 196 197 } 198 | Popular Tags |