You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by il...@apache.org on 2013/02/25 14:46:35 UTC
svn commit: r1449701 -
/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java
Author: ilgrosso
Date: Mon Feb 25 13:46:35 2013
New Revision: 1449701
URL: http://svn.apache.org/r1449701
Log:
[COCOON3-71] Re-organizing methods
Modified:
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java
Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java?rev=1449701&r1=1449700&r2=1449701&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java (original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/SchemaProcessorTransformer.java Mon Feb 25 13:46:35 2013
@@ -22,6 +22,7 @@ import java.net.URL;
import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.ValidatorHandler;
@@ -68,98 +69,81 @@ public final class SchemaProcessorTransf
public SchemaProcessorTransformer(final URL url) {
super();
- this.init(url);
+ this.load(url);
}
public SchemaProcessorTransformer(final Source source, final long lastModified) {
super();
- this.init(source, lastModified);
+ this.load(source, lastModified);
}
- @Override
- public void setConfiguration(final Map<String, ? extends Object> configuration) {
- this.init((URL) configuration.get(SOURCE));
- }
+ private void load(final URL url) {
+ if (url == null) {
+ throw new IllegalArgumentException("Cannot load schema from null URL");
+ }
- @Override
- public String toString() {
- return StringRepresentation.buildString(this, "src=" + (this.url == null
- ? "<" + this.source + "," + this.lastModified + ">"
- : this.url.toExternalForm()));
+ this.url = url;
+ this.lastModified = URLConnectionUtils.getLastModified(this.url);
+
+ this.load(new StreamSource(this.url.toExternalForm()), this.url.toExternalForm());
}
- @Override
- protected void setSAXConsumer(final SAXConsumer xmlConsumer) {
- ValidatorHandler validatorHandler = this.schema.newValidatorHandler();
- validatorHandler.setErrorHandler(new SchemaErrorHandler(LOG,
- this.url == null ? this.source.toString() : this.url.toExternalForm()));
- validatorHandler.setContentHandler(xmlConsumer);
+ private void load(final Source source, final long lastModified) {
+ if (source == null) {
+ throw new IllegalArgumentException("Cannot load schema from null Source");
+ }
- SAXConsumerAdapter saxConsumerAdapter = new SAXConsumerAdapter();
- saxConsumerAdapter.setContentHandler(validatorHandler);
- super.setSAXConsumer(saxConsumerAdapter);
+ this.source = source;
+ this.lastModified = lastModified;
+
+ this.load(this.source, this.source.toString());
}
- private void checkLocalCache(final String key) {
- if (SCHEMA_CACHE.containsKey(key)) {
- ValidityValue<Schema> cacheEntry = SCHEMA_CACHE.get(key);
+ private void load(final Source source, final String localCacheKey) {
+ if (SCHEMA_CACHE.containsKey(localCacheKey)) {
+ final ValidityValue<Schema> cacheEntry = SCHEMA_CACHE.get(localCacheKey);
if (cacheEntry.getLastModified() >= this.lastModified) {
- LOG.debug("{} local cache hit: {}", getClass().getSimpleName(), key);
+ LOG.debug("{} local cache hit: {}", getClass().getSimpleName(), localCacheKey);
this.schema = cacheEntry.getValue();
}
}
- }
-
- private void putInLocalCache(final String key) {
- LOG.debug("{} local cache miss: {}", getClass().getSimpleName(), key);
-
- LOG.debug("{} local cache put: {}", getClass().getSimpleName(), key);
-
- ValidityValue<Schema> cacheEntry = new ValidityValue<Schema>(this.schema, this.lastModified);
- SCHEMA_CACHE.put(key, cacheEntry);
- }
-
- private void init(final URL url) {
- if (url == null) {
- throw new IllegalArgumentException("The parameter 'source' mustn't be null.");
- }
+ if (this.schema == null) {
+ LOG.debug("{} local cache miss: {}", getClass().getSimpleName(), localCacheKey);
- this.url = url;
- this.lastModified = URLConnectionUtils.getLastModified(this.url);
+ try {
+ this.schema = SCHEMA_FACTORY.newSchema(source);
- checkLocalCache(this.url.toExternalForm());
+ LOG.debug("{} local cache put: {}", getClass().getSimpleName(), localCacheKey);
- if (this.schema == null) {
- try {
- this.schema = SCHEMA_FACTORY.newSchema(this.url);
+ final ValidityValue<Schema> cacheEntry = new ValidityValue<Schema>(this.schema, this.lastModified);
+ SCHEMA_CACHE.put(localCacheKey, cacheEntry);
} catch (SAXException e) {
- throw new SetupException("Could not initialize xschema source", e);
+ throw new SetupException("Impossible to read Schema from '" + source + "', see nested exception", e);
}
-
- putInLocalCache(this.url.toExternalForm());
}
}
- private void init(final Source source, final long lastModified) {
- if (source == null) {
- throw new IllegalArgumentException("The parameter 'source' mustn't be null.");
+ @Override
+ public void setConfiguration(final Map<String, ? extends Object> configuration) {
+ try {
+ this.load((URL) configuration.get(SOURCE));
+ } catch (ClassCastException cce) {
+ throw new SetupException(
+ "The configuration value of '" + SOURCE + "' can't be cast to " + URL.class.getName(), cce);
}
+ }
- this.source = source;
- this.lastModified = lastModified;
-
- checkLocalCache(this.source.toString());
-
- if (this.schema == null) {
- try {
- this.schema = SCHEMA_FACTORY.newSchema(this.source);
- } catch (SAXException e) {
- throw new SetupException("Could not initialize xschema source", e);
- }
+ @Override
+ protected void setSAXConsumer(final SAXConsumer xmlConsumer) {
+ final ValidatorHandler validatorHandler = this.schema.newValidatorHandler();
+ validatorHandler.setErrorHandler(new SchemaErrorHandler(LOG,
+ this.url == null ? this.source.toString() : this.url.toExternalForm()));
+ validatorHandler.setContentHandler(xmlConsumer);
- putInLocalCache(this.source.toString());
- }
+ final SAXConsumerAdapter saxConsumerAdapter = new SAXConsumerAdapter();
+ saxConsumerAdapter.setContentHandler(validatorHandler);
+ super.setSAXConsumer(saxConsumerAdapter);
}
@Override
@@ -172,4 +156,11 @@ public final class SchemaProcessorTransf
? new TimestampSourceCacheKey(this.source, this.lastModified)
: new TimestampURLCacheKey(this.url, this.lastModified);
}
+
+ @Override
+ public String toString() {
+ return StringRepresentation.buildString(this, "src=" + (this.url == null
+ ? "<" + this.source + "," + this.lastModified + ">"
+ : this.url.toExternalForm()));
+ }
}