Author: scabanovich
Date: 2007-08-17 08:26:40 -0400 (Fri, 17 Aug 2007)
New Revision: 3215
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/meta/impl/MetaLibLoader.java
Log:
EXIN-160 - Loading of meta model is sped up.
Modified:
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/meta/impl/MetaLibLoader.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/meta/impl/MetaLibLoader.java 2007-08-17
12:25:50 UTC (rev 3214)
+++
trunk/common/plugins/org.jboss.tools.common.model/src/org/jboss/tools/common/meta/impl/MetaLibLoader.java 2007-08-17
12:26:40 UTC (rev 3215)
@@ -10,17 +10,27 @@
******************************************************************************/
package org.jboss.tools.common.meta.impl;
+import org.xml.sax.ContentHandler;
import java.io.*;
import java.util.*;
import java.net.*;
-import org.eclipse.core.runtime.FileLocator;
import org.w3c.dom.*;
+import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.DefaultHandler;
+import org.xml.sax.helpers.XMLReaderFactory;
+import org.jboss.tools.common.CommonPlugin;
import org.jboss.tools.common.model.plugin.ModelPlugin;
import org.jboss.tools.common.model.util.*;
+import org.jboss.tools.common.xml.SAXValidator;
import org.jboss.tools.common.xml.XMLEntityResolver;
+import org.jboss.tools.common.xml.XMLEntityResolverImpl;
+import org.jboss.tools.common.xml.XMLUtilities;
public class MetaLibLoader {
public static String DOC_PUBLICID = "-//Red Hat, Inc.//DTD Meta 1.0//EN";
@@ -38,10 +48,10 @@
private XModelMetaDataImpl meta = null;
-// private String prefix = "meta/";
-// private String suffix = ".meta";
private HashSet<String> metas = new HashSet<String>();
private ArrayList<ModuleRef> metarefs = new ArrayList<ModuleRef>();
+
+ private Element root = XMLUtilities.createDocumentElement("meta");
public MetaLibLoader() {}
@@ -59,12 +69,12 @@
ModelPlugin.getPluginLog().logError("Error in loading meta model
resources", t);
}
- long t = System.currentTimeMillis();
+// long t = System.currentTimeMillis();
for (int i = 0; i < metarefs.size(); i++) {
ModuleRef r = metarefs.get(i);
load(r.element, r.name, r.info);
}
- long dt = - t + (t = System.currentTimeMillis());
+// long dt = - t + (t = System.currentTimeMillis());
// System.out.println("Loaded from elements in " + dt + " ms");
}
@@ -82,26 +92,18 @@
}
void load(String name, URL url) {
- long t = System.currentTimeMillis();
+// long t = System.currentTimeMillis();
InputStream stream = null;
try {
stream = url.openStream();
+ stream = new BufferedInputStream(stream, 16384);
} catch (Exception e) {
ModelPlugin.getPluginLog().logError("MetaLoader: Cannot read resource " +
url.toString());
return;
}
- Element g = XMLUtil.getElement(stream);
- Serializable s = (Serializable)g;
-// try {
-// ByteArrayOutputStream o = new ByteArrayOutputStream();
-// BufferedOutputStream b = new BufferedOutputStream(o);
-// ObjectOutputStream os = new ObjectOutputStream(b);
-// os.writeObject(s);
-// os.close();
-// System.out.println(o.toByteArray().length);
-// } catch (Exception e) {
-// e.printStackTrace();
-// }
+
+ Element g = parse(stream);
+ //XMLUtil.getElement(stream);
if(g == null) {
ModelPlugin.getPluginLog().logInfo("Corrupted meta resource " + name);
} else try {
@@ -110,7 +112,7 @@
ModelPlugin.getPluginLog().logError(e);
}
- long dt = - t + (t = System.currentTimeMillis());
+// long dt = - t + (t = System.currentTimeMillis());
// System.out.println("Loaded " + url + " in " + dt + "
ms");
if(validateMetaXML) {
try {
@@ -126,11 +128,26 @@
} catch (Exception e) {
ModelPlugin.getPluginLog().logError(e);
}
- dt = - t + (t = System.currentTimeMillis());
+// dt = - t + (t = System.currentTimeMillis());
// System.out.println("Validated " + url + " in " + dt + "
ms");
}
}
+
+ Element parse(InputStream stream) {
+ Parser p = new Parser();
+ p.documentElement = root;
+ p.current = root;
+ try {
+ p.parse(stream);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ Element g = p.documentElement;
+ g = XMLUtilities.getUniqueChild(g, "XModelEntityGroup");
+ p.documentElement.removeChild(g);
+ return g;
+ }
void load0(Element g, String name, String source) {
metarefs.add(new ModuleRef(g, name, source));
@@ -174,28 +191,116 @@
while(st.hasMoreTokens()) depends.add(st.nextToken());
}
}
-/*
+
public boolean acceptable(Set modules) {
- if(key.length() == 0 || !modules.contains(key)) {
- ModelPlugin.log("Meta source " + info + " is not covered by
license.");
- return false;
+ return (true || key.endsWith("1.0"));
+ }
+
+}
+
+class Parser implements ContentHandler {
+ protected static final String FATAL_ERROR_PROCESSING_FEATURE_ID =
"http://apache.org/xml/features/continue-after-fatal-error";
+ protected static final String ENTITY_RESOLVER_PROPERTY_ID =
"http://apache.org/xml/properties/internal/entity-resolver";
+ protected static final String NAMESPACES_FEATURE_ID =
"http://xml.org/sax/features/namespaces";
+ protected static final String NAMESPACE_PREFIXES_FEATURE_ID =
"http://xml.org/sax/features/namespace-prefixes";
+ protected static final String VALIDATION_FEATURE_ID =
"http://xml.org/sax/features/validation";
+ protected static final String VALIDATION_SCHEMA_FEATURE_ID =
"http://apache.org/xml/features/validation/schema";
+ protected static final String VALIDATION_SCHEMA_CHECKING_FEATURE_ID =
"http://apache.org/xml/features/validation/schema-full-checking";
+ protected static final String VALIDATION_DYNAMIC_FEATURE_ID =
"http://apache.org/xml/features/validation/dynamic";
+
+ protected static final String DEFAULT_SAX_PARSER_CLASS_NAME =
"org.apache.xerces.parsers.SAXParser";
+
+ Element documentElement = null;
+ Element current = null;
+
+ public void parse(InputStream is) {
+ parse(new InputSource(is));
+ }
+
+ public void parse(org.xml.sax.InputSource is) {
+ XMLReader parser = createParser();
+ try {
+ parser.parse(is);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ XMLReader createParser() {
+ DefaultHandler handler = new DefaultHandler();
+ XMLReader parserInstance = null;
+
+ try {
+ parserInstance =
XMLReaderFactory.createXMLReader(DEFAULT_SAX_PARSER_CLASS_NAME);
+ } catch (Exception e) {
+ return null;
}
- for (int i = 0; i < depends.size(); i++) {
- String k = (String)depends.get(i);
- if(modules.contains(k)) continue;
- if(modules.contains(key)) {
- modules.remove(key);
- ModelPlugin.log("Module " + key + " is not covered by
license.");
- }
- ModelPlugin.log("Meta source " + info + " is not covered by
license.");
- return false;
+
+ SAXValidator.setFeature(parserInstance, NAMESPACES_FEATURE_ID, false);
+ SAXValidator.setFeature(parserInstance, NAMESPACE_PREFIXES_FEATURE_ID, false);
+ SAXValidator.setFeature(parserInstance, VALIDATION_FEATURE_ID, false);
+ SAXValidator.setFeature(parserInstance, VALIDATION_SCHEMA_FEATURE_ID, false);
+ SAXValidator.setFeature(parserInstance, VALIDATION_SCHEMA_CHECKING_FEATURE_ID,
false);
+ SAXValidator.setFeature(parserInstance, VALIDATION_DYNAMIC_FEATURE_ID, false);
+ SAXValidator.setFeature(parserInstance, FATAL_ERROR_PROCESSING_FEATURE_ID,
false);
+
+ try {
+ parserInstance.setProperty(ENTITY_RESOLVER_PROPERTY_ID, new
XMLEntityResolverImpl());
+ } catch (Exception e1) {
+ CommonPlugin.getPluginLog().logError( e1.getMessage()+"", e1);
}
- return true;
+
+ parserInstance.setContentHandler(handler);
+ parserInstance.setErrorHandler(handler);
+ parserInstance.setContentHandler(this);
+ return parserInstance;
}
-*/
- public boolean acceptable(Set modules) {
- return (true || key.endsWith("1.0"));
- }
+
+ //SAXParser
+ public void characters(char[] ch, int start, int length) throws SAXException {
+ }
+
+ public void endDocument() throws SAXException {
+ }
+
+ public void endElement(String uri, String localName, String name) throws SAXException {
+ Node p = current.getParentNode();
+ if(p instanceof Element) {
+ current = (Element)p;
+ }
+ }
+
+ public void endPrefixMapping(String prefix) throws SAXException {}
+
+ public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
{}
+
+ public void processingInstruction(String target, String data) throws SAXException {
+ }
+
+ public void setDocumentLocator(Locator locator) {}
+
+ public void skippedEntity(String name) throws SAXException {}
+
+ public void startDocument() throws SAXException {
+ }
+
+ public void startElement(String uri, String localName, String name,
+ Attributes atts) throws SAXException {
+ if(current == null) {
+ current = XMLUtilities.createDocumentElement(name);
+ documentElement = current;
+ } else {
+ current = XMLUtilities.createElement(current, name);
+ }
+ int l = atts.getLength();
+ for (int i = 0; i < l; i++) {
+ String n = atts.getQName(i);
+ String v = atts.getValue(i);
+ current.setAttribute(n, v);
+ }
+ }
+
+ public void startPrefixMapping(String prefix, String uri) throws SAXException {}
+
}
-