Author: snjeza
Date: 2010-03-01 19:18:28 -0500 (Mon, 01 Mar 2010)
New Revision: 20550
Added:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/XHTMLEntityResolver.java
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/XHTMLSyntaxValidator.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-5923 After several files deletion eclipse
decelerate and takes 50% cpu time
Added:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/XHTMLEntityResolver.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/XHTMLEntityResolver.java
(rev 0)
+++
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/XHTMLEntityResolver.java 2010-03-02
00:18:28 UTC (rev 20550)
@@ -0,0 +1,134 @@
+package org.jboss.tools.jsf.web.validation;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.xerces.xni.XMLResourceIdentifier;
+import org.apache.xerces.xni.XNIException;
+import org.apache.xerces.xni.parser.XMLEntityResolver;
+import org.apache.xerces.xni.parser.XMLInputSource;
+import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver;
+import org.eclipse.wst.xml.core.internal.validation.XMLNestedValidatorContext;
+import org.eclipse.wst.xml.core.internal.validation.core.LazyURLInputStream;
+import org.eclipse.wst.xml.core.internal.validation.core.NestedValidatorContext;
+
+/**
+ * A custom entity resolver that uses the URI resolver specified to resolve
+ * entities.
+ */
+public class XHTMLEntityResolver implements XMLEntityResolver {
+ private URIResolver uriResolver;
+ private String resolvedDTDLocation;
+ private NestedValidatorContext context;
+
+ /**
+ * Constructor.
+ *
+ * @param uriResolver
+ * The URI resolver to use with this entity resolver.
+ * @param context
+ * The XML validator context.
+ */
+ public XHTMLEntityResolver(URIResolver uriResolver,
+ NestedValidatorContext context) {
+ this.uriResolver = uriResolver;
+ this.context = context;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.apache.xerces.xni.parser.XMLEntityResolver#resolveEntity(org.apache
+ * .xerces.xni.XMLResourceIdentifier)
+ */
+ public XMLInputSource resolveEntity(XMLResourceIdentifier rid)
+ throws XNIException, IOException {
+ XMLInputSource inputSource = _internalResolveEntity(uriResolver, rid,
+ context);
+ if (inputSource != null) {
+ resolvedDTDLocation = inputSource.getSystemId();
+ }
+ return inputSource;
+ }
+
+ public String getLocation() {
+ return resolvedDTDLocation;
+ }
+
+ // cs : I've refactored the common SAX based resolution code into this
+ // method for use by other validators
+ // (i.e. XML Schema, WSDL etc). The other approach is maintain a copy for
+ // each validator that has
+ // identical code. In any case we should strive to ensure that the
+ // validators perform resolution consistently.
+ public static XMLInputSource _internalResolveEntity(
+ URIResolver uriResolver, XMLResourceIdentifier rid)
+ throws IOException {
+ return _internalResolveEntity(uriResolver, rid, null);
+ }
+
+ public static XMLInputSource _internalResolveEntity(
+ URIResolver uriResolver, XMLResourceIdentifier rid,
+ NestedValidatorContext context) throws IOException {
+ XMLInputSource is = null;
+
+ if (uriResolver != null) {
+ String id = rid.getPublicId();
+ if (id == null) {
+ id = rid.getNamespace();
+ }
+
+ String location = null;
+ if (id != null || rid.getLiteralSystemId() != null) {
+ location = uriResolver.resolve(rid.getBaseSystemId(), id, rid
+ .getLiteralSystemId());
+ }
+
+ if (location != null) {
+ String physical = uriResolver.resolvePhysicalLocation(rid
+ .getBaseSystemId(), id, location);
+
+ // if physical is already a known bad uri, just go ahead and
+ // throw an exception
+ if (context instanceof XMLNestedValidatorContext) {
+ XMLNestedValidatorContext xmlContext = ((XMLNestedValidatorContext) context);
+
+ if (xmlContext.isURIMarkedInaccessible(physical)) {
+ throw new FileNotFoundException(physical);
+ }
+ }
+
+ is = new XMLInputSource(rid.getPublicId(), location, location);
+
+ // This block checks that the file exists. If it doesn't we need
+ // to throw
+ // an exception so Xerces will report an error. note: This may
+ // not be
+ // necessary with all versions of Xerces but has specifically
+ // been
+ // experienced with the version included in IBM's 1.4.2 JDK.
+ InputStream isTemp = null;
+ try {
+ isTemp = new URL(physical).openStream();
+ } catch (IOException e) {
+ // physical was a bad url, so cache it so we know next time
+ if (context instanceof XMLNestedValidatorContext) {
+ XMLNestedValidatorContext xmlContext = ((XMLNestedValidatorContext) context);
+ xmlContext.markURIInaccessible(physical);
+ }
+ throw e;
+ } finally {
+ if (isTemp != null) {
+ isTemp.close();
+ }
+ }
+ is.setByteStream(new LazyURLInputStream(physical));
+ }
+ }
+ return is;
+ }
+
+}
Modified:
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/XHTMLSyntaxValidator.java
===================================================================
---
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/XHTMLSyntaxValidator.java 2010-03-01
21:36:01 UTC (rev 20549)
+++
trunk/jsf/plugins/org.jboss.tools.jsf/src/org/jboss/tools/jsf/web/validation/XHTMLSyntaxValidator.java 2010-03-02
00:18:28 UTC (rev 20550)
@@ -19,6 +19,7 @@
import java.io.StringReader;
import java.util.ArrayList;
+import org.apache.xerces.xni.parser.XMLEntityResolver;
import org.eclipse.core.resources.IResource;
import org.eclipse.wst.common.uriresolver.internal.provisional.URIResolverPlugin;
import org.eclipse.wst.validation.ValidationResult;
@@ -74,9 +75,9 @@
XMLValidationReport valreport = null;
if (inputstream != null) {
valreport = validator.validate(uri, inputstream, configuration,
- result);
+ context, result);
} else {
- valreport = validator.validate(uri, null, configuration, result);
+ valreport = validator.validate(uri, null, configuration, context, result);
}
if (JSFModelPlugin.getDefault().isDebugging()) {
@@ -131,7 +132,7 @@
* Returns an XML validation report.
*/
public XMLValidationReport validate(String uri, InputStream inputStream,
- XMLValidationConfiguration configuration, ValidationResult result) {
+ XMLValidationConfiguration configuration, NestedValidatorContext context,
ValidationResult result) {
String grammarFile = "";
Reader reader1 = null; // Used for the preparse.
Reader reader2 = null; // Used for validation parse.
@@ -143,8 +144,9 @@
}
XMLValidationInfo valinfo = new XMLValidationInfo(uri);
- XHTMLValidatorHelper helper = new XHTMLValidatorHelper();
-
+ XHTMLEntityResolver entityResolver = new XHTMLEntityResolver(uriResolver,
context);
+ XHTMLValidatorHelper helper = new XHTMLValidatorHelper(entityResolver);
+
try {
helper.computeValidationInformation(uri, reader1, uriResolver);
@@ -160,7 +162,7 @@
return valinfo;
}
- XMLReader reader = createXMLReader(valinfo, new XMLEntityResolverImpl());
+ XMLReader reader = createXMLReader(valinfo, entityResolver);
XMLErrorHandler errorhandler = new XMLErrorHandler(valinfo);
reader.setErrorHandler(errorhandler);
@@ -216,7 +218,12 @@
*/
class XHTMLValidatorHelper extends ValidatorHelper {
public boolean isXHTMLDoctype = false;
+ private XHTMLEntityResolver entityResolver;
+ public XHTMLValidatorHelper(XHTMLEntityResolver entityResolver) {
+ this.entityResolver = entityResolver;
+ }
+
protected XMLReader createXMLReader(String uri) throws Exception
{
XMLReader reader = super.createXMLReader(uri);
@@ -259,7 +266,9 @@
}
};
reader.setProperty("http://xml.org/sax/properties/lexical-handler",
lexicalHandler); //$NON-NLS-1$
-
reader.setProperty("http://apache.org/xml/properties/internal/entity...;,
new XMLEntityResolverImpl()); //$NON-NLS-1$
+ if (entityResolver != null) {
+
reader.setProperty("http://apache.org/xml/properties/internal/entity...;,
entityResolver); //$NON-NLS-1$
+ }
return reader;
}
}