You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/12/10 18:37:18 UTC
svn commit: r485227 - in /cocoon/trunk:
blocks/cocoon-authentication-fw/cocoon-authentication-fw-impl/src/main/java/org/apache/cocoon/webapps/authentication/context/
blocks/cocoon-authentication-fw/cocoon-authentication-fw-impl/src/main/java/org/apache...
Author: cziegeler
Date: Sun Dec 10 09:37:13 2006
New Revision: 485227
URL: http://svn.apache.org/viewvc?view=rev&rev=485227
Log:
Clean up source util
Add experimental code for new pojo based parsers
Added:
cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/xml/XMLUtil.java (with props)
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/DOMParser.java (with props)
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/SAXParser.java (with props)
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java (with props)
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java (with props)
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java (with props)
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java (with props)
Removed:
cocoon/trunk/blocks/cocoon-authentication-fw/cocoon-authentication-fw-impl/src/main/java/org/apache/cocoon/webapps/authentication/flow/
Modified:
cocoon/trunk/blocks/cocoon-authentication-fw/cocoon-authentication-fw-impl/src/main/java/org/apache/cocoon/webapps/authentication/context/AuthenticationContext.java
cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/context/SimpleSessionContext.java
cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/SourceUtil.java
Modified: cocoon/trunk/blocks/cocoon-authentication-fw/cocoon-authentication-fw-impl/src/main/java/org/apache/cocoon/webapps/authentication/context/AuthenticationContext.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-authentication-fw/cocoon-authentication-fw-impl/src/main/java/org/apache/cocoon/webapps/authentication/context/AuthenticationContext.java?view=diff&rev=485227&r1=485226&r2=485227
==============================================================================
--- cocoon/trunk/blocks/cocoon-authentication-fw/cocoon-authentication-fw-impl/src/main/java/org/apache/cocoon/webapps/authentication/context/AuthenticationContext.java (original)
+++ cocoon/trunk/blocks/cocoon-authentication-fw/cocoon-authentication-fw-impl/src/main/java/org/apache/cocoon/webapps/authentication/context/AuthenticationContext.java Sun Dec 10 09:37:13 2006
@@ -31,9 +31,12 @@
import org.apache.cocoon.webapps.authentication.user.UserHandler;
import org.apache.cocoon.webapps.session.context.SessionContext;
import org.apache.cocoon.webapps.session.context.SimpleSessionContext;
+import org.apache.cocoon.webapps.session.xml.XMLUtil;
import org.apache.cocoon.xml.XMLUtils;
import org.apache.cocoon.xml.dom.DOMUtil;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
import org.apache.excalibur.source.SourceParameters;
import org.apache.excalibur.source.SourceResolver;
import org.apache.excalibur.xml.xpath.XPathProcessor;
@@ -71,6 +74,48 @@
/** A list of roles the user is in */
protected List roles;
+ /**
+ * Read a DOM Fragment from a source
+ *
+ * @param location URI of the Source
+ * @param typeParameters Type of Source query. Currently, only
+ * <code>method</code> parameter (value typically <code>GET</code> or
+ * <code>POST</code>) is recognized. May be <code>null</code>.
+ * @param parameters Parameters (e.g. URL params) of the source.
+ * May be <code>null</code>
+ * @param resolver Resolver for the source.
+ *
+ * @return DOM <code>DocumentFragment</code> constructed from the specified
+ * source.
+ *
+ * @throws ProcessingException
+ */
+ public static DocumentFragment readDOM(String location,
+ Parameters typeParameters,
+ SourceParameters parameters,
+ SourceResolver resolver)
+ throws ProcessingException {
+
+ Source source = null;
+ try {
+ source = SourceUtil.getSource(location, typeParameters, parameters, resolver);
+ Document doc = SourceUtil.toDOM(source);
+
+ DocumentFragment fragment = doc.createDocumentFragment();
+ fragment.appendChild(doc.getDocumentElement());
+
+ return fragment;
+ } catch (SourceException e) {
+ throw SourceUtil.handle(e);
+ } catch (IOException e) {
+ throw new ProcessingException(e);
+ } catch (SAXException e) {
+ throw new ProcessingException(e);
+ } finally {
+ resolver.release(source);
+ }
+ }
+
/** Constructor */
public AuthenticationContext(Context context, XPathProcessor processor, SourceResolver resolver) {
this.context = context;
@@ -545,7 +590,7 @@
parameters = this.createParameters(parameters,
path,
false);
- SourceUtil.writeDOM(authSaveResource,
+ XMLUtil.writeDOM(authSaveResource,
null,
parameters,
fragment,
@@ -581,10 +626,7 @@
false);
DocumentFragment frag;
- frag = SourceUtil.readDOM(authLoadResource,
- null,
- parameters,
- resolver);
+ frag = readDOM(authLoadResource, null, parameters, resolver);
this.setXML(path, frag);
@@ -619,10 +661,7 @@
path,
true);
DocumentFragment fragment;
- fragment = SourceUtil.readDOM(loadResource,
- null,
- parameters,
- resolver);
+ fragment = readDOM(loadResource, null, parameters, resolver);
this.authContext.setXML("/applications/" + applicationName + '/', fragment);
} // end synchronized
@@ -662,7 +701,7 @@
fragment = DOMUtil.createDocument().createDocumentFragment();
}
- SourceUtil.writeDOM(saveResource,
+ XMLUtil.writeDOM(saveResource,
null,
parameters,
fragment,
@@ -799,10 +838,7 @@
null,
true);
DocumentFragment fragment;
- fragment = SourceUtil.readDOM(loadResource,
- null,
- parameters,
- resolver);
+ fragment = readDOM(loadResource, null, parameters, resolver);
this.authContext.setXML("/applications/" + appConf.getName() + '/', fragment);
} // end synchronized
Modified: cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/context/SimpleSessionContext.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/context/SimpleSessionContext.java?view=diff&rev=485227&r1=485226&r2=485227
==============================================================================
--- cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/context/SimpleSessionContext.java (original)
+++ cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/context/SimpleSessionContext.java Sun Dec 10 09:37:13 2006
@@ -22,6 +22,7 @@
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.components.source.SourceUtil;
+import org.apache.cocoon.webapps.session.xml.XMLUtil;
import org.apache.cocoon.xml.IncludeXMLConsumer;
import org.apache.cocoon.xml.dom.DOMUtil;
import org.apache.excalibur.source.Source;
@@ -443,7 +444,7 @@
frag = DOMUtil.createDocument().createDocumentFragment();
}
- SourceUtil.writeDOM(this.saveResource,
+ XMLUtil.writeDOM(this.saveResource,
null,
parameters,
frag,
Added: cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/xml/XMLUtil.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/xml/XMLUtil.java?view=auto&rev=485227
==============================================================================
--- cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/xml/XMLUtil.java (added)
+++ cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/xml/XMLUtil.java Sun Dec 10 09:37:13 2006
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.webapps.session.xml;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Properties;
+
+import javax.xml.transform.OutputKeys;
+
+import org.apache.avalon.framework.parameters.Parameters;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.components.source.SourceUtil;
+import org.apache.cocoon.environment.internal.EnvironmentHelper;
+import org.apache.cocoon.serialization.Serializer;
+import org.apache.cocoon.xml.XMLUtils;
+import org.apache.cocoon.xml.dom.DOMStreamer;
+import org.apache.excalibur.source.ModifiableSource;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceParameters;
+import org.apache.excalibur.source.SourceResolver;
+import org.w3c.dom.DocumentFragment;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+
+
+/**
+ * A utility class which will soon be removed...
+ *
+ * @deprecated This block is deprecated and will be removed in future versions.
+ * @version $Id$
+*/
+public final class XMLUtil {
+
+ /**
+ * Write a DOM Fragment to a source.
+ * If the source is a ModifiableSource the interface is used.
+ * If not, the source is invoked with an additional parameter named
+ * "content" containing the XML.
+ *
+ * @param location URI of the Source
+ * @param typeParameters Type of Source query. Currently, only
+ * <code>method</code> parameter (value typically <code>GET</code> or
+ * <code>POST</code>) is recognized. May be <code>null</code>.
+ * @param parameters Parameters (e.g. URL params) of the source.
+ * May be <code>null</code>
+ * @param frag DOM fragment to serialize to the Source
+ * @param resolver Resolver for the source.
+ * @param serializerName The serializer to use
+ * @throws ProcessingException
+ */
+ public static void writeDOM(String location,
+ Parameters typeParameters,
+ SourceParameters parameters,
+ DocumentFragment frag,
+ SourceResolver resolver,
+ String serializerName)
+ throws ProcessingException {
+ Source source = null;
+
+ try {
+ source = SourceUtil.getSource(location, typeParameters,
+ parameters, resolver);
+ if (source instanceof ModifiableSource) {
+ ModifiableSource ws = (ModifiableSource) source;
+
+ frag.normalize();
+
+ if (null != serializerName) {
+ ServiceManager manager = EnvironmentHelper.getSitemapServiceManager();
+
+ Serializer serializer = null;
+ OutputStream oStream = null;
+ try {
+ serializer = (Serializer)manager.lookup(Serializer.ROLE + '/' + serializerName);
+ oStream = ws.getOutputStream();
+ serializer.setOutputStream(oStream);
+ serializer.startDocument();
+ DOMStreamer streamer = new DOMStreamer(serializer);
+ streamer.stream(frag);
+ serializer.endDocument();
+ } catch (ServiceException e) {
+ throw new ProcessingException("Unable to lookup serializer.", e);
+ } finally {
+ if (oStream != null) {
+ oStream.flush();
+ try {
+ oStream.close();
+ } catch (Exception ignore) {
+ }
+ }
+ manager.release(serializer);
+ }
+ } else {
+ Properties props = XMLUtils.createPropertiesForXML(false);
+ props.put(OutputKeys.ENCODING, "ISO-8859-1");
+ final String content = XMLUtils.serializeNode(frag, props);
+ OutputStream oStream = ws.getOutputStream();
+
+ oStream.write(content.getBytes());
+ oStream.flush();
+ oStream.close();
+ }
+ } else {
+ String content;
+ if (null != serializerName) {
+ ServiceManager manager = EnvironmentHelper.getSitemapServiceManager();
+
+ Serializer serializer = null;
+ ByteArrayOutputStream oStream = new ByteArrayOutputStream();
+ try {
+ serializer = (Serializer)manager.lookup(Serializer.ROLE + '/' + serializerName);
+ serializer.setOutputStream(oStream);
+ serializer.startDocument();
+ DOMStreamer streamer = new DOMStreamer(serializer);
+ streamer.stream(frag);
+ serializer.endDocument();
+ } catch (ServiceException e) {
+ throw new ProcessingException("Unable to lookup serializer.", e);
+ } finally {
+ oStream.flush();
+ try {
+ oStream.close();
+ } catch (Exception ignore) {
+ // do nothing
+ }
+ manager.release(serializer);
+ }
+ content = oStream.toString();
+ } else {
+ Properties props = XMLUtils.createPropertiesForXML(false);
+ props.put(OutputKeys.ENCODING, "ISO-8859-1");
+ content = XMLUtils.serializeNode(frag, props);
+ }
+
+ if (parameters == null) {
+ parameters = new SourceParameters();
+ } else {
+ parameters = (SourceParameters) parameters.clone();
+ }
+ parameters.setSingleParameterValue("content", content);
+
+ source = SourceUtil.getSource(location, typeParameters,
+ parameters, resolver);
+ SourceUtil.toSAX(source, new DefaultHandler());
+ }
+ } catch (SourceException e) {
+ throw SourceUtil.handle(e);
+ } catch (IOException e) {
+ throw new ProcessingException(e);
+ } catch (SAXException e) {
+ throw new ProcessingException(e);
+ } finally {
+ resolver.release(source);
+ }
+ }
+}
Propchange: cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/xml/XMLUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/blocks/cocoon-session-fw/cocoon-session-fw-impl/src/main/java/org/apache/cocoon/webapps/session/xml/XMLUtil.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/SourceUtil.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/SourceUtil.java?view=diff&rev=485227&r1=485226&r2=485227
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/SourceUtil.java (original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/source/SourceUtil.java Sun Dec 10 09:37:13 2006
@@ -16,22 +16,22 @@
*/
package org.apache.cocoon.components.source;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.ServiceSelector;
-
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.ResourceNotFoundException;
import org.apache.cocoon.environment.internal.EnvironmentHelper;
-import org.apache.cocoon.serialization.Serializer;
import org.apache.cocoon.util.NetUtils;
import org.apache.cocoon.xml.IncludeXMLConsumer;
-import org.apache.cocoon.xml.XMLUtils;
import org.apache.cocoon.xml.dom.DOMBuilder;
-import org.apache.cocoon.xml.dom.DOMStreamer;
-
-import org.apache.excalibur.source.ModifiableSource;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceException;
import org.apache.excalibur.source.SourceNotFoundException;
@@ -45,22 +45,9 @@
import org.apache.regexp.REProgram;
import org.apache.regexp.RESyntaxException;
import org.w3c.dom.Document;
-import org.w3c.dom.DocumentFragment;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
-import org.xml.sax.helpers.DefaultHandler;
-
-import javax.xml.transform.OutputKeys;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.Reader;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Properties;
/**
* This class contains some utility methods for the source resolving.
@@ -542,183 +529,6 @@
resourceParameters);
return resolver.resolveURI(uri, null, resolverParameters);
- }
-
- /**
- * Write a DOM Fragment to a source.
- * If the source is a ModifiableSource the interface is used.
- * If not, the source is invoked with an additional parameter named
- * "content" containing the XML.
- * The current sitemap service manager is used to lookup the serializer.
- *
- * @param location URI of the Source
- * @param typeParameters Type of Source query. Currently, only
- * <code>method</code> parameter (value typically <code>GET</code> or
- * <code>POST</code>) is recognized. May be <code>null</code>.
- * @param parameters Parameters (e.g. URL params) of the source.
- * May be <code>null</code>
- * @param frag DOM fragment to serialize to the Source
- * @param resolver Resolver for the source.
- * @param serializerName The serializer to use
- *
- * @throws ProcessingException
- */
- public static void writeDOM(String location,
- Parameters typeParameters,
- SourceParameters parameters,
- DocumentFragment frag,
- SourceResolver resolver,
- String serializerName)
- throws ProcessingException {
- Source source = null;
-
- try {
- source = SourceUtil.getSource(location, typeParameters,
- parameters, resolver);
- if (source instanceof ModifiableSource) {
- ModifiableSource ws = (ModifiableSource) source;
-
- frag.normalize();
-
- if (null != serializerName) {
- ServiceManager manager = getSitemapServiceManager();
-
- ServiceSelector selector = null;
- Serializer serializer = null;
- OutputStream oStream = null;
- try {
- selector = (ServiceSelector)manager.lookup(Serializer.ROLE + "Selector");
- serializer = (Serializer)selector.select(serializerName);
- oStream = ws.getOutputStream();
- serializer.setOutputStream(oStream);
- serializer.startDocument();
- DOMStreamer streamer = new DOMStreamer(serializer);
- streamer.stream(frag);
- serializer.endDocument();
- } catch (ServiceException e) {
- throw new ProcessingException("Unable to lookup serializer.", e);
- } finally {
- if (oStream != null) {
- oStream.flush();
- try {
- oStream.close();
- } catch (Exception ignore) {
- }
- }
- if (selector != null) {
- selector.release(serializer);
- manager.release(selector);
- }
- }
- } else {
- Properties props = XMLUtils.createPropertiesForXML(false);
- props.put(OutputKeys.ENCODING, "ISO-8859-1");
- final String content = XMLUtils.serializeNode(frag, props);
- OutputStream oStream = ws.getOutputStream();
-
- oStream.write(content.getBytes());
- oStream.flush();
- oStream.close();
- }
- } else {
- String content;
- if (null != serializerName) {
- ServiceManager manager = getSitemapServiceManager();
-
- ServiceSelector selector = null;
- Serializer serializer = null;
- ByteArrayOutputStream oStream = new ByteArrayOutputStream();
- try {
- selector = (ServiceSelector)manager.lookup(Serializer.ROLE + "Selector");
- serializer = (Serializer)selector.select(serializerName);
- serializer.setOutputStream(oStream);
- serializer.startDocument();
- DOMStreamer streamer = new DOMStreamer(serializer);
- streamer.stream(frag);
- serializer.endDocument();
- } catch (ServiceException e) {
- throw new ProcessingException("Unable to lookup serializer.", e);
- } finally {
- oStream.flush();
- try {
- oStream.close();
- } catch (Exception ignore) {
- // do nothing
- }
- if (selector != null) {
- selector.release(serializer);
- manager.release(selector);
- }
- }
- content = oStream.toString();
- } else {
- Properties props = XMLUtils.createPropertiesForXML(false);
- props.put(OutputKeys.ENCODING, "ISO-8859-1");
- content = XMLUtils.serializeNode(frag, props);
- }
-
- if (parameters == null) {
- parameters = new SourceParameters();
- } else {
- parameters = (SourceParameters) parameters.clone();
- }
- parameters.setSingleParameterValue("content", content);
-
- source = SourceUtil.getSource(location, typeParameters,
- parameters, resolver);
- SourceUtil.toSAX(source, new DefaultHandler());
- }
- } catch (SourceException e) {
- throw SourceUtil.handle(e);
- } catch (IOException e) {
- throw new ProcessingException(e);
- } catch (SAXException e) {
- throw new ProcessingException(e);
- } finally {
- resolver.release(source);
- }
- }
-
- /**
- * Read a DOM Fragment from a source
- *
- * @param location URI of the Source
- * @param typeParameters Type of Source query. Currently, only
- * <code>method</code> parameter (value typically <code>GET</code> or
- * <code>POST</code>) is recognized. May be <code>null</code>.
- * @param parameters Parameters (e.g. URL params) of the source.
- * May be <code>null</code>
- * @param resolver Resolver for the source.
- *
- * @return DOM <code>DocumentFragment</code> constructed from the specified
- * source.
- *
- * @throws ProcessingException
- */
- public static DocumentFragment readDOM(String location,
- Parameters typeParameters,
- SourceParameters parameters,
- SourceResolver resolver)
- throws ProcessingException {
-
- Source source = null;
- try {
- source = SourceUtil.getSource(location, typeParameters, parameters, resolver);
- Document doc = SourceUtil.toDOM(source);
-
- DocumentFragment fragment = doc.createDocumentFragment();
- fragment.appendChild(doc.getDocumentElement());
-
- return fragment;
- } catch (SourceException e) {
- throw SourceUtil.handle(e);
- } catch (IOException e) {
- throw new ProcessingException(e);
- } catch (SAXException e) {
- throw new ProcessingException(e);
- } finally {
- resolver.release(source);
- }
}
/**
Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/DOMParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/DOMParser.java?view=auto&rev=485227
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/DOMParser.java (added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/DOMParser.java Sun Dec 10 09:37:13 2006
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.core.xml;
+
+import java.io.IOException;
+
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * The parser can be used to parse any XML document given
+ * by a {@link InputSource} object.
+ * It can create a DOM from the parsed document.
+ *
+ * @version $Id$
+ * @since 2.2
+ */
+public interface DOMParser {
+
+ /**
+ * Parse the {@link InputSource} and create
+ * a DOM out of it.
+ */
+ Document parseDocument( InputSource in )
+ throws SAXException, IOException;
+
+ /**
+ * Return a new {@link Document}.
+ */
+ Document createDocument() throws SAXException;
+}
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/DOMParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/DOMParser.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/SAXParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/SAXParser.java?view=auto&rev=485227
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/SAXParser.java (added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/SAXParser.java Sun Dec 10 09:37:13 2006
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.core.xml;
+
+import java.io.IOException;
+
+import org.xml.sax.ContentHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.ext.LexicalHandler;
+
+/**
+ * The parser can be used to parse any XML document given
+ * by a {@link InputSource} object.
+ * It can send XML events from the parsed document.
+ *
+ * @version $Id$
+ * @since 2.2
+ */
+public interface SAXParser {
+
+ /**
+ * Parse the {@link InputSource} and send
+ * SAX events to the consumer.
+ * Attention: the consumer can implement the
+ * {@link LexicalHandler} as well.
+ * The parse should take care of this.
+ */
+ void parse( InputSource in, ContentHandler consumer )
+ throws SAXException, IOException;
+
+ /**
+ * Parse the {@link InputSource} and send
+ * SAX events to the content handler and
+ * the lexical handler.
+ */
+ void parse( InputSource in,
+ ContentHandler contentHandler,
+ LexicalHandler lexicalHandler )
+ throws SAXException, IOException;
+}
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/SAXParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/SAXParser.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java?view=auto&rev=485227
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java (added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java Sun Dec 10 09:37:13 2006
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.core.xml.impl;
+
+import org.apache.cocoon.util.AbstractLogEnabled;
+import org.xml.sax.EntityResolver;
+
+/**
+ * An abstract base class for implementing Jaxp based parsers.
+ *
+ * @see JaxpDOMParser
+ * @see JaxpSAXParser
+ *
+ * @version $Id$
+ * @since 2.2
+ */
+public abstract class AbstractJaxpParser
+ extends AbstractLogEnabled {
+
+ /** the Entity Resolver */
+ protected EntityResolver resolver;
+
+ /** do we want to reuse parsers ? */
+ protected boolean reuseParsers = true;
+
+ /** Do we want to validate? */
+ protected boolean validate = false;
+
+ public void setEntityResolver(EntityResolver r) {
+ this.resolver = r;
+ }
+
+ public EntityResolver getEntityResolver() {
+ return this.resolver;
+ }
+
+ /**
+ * @see #setReuseParsers(boolean)
+ */
+ public boolean isReuseParsers() {
+ return reuseParsers;
+ }
+
+ /**
+ * Do we want to reuse parsers or create a new parser for each parse ?
+ * (Default is true)
+ * <i>Note</i> : even if this parameter is <code>true</code>, parsers are not
+ * recycled in case of parsing errors : some parsers (e.g. Xerces) don't like
+ * to be reused after failure.
+ */
+ public void setReuseParsers(boolean reuseParsers) {
+ this.reuseParsers = reuseParsers;
+ }
+
+ /**
+ * @see #setValidate(boolean)
+ */
+ public boolean isValidate() {
+ return validate;
+ }
+
+ /**
+ * should the parser validate parsed documents ?
+ * Default is false.
+ */
+ public void setValidate(boolean validate) {
+ this.validate = validate;
+ }
+
+ /**
+ * Load a class
+ */
+ protected Class loadClass( String name ) throws Exception {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if( loader == null ) {
+ loader = getClass().getClassLoader();
+ }
+ return loader.loadClass( name );
+ }
+}
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/AbstractJaxpParser.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java?view=auto&rev=485227
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java (added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java Sun Dec 10 09:37:13 2006
@@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.core.xml.impl;
+
+import java.io.IOException;
+
+import org.apache.cocoon.core.container.spring.ResourceUtils;
+import org.apache.cocoon.util.AbstractLogEnabled;
+import org.apache.xml.resolver.CatalogManager;
+import org.apache.xml.resolver.tools.CatalogResolver;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+
+/**
+ * A component that uses catalogs for resolving entities.
+ * This implementation uses the XML Entity and URI Resolvers from
+ * http://xml.apache.org/commons/
+ * published by Norman Walsh. More information on the catalogs can be
+ * found at
+ * http://xml.apache.org/cocoon/userdocs/concepts/catalog.html
+ *
+ * @version $Id$
+ * @since 2.2
+ */
+public class DefaultEntityResolver
+ extends AbstractLogEnabled
+ implements EntityResolver {
+
+ /** The catalog manager */
+ protected CatalogManager catalogManager = new CatalogManager();
+
+ /** The catalog resolver */
+ protected CatalogResolver catalogResolver = new CatalogResolver(catalogManager);
+
+ /** Verbosity level. */
+ protected Integer verbosity;
+
+ protected String catalog = "WEB-INF/cocoon/entities/catalog";
+
+ protected String localCatalog;
+
+ protected ResourceLoader resourceLoader;
+
+ public Integer getVerbosity() {
+ return verbosity;
+ }
+
+ public void setVerbosity(Integer verbosity) {
+ this.verbosity = verbosity;
+ }
+
+ public String getCatalog() {
+ return catalog;
+ }
+
+ public void setCatalog(String catalog) {
+ this.catalog = catalog;
+ }
+
+ public String getLocalCatalog() {
+ return localCatalog;
+ }
+
+ public void setLocalCatalog(String localCatalog) {
+ this.localCatalog = localCatalog;
+ }
+
+ public void setResourceLoader(ResourceLoader loader) {
+ this.resourceLoader = loader;
+ }
+
+ public ResourceLoader getResourceLoader() {
+ return resourceLoader;
+ }
+
+ /**
+ * Set the configuration. Load the system catalog and apply any
+ * parameters that may have been specified in cocoon.xconf
+ * @param params The configuration information
+ * @exception ParameterException
+ */
+ protected void init() {
+ // Over-ride debug level that is set by CatalogManager.properties
+ if ( this.verbosity != null ) {
+ if (this.getLogger().isDebugEnabled()) {
+ this.getLogger().debug("Setting Catalog resolver verbosity level to " + this.verbosity);
+ }
+ this.catalogManager.setVerbosity(this.verbosity.intValue());
+ }
+
+ // Load the built-in catalog
+ if ( this.catalog == null ) {
+ this.getLogger().warn("No default catalog defined.");
+ } else {
+ this.parseCatalog(this.catalog);
+ }
+
+ // Load a single additional local catalog
+ if ( this.localCatalog != null ) {
+ this.parseCatalog( this.localCatalog );
+ }
+ }
+
+ /**
+ * Parse a catalog
+ */
+ protected void parseCatalog(String uri) {
+ if (this.getLogger().isDebugEnabled()) {
+ this.getLogger().debug("Additional Catalog is " + uri);
+ }
+
+ final Resource resource = this.resourceLoader.getResource(uri);
+ try {
+ this.catalogResolver.getCatalog().parseCatalog(ResourceUtils.getUri(resource));
+ } catch (Exception e) {
+ this.getLogger().warn("Could not get Catalog file. Trying again: " + uri, e);
+
+ // try it again
+ try {
+ this.catalogResolver.getCatalog().parseCatalog("text/plain", resource.getInputStream());
+ } catch (Exception ex) {
+ this.getLogger().warn("Could not get Catalog file: " + uri, ex);
+ }
+ }
+ }
+
+ /**
+ * Allow the application to resolve external entities.
+ *
+ * <p>The Parser will call this method before opening any external
+ * entity except the top-level document entity (including the
+ * external DTD subset, external entities referenced within the
+ * DTD, and external entities referenced within the document
+ * element): the application may request that the parser resolve
+ * the entity itself, that it use an alternative URI, or that it
+ * use an entirely different input source.</p>
+ *
+ * <p>Application writers can use this method to redirect external
+ * system identifiers to secure and/or local URIs, to look up
+ * public identifiers in a catalogue, or to read an entity from a
+ * database or other input source (including, for example, a dialog
+ * box).</p>
+ *
+ * <p>If the system identifier is a URL, the SAX parser must
+ * resolve it fully before reporting it to the application.</p>
+ *
+ * @param publicId The public identifier of the external entity
+ * being referenced, or null if none was supplied.
+ * @param systemId The system identifier of the external entity
+ * being referenced.
+ * @return An InputSource object describing the new input source,
+ * or null to request that the parser open a regular
+ * URI connection to the system identifier.
+ * @exception org.xml.sax.SAXException Any SAX exception, possibly
+ * wrapping another exception.
+ * @exception java.io.IOException A Java-specific IO exception,
+ * possibly the result of creating a new InputStream
+ * or Reader for the InputSource.
+ * @see org.xml.sax.InputSource
+ */
+ public InputSource resolveEntity(String publicId, String systemId)
+ throws SAXException, IOException {
+ InputSource altInputSource = this.catalogResolver.resolveEntity(publicId, systemId);
+ if (altInputSource != null) {
+ if (this.getLogger().isDebugEnabled()) {
+ this.getLogger().debug("Resolved catalog entity: "
+ + publicId + " " + altInputSource.getSystemId());
+ }
+ }
+
+ return altInputSource;
+ }
+
+}
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/DefaultEntityResolver.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java?view=auto&rev=485227
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java (added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java Sun Dec 10 09:37:13 2006
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.core.xml.impl;
+
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.cocoon.core.xml.DOMParser;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * A dom parser which uses a JAXP 1.1 compliant parsers.
+ *
+ * @see AbstractJaxpParser
+ * @version $Id$
+ * @since 2.2
+ */
+public final class JaxpDOMParser
+ extends AbstractJaxpParser
+ implements DOMParser {
+
+ /** the Document Builder factory */
+ protected DocumentBuilderFactory factory;
+
+ /** The DOM builder. It is created lazily by {@link #setupDocumentBuilder()}
+ and cleared if a parsing error occurs. */
+ protected DocumentBuilder docBuilder;
+
+ protected String documentBuilderFactoryName = "javax.xml.parsers.DocumentBuilderFactory";
+
+ public String getDocumentBuilderFactoryName() {
+ return documentBuilderFactoryName;
+ }
+
+ public void setDocumentBuilderFactoryName(String documentBuilderFactoryName) {
+ this.documentBuilderFactoryName = documentBuilderFactoryName;
+ }
+
+ /**
+ * Initialize the dom builder factory.
+ */
+ protected void initDomBuilderFactory()
+ throws Exception {
+ if ( this.factory == null ) {
+ if( "javax.xml.parsers.DocumentBuilderFactory".equals( this.documentBuilderFactoryName ) ) {
+ this.factory = DocumentBuilderFactory.newInstance();
+ } else {
+ final Class factoryClass = loadClass( this.documentBuilderFactoryName );
+ this.factory = (DocumentBuilderFactory)factoryClass.newInstance();
+ }
+ this.factory.setNamespaceAware( true );
+ this.factory.setValidating( this.validate );
+ }
+ }
+
+ /**
+ * Parses a new Document object from the given InputSource.
+ */
+ public Document parseDocument( final InputSource input )
+ throws SAXException, IOException {
+ this.setupDocumentBuilder();
+
+ // Ensure we will use a fresh new parser at next parse in case of failure
+ DocumentBuilder tmpBuilder = this.docBuilder;
+ this.docBuilder = null;
+
+ if( this.resolver != null ) {
+ tmpBuilder.setEntityResolver( this.resolver );
+ }
+
+ final Document result = tmpBuilder.parse( input );
+
+ // Here, parsing was successful : restore builder
+ if( this.reuseParsers ) {
+ this.docBuilder = tmpBuilder;
+ }
+
+ return result;
+ }
+
+ /**
+ * Creates a new {@link DocumentBuilder} if needed.
+ */
+ protected void setupDocumentBuilder()
+ throws SAXException {
+ try {
+ this.initDomBuilderFactory();
+ } catch (Exception e) {
+ final String message = "Cannot initialize dom builder factory";
+ throw new SAXException( message, e );
+ }
+ if( this.docBuilder == null ) {
+ try {
+ this.docBuilder = this.factory.newDocumentBuilder();
+ } catch( final ParserConfigurationException pce ) {
+ final String message = "Could not create DocumentBuilder";
+ throw new SAXException( message, pce );
+ }
+ }
+ }
+
+ /**
+ * Return a new {@link Document}.
+ */
+ public Document createDocument()
+ throws SAXException {
+ setupDocumentBuilder();
+ return this.docBuilder.newDocument();
+ }
+}
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpDOMParser.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java?view=auto&rev=485227
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java (added)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java Sun Dec 10 09:37:13 2006
@@ -0,0 +1,363 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.core.xml.impl;
+
+import java.io.IOException;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.cocoon.core.xml.SAXParser;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.ext.LexicalHandler;
+
+/**
+ * A SAX parser which uses a JAXP 1.1 compliant parsers.
+ *
+ * @see AbstractJaxpParser
+ * @version $Id$
+ * @since 2.2
+ */
+public final class JaxpSAXParser
+ extends AbstractJaxpParser
+ implements SAXParser, ErrorHandler {
+
+ /** the SAX Parser factory */
+ protected SAXParserFactory factory;
+
+ /** The SAX reader. It is created lazily by {@link #setupXMLReader()}
+ and cleared if a parsing error occurs. */
+ protected XMLReader reader;
+
+ /** do we want namespaces also as attributes ? */
+ protected boolean nsPrefixes = false;
+
+ /** do we stop on warnings ? */
+ protected boolean stopOnWarning = true;
+
+ /** do we stop on recoverable errors ? */
+ protected boolean stopOnRecoverableError = true;
+
+ /** Should comments appearing between start/endDTD events be dropped ? */
+ protected boolean dropDtdComments = false;
+
+ /** The name of the sax parser factory. */
+ protected String saxParserFactoryName = "javax.xml.parsers.SAXParserFactory";
+
+ /**
+ * @see #setDropDtdComments(boolean)
+ */
+ public boolean isDropDtdComments() {
+ return dropDtdComments;
+ }
+
+ /**
+ * Should comment() events from DTD's be dropped? (Default is false.) Since this implementation
+ * does not support the DeclHandler interface anyway, it is quite useless to only have the comments
+ * from DTD. And the comment events from the internal DTD subset would appear in the serialized output
+ * again.
+ */
+ public void setDropDtdComments(boolean dropDtdComments) {
+ this.dropDtdComments = dropDtdComments;
+ }
+
+ /**
+ * @see #setNsPrefixes(boolean)
+ */
+ public boolean isNsPrefixes() {
+ return nsPrefixes;
+ }
+
+ /**
+ * Do we want namespaces declarations also as 'xmlns:' attributes ?
+ * Default is false.
+ * <i>Note</i> : setting this to <code>true</code> confuses some XSL
+ * processors (e.g. Saxon).
+ */
+ public void setNsPrefixes(boolean nsPrefixes) {
+ this.nsPrefixes = nsPrefixes;
+ }
+
+ /**
+ * @see #setStopOnRecoverableError(boolean)
+ */
+ public boolean isStopOnRecoverableError() {
+ return stopOnRecoverableError;
+ }
+
+ /**
+ * Should the parser stop parsing if a recoverable error occurs ?
+ * Default is true.
+ */
+ public void setStopOnRecoverableError(boolean stopOnRecoverableError) {
+ this.stopOnRecoverableError = stopOnRecoverableError;
+ }
+
+ /**
+ * @see #setStopOnWarning(boolean)
+ */
+ public boolean isStopOnWarning() {
+ return stopOnWarning;
+ }
+
+ /**
+ * Should the parser stop parsing if a warning occurs ?
+ * Default is true.
+ */
+ public void setStopOnWarning(boolean stopOnWarning) {
+ this.stopOnWarning = stopOnWarning;
+ }
+
+ /**
+ * Return the name of the sax parser factory.
+ * @return the name of the sax parser factory.
+ * @see #setSaxParserFactoryName(String)
+ */
+ public String getSaxParserFactoryName() {
+ return this.saxParserFactoryName;
+ }
+
+ /**
+ * Set the name of the <code>SAXParserFactory</code>
+ * implementation class to be used instead of using the standard JAXP mechanism
+ * (<code>SAXParserFactory.newInstance()</code>). This allows to choose
+ * unambiguously the JAXP implementation to be used when several of them are
+ * available in the classpath.
+ */
+ public void setSaxParserFactoryName(String saxParserFactoryName) {
+ this.saxParserFactoryName = saxParserFactoryName;
+ }
+
+ /**
+ * Initialize the sax parser factory.
+ */
+ protected void initSaxParserFactory()
+ throws Exception {
+ if ( this.factory == null ) {
+ if( "javax.xml.parsers.SAXParserFactory".equals( this.saxParserFactoryName ) ) {
+ this.factory = SAXParserFactory.newInstance();
+ } else {
+ final Class factoryClass = loadClass( this.saxParserFactoryName );
+ this.factory = (SAXParserFactory)factoryClass.newInstance();
+ }
+ this.factory.setNamespaceAware( true );
+ this.factory.setValidating( this.validate );
+ }
+ }
+
+ /**
+ * Parse the <code>InputSource</code> and send
+ * SAX events to the consumer.
+ * Attention: the consumer can implement the
+ * <code>LexicalHandler</code> as well.
+ * The parse should take care of this.
+ */
+ public void parse( final InputSource in,
+ final ContentHandler contentHandler,
+ final LexicalHandler lexicalHandler )
+ throws SAXException, IOException {
+ this.setupXMLReader();
+
+ // Ensure we will use a fresh new parser at next parse in case of failure
+ XMLReader tmpReader = this.reader;
+ this.reader = null;
+
+ try {
+ LexicalHandler theLexicalHandler = null;
+ if ( null == lexicalHandler
+ && contentHandler instanceof LexicalHandler) {
+ theLexicalHandler = (LexicalHandler)contentHandler;
+ }
+ if( null != lexicalHandler ) {
+ theLexicalHandler = lexicalHandler;
+ }
+ if (theLexicalHandler != null) {
+ if (this.dropDtdComments) {
+ theLexicalHandler = new DtdCommentEater(theLexicalHandler);
+ }
+ tmpReader.setProperty( "http://xml.org/sax/properties/lexical-handler",
+ theLexicalHandler );
+ }
+ } catch( final SAXException e ) {
+ final String message =
+ "SAX2 driver does not support property: " +
+ "'http://xml.org/sax/properties/lexical-handler'";
+ this.getLogger().warn( message );
+ }
+
+ tmpReader.setErrorHandler( this );
+ tmpReader.setContentHandler( contentHandler );
+ if( this.resolver != null ) {
+ tmpReader.setEntityResolver( this.resolver );
+ }
+
+ tmpReader.parse( in );
+
+ // Here, parsing was successful : restore reader
+ if ( this.reuseParsers ) {
+ this.reader = tmpReader;
+ }
+ }
+
+ /**
+ * @see org.apache.cocoon.core.xml.SAXParser#parse(org.xml.sax.InputSource, org.xml.sax.ContentHandler)
+ */
+ public void parse( InputSource in, ContentHandler consumer )
+ throws SAXException, IOException {
+ this.parse( in, consumer,
+ (consumer instanceof LexicalHandler ? (LexicalHandler)consumer : null));
+ }
+
+ /**
+ * Creates a new {@link XMLReader} if needed.
+ */
+ protected void setupXMLReader()
+ throws SAXException {
+ try {
+ this.initSaxParserFactory();
+ } catch (Exception e) {
+ final String message = "Cannot initialize sax parser factory";
+ throw new SAXException( message, e );
+ }
+ if( this.reader == null ) {
+ // Create the XMLReader
+ try {
+ this.reader = this.factory.newSAXParser().getXMLReader();
+ } catch( final ParserConfigurationException pce ) {
+ final String message = "Cannot produce a valid parser";
+ throw new SAXException( message, pce );
+ }
+
+ this.reader.setFeature( "http://xml.org/sax/features/namespaces", true );
+
+ if( this.nsPrefixes ) {
+ try {
+ this.reader.setFeature( "http://xml.org/sax/features/namespace-prefixes",
+ this.nsPrefixes );
+ } catch( final SAXException se ) {
+ final String message =
+ "SAX2 XMLReader does not support setting feature: " +
+ "'http://xml.org/sax/features/namespace-prefixes'";
+ this.getLogger().warn( message );
+ }
+ }
+ }
+ }
+
+ /**
+ * Receive notification of a recoverable error.
+ */
+ public void error( final SAXParseException spe )
+ throws SAXException {
+ final String message =
+ "Error parsing " + spe.getSystemId() + " (line " +
+ spe.getLineNumber() + " col. " + spe.getColumnNumber() +
+ "): " + spe.getMessage();
+ if( this.stopOnRecoverableError ) {
+ throw new SAXException( message, spe );
+ }
+ this.getLogger().error( message, spe );
+ }
+
+ /**
+ * Receive notification of a fatal error.
+ */
+ public void fatalError( final SAXParseException spe )
+ throws SAXException {
+ final String message =
+ "Fatal error parsing " + spe.getSystemId() + " (line " +
+ spe.getLineNumber() + " col. " + spe.getColumnNumber() +
+ "): " + spe.getMessage();
+ throw new SAXException( message, spe );
+ }
+
+ /**
+ * Receive notification of a warning.
+ */
+ public void warning( final SAXParseException spe )
+ throws SAXException {
+ final String message =
+ "Warning parsing " + spe.getSystemId() + " (line " +
+ spe.getLineNumber() + " col. " + spe.getColumnNumber() +
+ "): " + spe.getMessage();
+
+ if( this.stopOnWarning ) {
+ throw new SAXException( message, spe );
+ }
+ this.getLogger().warn( message, spe );
+ }
+
+ /**
+ * A LexicalHandler implementation that strips all comment events between
+ * startDTD and endDTD. In all other cases the events are forwarded to another
+ * LexicalHandler.
+ */
+ protected static class DtdCommentEater implements LexicalHandler {
+
+ protected LexicalHandler next;
+ protected boolean inDTD;
+
+ public DtdCommentEater(LexicalHandler nextHandler) {
+ this.next = nextHandler;
+ }
+
+ public void startDTD (String name, String publicId, String systemId)
+ throws SAXException {
+ inDTD = true;
+ next.startDTD(name, publicId, systemId);
+ }
+
+ public void endDTD ()
+ throws SAXException {
+ inDTD = false;
+ next.endDTD();
+ }
+
+ public void startEntity (String name)
+ throws SAXException {
+ next.startEntity(name);
+ }
+
+ public void endEntity (String name)
+ throws SAXException {
+ next.endEntity(name);
+ }
+
+ public void startCDATA ()
+ throws SAXException {
+ next.startCDATA();
+ }
+
+ public void endCDATA ()
+ throws SAXException {
+ next.endCDATA();
+ }
+
+ public void comment (char ch[], int start, int length)
+ throws SAXException {
+ if (!inDTD) {
+ next.comment(ch, start, length);
+ }
+ }
+ }
+
+}
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/core/xml/impl/JaxpSAXParser.java
------------------------------------------------------------------------------
svn:keywords = Id