vgritsenko 01/09/07 21:31:11 Modified: src/org/apache/cocoon/components/language/markup/xsp XSPObjectHelper.java src/org/apache/cocoon/components/source SitemapSource.java URLSource.java src/org/apache/cocoon/generation FileGenerator.java src/org/apache/cocoon/xml XMLizable.java Log: exception handling in XMLizable and sources Revision Changes Path 1.5 +9 -3 xml-cocoon2/src/org/apache/cocoon/components/language/markup/xsp/XSPObjectHelper.java Index: XSPObjectHelper.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/components/language/markup/xsp/XSPObjectHelper.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- XSPObjectHelper.java 2001/08/24 10:03:45 1.4 +++ XSPObjectHelper.java 2001/09/08 04:31:10 1.5 @@ -10,6 +10,8 @@ import org.apache.cocoon.xml.XMLFragment; import org.apache.cocoon.xml.XMLizable; import org.apache.cocoon.xml.dom.DOMStreamer; +import org.apache.cocoon.ProcessingException; + import org.w3c.dom.Node; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; @@ -24,7 +26,7 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Ricardo Rocha</a> * @author <a href="[EMAIL PROTECTED]">Sylvain Wallez</a> * (Cocoon1 <code>xspExpr()</code> methods port) - * @version CVS $Revision: 1.4 $ $Date: 2001/08/24 10:03:45 $ + * @version CVS $Revision: 1.5 $ $Date: 2001/09/08 04:31:10 $ */ public class XSPObjectHelper { /** @@ -281,7 +283,11 @@ { if (v != null) { - v.toSAX(contentHandler); + try{ + v.toSAX(contentHandler); + }catch(ProcessingException e){ + throw new SAXException(e); + } } } @@ -352,7 +358,7 @@ } // Check handled object types in case they were not typed in the XSP - + // XMLizable if (v instanceof XMLizable) { 1.21 +29 -15 xml-cocoon2/src/org/apache/cocoon/components/source/SitemapSource.java Index: SitemapSource.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/components/source/SitemapSource.java,v retrieving revision 1.20 retrieving revision 1.21 diff -u -r1.20 -r1.21 --- SitemapSource.java 2001/09/06 14:04:11 1.20 +++ SitemapSource.java 2001/09/08 04:31:10 1.21 @@ -3,7 +3,7 @@ * ------------------------------------------------------------------------- * * This software is published under the terms of the Apache Software License * * version 1.1, a copy of which has been included with this distribution in * - * the LICENSE file. s * + * the LICENSE file. * *****************************************************************************/ package org.apache.cocoon.components.source; @@ -48,7 +48,7 @@ * Description of a source which is defined by a pipeline. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Revision: 1.20 $ $Date: 2001/09/06 14:04:11 $ + * @version CVS $Revision: 1.21 $ $Date: 2001/09/08 04:31:10 $ */ public final class SitemapSource @@ -86,7 +86,7 @@ private Source redirectSource; /** The <code>SAXException</code> if unable to get resource */ - private SAXException exception; + private ProcessingException exception; /** Do I need a refresh ? */ private boolean needsRefresh; @@ -110,7 +110,7 @@ } // does the uri point to this sitemap or to the root sitemap? - if (uri.startsWith("//") == true) { + if (uri.startsWith("//")) { uri = uri.substring(2); Processor processor = null; try { @@ -120,7 +120,7 @@ } this.prefix = ""; // start at the root this.processor = processor; - } else if (uri.startsWith("/") == true) { + } else if (uri.startsWith("/")) { this.prefix = null; uri = uri.substring(1); this.processor = sitemap; @@ -155,7 +155,9 @@ * is not possible to determine the date. */ public long getLastModified() { - if (this.needsRefresh == true) this.refresh(); + if (this.needsRefresh) { + this.refresh(); + } return this.lastModificationDate; } @@ -172,11 +174,13 @@ */ public InputStream getInputStream() throws ProcessingException, IOException { + if (this.needsRefresh) { + this.refresh(); + } // VG: Why exception is not thrown in constructor? if (this.exception != null) { - throw new ProcessingException(this.exception); + throw this.exception; } - if (this.needsRefresh == true) this.refresh(); SitemapComponentSelector serializerSelector = null; Serializer serializer = null; try { @@ -191,6 +195,8 @@ return new ByteArrayInputStream(os.toByteArray()); } catch (ComponentException cme) { throw new ProcessingException("could not lookup pipeline components", cme); + } catch (ProcessingException e) { + throw e; } catch (Exception e) { throw new ProcessingException("Exception during processing of " + this.systemId, e); } finally { @@ -248,10 +254,13 @@ this.redirectSource = this.environment.resolve(redirectURL); this.lastModificationDate = this.redirectSource.getLastModified(); } + } catch (ProcessingException e) { + reset(); + this.exception = e; } catch (Exception e) { reset(); - this.exception = new SAXException("Could not get sitemap source " - + this.systemId, e); + this.exception = new ProcessingException("Could not get sitemap source " + + this.systemId, e); } this.needsRefresh = false; } @@ -270,11 +279,13 @@ * Stream content to the content handler */ public void toSAX(ContentHandler contentHandler) - throws SAXException { + throws SAXException, ProcessingException { + if (this.needsRefresh) { + this.refresh(); + } if (this.exception != null) { throw this.exception; } - if (this.needsRefresh == true) this.refresh(); try { XMLConsumer consumer; if (contentHandler instanceof XMLConsumer) { @@ -291,10 +302,13 @@ eventPipeline.process(this.environment); } } catch (ComponentException cme) { - throw new SAXException("could not lookup pipeline components", cme); + throw new ProcessingException("Could not lookup pipeline components", cme); + } catch (ProcessingException e) { + // Preserve original exception + throw e; } catch (Exception e) { - throw new SAXException("Exception during processing of " - + this.systemId, e); + throw new ProcessingException("Exception during processing of " + + this.systemId, e); } finally { reset(); } 1.13 +58 -39 xml-cocoon2/src/org/apache/cocoon/components/source/URLSource.java Index: URLSource.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/components/source/URLSource.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- URLSource.java 2001/09/06 14:04:11 1.12 +++ URLSource.java 2001/09/08 04:31:10 1.13 @@ -11,6 +11,7 @@ import org.apache.avalon.framework.component.ComponentException; import org.apache.avalon.framework.component.ComponentManager; import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.ResourceNotFoundException; import org.apache.cocoon.components.parser.Parser; import org.apache.cocoon.environment.ModifiableSource; import org.apache.cocoon.xml.XMLConsumer; @@ -22,6 +23,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.FileNotFoundException; import java.io.InputStream; import java.lang.reflect.Method; import java.net.URL; @@ -31,7 +33,7 @@ * Description of a source which is described by an URL. * * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Revision: 1.12 $ $Date: 2001/09/06 14:04:11 $ + * @version CVS $Revision: 1.13 $ $Date: 2001/09/08 04:31:10 $ */ public final class URLSource implements ModifiableSource { @@ -125,27 +127,36 @@ /** * Return an <code>InputStream</code> object to read from the source. + * + * @throws ResourceNotFoundException if file not found or + * HTTP location does not exist. + * @throws IOException if I/O error occured. */ public InputStream getInputStream() - throws IOException { + throws IOException, ProcessingException { this.getInfos(); - InputStream input = null; - if (this.isFile == true) { - input = new FileInputStream(this.systemId.substring(FILE.length())); - } else { - if (this.connection == null) { - this.connection = this.url.openConnection(); - /* The following requires a jdk 1.3 */ - String userInfo = this.getUserInfo(); - if (this.url.getProtocol().startsWith("http") == true && userInfo != null) { - this.connection.setRequestProperty("Authorization","Basic "+encodeBASE64(userInfo)); + try{ + InputStream input = null; + if (this.isFile == true) { + input = new FileInputStream(this.systemId.substring(FILE.length())); + } else { + if (this.connection == null) { + this.connection = this.url.openConnection(); + /* The following requires a jdk 1.3 */ + String userInfo = this.getUserInfo(); + if (this.url.getProtocol().startsWith("http") == true && userInfo != null) { + this.connection.setRequestProperty("Authorization","Basic "+encodeBASE64(userInfo)); + } } - } - input = this.connection.getInputStream(); - this.connection = null; // make sure a new connection is created next time + input = this.connection.getInputStream(); + this.connection = null; // make sure a new connection is created next time + } + return input; + }catch(FileNotFoundException e){ + throw new ResourceNotFoundException("Resource not found " + + this.systemId); } - return input; } private static boolean checkedURLClass = false; @@ -203,9 +214,13 @@ /** * Return a new <code>InputSource</code> object + * + * @throws ResourceNotFoundException if file not found or + * HTTP location does not exist. + * @throws IOException if I/O error occured. */ public InputSource getInputSource() - throws IOException { + throws IOException, ProcessingException { InputSource newObject = new InputSource(this.getInputStream()); newObject.setSystemId(this.systemId); return newObject; @@ -292,33 +307,37 @@ return new String ( out ); } - /** - * Stream content to a content handler or to an XMLConsumer - */ - public void toSAX(ContentHandler handler) - throws SAXException { - Parser parser = null; - try { - parser = (Parser)this.manager.lookup(Parser.ROLE); + /** + * Stream content to a content handler or to an XMLConsumer + */ + public void toSAX(ContentHandler handler) + throws SAXException, ProcessingException + { + Parser parser = null; + try { + parser = (Parser)this.manager.lookup(Parser.ROLE); if (handler instanceof XMLConsumer) { - parser.setConsumer((XMLConsumer)handler); + parser.setConsumer((XMLConsumer)handler); } else { - parser.setContentHandler(handler); + parser.setContentHandler(handler); if (handler instanceof LexicalHandler) { parser.setLexicalHandler((LexicalHandler)handler); } } - parser.parse(this.getInputSource()); - } catch (Exception e){ - throw new SAXException("Exception in URLSource.stream()", e); - } finally { - if (parser != null) this.manager.release(parser); - } - } - - public void recycle() - { - } -} + parser.parse(this.getInputSource()); + } catch (ProcessingException e){ + // Preserve original exception + throw e; + } catch (Exception e){ + throw new ProcessingException("Exception during processing of " + + this.systemId, e); + } finally { + if (parser != null) this.manager.release(parser); + } + } + public void recycle() + { + } +} 1.23 +6 -4 xml-cocoon2/src/org/apache/cocoon/generation/FileGenerator.java Index: FileGenerator.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/generation/FileGenerator.java,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- FileGenerator.java 2001/09/06 11:07:48 1.22 +++ FileGenerator.java 2001/09/08 04:31:10 1.23 @@ -35,7 +35,7 @@ * @author <a href="mailto:[EMAIL PROTECTED]">Pierpaolo Fumagalli</a> * (Apache Software Foundation, Exoffice Technologies) * @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a> - * @version CVS $Revision: 1.22 $ $Date: 2001/09/06 11:07:48 $ + * @version CVS $Revision: 1.23 $ $Date: 2001/09/08 04:31:10 $ */ public class FileGenerator extends ComposerGenerator implements Cacheable, Recyclable { @@ -101,17 +101,19 @@ * Generate XML data. */ public void generate() - throws IOException, SAXException { + throws IOException, SAXException, ProcessingException { try { getLogger().debug("processing file " + super.source); getLogger().debug("file resolved to " + this.inputSource.getSystemId()); this.inputSource.toSAX(super.xmlConsumer); + } catch (ProcessingException e) { + throw e; } catch (Exception e) { getLogger().error("Could not read resource " + this.inputSource.getSystemId(), e); - throw new SAXException("Could not read resource " - + this.inputSource.getSystemId(), e); + throw new ProcessingException("Could not read resource " + + this.inputSource.getSystemId(), e); } } } 1.2 +5 -3 xml-cocoon2/src/org/apache/cocoon/xml/XMLizable.java Index: XMLizable.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/xml/XMLizable.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- XMLizable.java 2001/08/24 10:03:45 1.1 +++ XMLizable.java 2001/09/08 04:31:10 1.2 @@ -7,6 +7,8 @@ *****************************************************************************/ package org.apache.cocoon.xml; +import org.apache.cocoon.ProcessingException; + import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; @@ -15,7 +17,7 @@ * of their current state as SAX events. * * @author <a href="mailto:[EMAIL PROTECTED]">Sylvain Wallez</a> - * @version CVS $Revision: 1.1 $ $Date: 2001/08/24 10:03:45 $ + * @version CVS $Revision: 1.2 $ $Date: 2001/09/08 04:31:10 $ */ public interface XMLizable { @@ -25,6 +27,6 @@ * that <code>handler</code> can actually be a {@link XMLConsumer} that accepts such * events. */ - void toSAX(ContentHandler handler) throws SAXException; - + void toSAX(ContentHandler handler) throws SAXException, ProcessingException; + } ---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]