1 19 20 package org.netbeans.modules.mdrxml; 21 22 import org.xml.sax.*; 23 24 import java.util.*; 25 import java.io.*; 26 27 import org.openide.*; 28 import org.openide.filesystems.*; 29 import org.openide.loaders.*; 30 import org.openide.xml.*; 31 import org.openide.util.*; 32 33 35 import org.netbeans.api.mdr.*; 36 import xmlmodel.*; 37 38 import javax.jmi.reflect.RefPackage; 39 import javax.jmi.model.*; 40 41 46 public class FillRepositoryAction extends org.netbeans.modules.mdrxml.util.XMLToolsAction { 47 48 private static final boolean DEBUG = false; 49 50 xmlmodel.Contains contains; 52 xmlmodel.TextNodeClass textNodeClass; 53 xmlmodel.RootNodeClass rootNodeClass; 54 xmlmodel.AttributeNodeClass attributeClass; 55 xmlmodel.ElementNodeClass nodeClass; 56 57 58 public FillRepositoryAction() { 59 } 60 61 public org.openide.util.HelpCtx getHelpCtx() { 62 return null; 63 } 64 65 public String getName() { 66 return "Fill MDRepository"; 67 } 68 69 protected void performXMLAction(FileObject fo) { 70 71 73 75 MDRepository repository = MDRManager.getDefault().getDefaultRepository(); 77 TopManager tm = TopManager.getDefault(); 78 79 XmlmodelPackage extent = (XmlmodelPackage) repository.getExtent(MDRXMLModule.XML_MODEL); 81 contains = extent.getContains(); 82 textNodeClass = extent.getTextNode(); 83 rootNodeClass = extent.getRootNode(); 84 attributeClass = extent.getAttributeNode(); 85 nodeClass = extent.getElementNode(); 86 87 try { 89 repository.beginTrans(true); 90 tm.setStatusText ("XML parsing started."); 91 92 String id = fo.getURL().toExternalForm(); 93 if (DEBUG) System.err.println("URL:" + id); 94 ParserImpl impl = new ParserImpl(id); 95 XMLReader reader = XMLUtil.createXMLReader(); 96 98 reader.setErrorHandler( new ErrorHandler() { 99 public void error(SAXParseException ex) throws SAXException { 100 throw ex; 101 } 102 public void warning(SAXParseException ex) throws SAXException { 103 throw ex; 104 } 105 106 public void fatalError(SAXParseException ex) throws SAXException { 107 throw ex; 108 } 109 110 }); 111 InputSource in = new InputSource(id); 112 in.setByteStream(fo.getInputStream()); 113 reader.setContentHandler(impl); 114 reader.parse(in); 115 } catch (IOException ex) { 116 tm.notifyException(ex); 118 } catch (SAXException ex) { 119 tm.notifyException(ex); 122 } finally { 123 repository.endTrans(); 124 tm.setStatusText ("XML parsing finished."); 125 } 126 } 127 128 private class ParserImpl implements ContentHandler { 129 130 private final StringBuffer content = new StringBuffer (); 131 private boolean root = true; 132 private final Stack parents = new Stack(); 133 private final String name; 134 135 public ParserImpl(String documentID) { 136 name = documentID; 137 } 138 139 public void characters(char[] values, int offset, int len) throws org.xml.sax.SAXException { 140 if (DEBUG) System.err.println("characters()"); 141 content.append(values, offset, len); 142 } 143 144 public void endDocument() throws org.xml.sax.SAXException { 145 } 146 147 public void startDocument() throws org.xml.sax.SAXException { 148 parents.push(null); 149 } 150 151 public void startElement(String uri, String local, String qname, org.xml.sax.Attributes attrs) throws org.xml.sax.SAXException { 152 153 if (DEBUG) System.err.println("startElement"); 154 155 String data = content.toString(); 156 content.setLength(0); 157 ElementNode me = (ElementNode) parents.peek(); 158 if (data.trim().length() > 0) { 159 Node text = textNodeClass.createTextNode(data); 160 161 contains.add(text, me); 163 } 164 165 ElementNode node; 167 if (root) { 168 node = rootNodeClass.createRootNode(qname, name); 169 root = false; 170 } else { 171 node = nodeClass.createElementNode(qname); 172 } 173 174 for (int i = 0; i<attrs.getLength(); i++) { 176 String name = attrs.getQName(i); 177 String val = attrs.getValue(i); 178 xmlmodel.AttributeNode attr = attributeClass.createAttributeNode(name, val); 179 contains.add(attr, node); 180 } 181 182 parents.push(node); 184 } 185 186 public void endElement(String str, String str1, String str2) throws org.xml.sax.SAXException { 187 188 if (DEBUG) System.err.println("endElement"); 189 190 String data = content.toString(); 191 content.setLength(0); 192 ElementNode me = (ElementNode) parents.pop(); 193 if (data.trim().length() > 0) { 194 Node text = textNodeClass.createTextNode(data); 195 196 contains.add(text, me); 198 } 199 200 ElementNode parent = (ElementNode) parents.peek(); 201 if (parent != null) { 202 contains.add(me, parent); 204 } 205 } 206 207 public void endPrefixMapping(String str) throws org.xml.sax.SAXException { 208 } 209 210 public void ignorableWhitespace(char[] values, int param, int param2) throws org.xml.sax.SAXException { 211 } 212 213 public void processingInstruction(String str, String str1) throws org.xml.sax.SAXException { 214 } 215 216 public void setDocumentLocator(org.xml.sax.Locator locator) { 217 } 218 219 public void skippedEntity(String str) throws org.xml.sax.SAXException { 220 } 221 222 public void startPrefixMapping(String str, String str1) throws org.xml.sax.SAXException { 223 } 224 225 } 226 } 227 | Popular Tags |