Return-Path: Delivered-To: apmail-xml-cocoon-cvs-archive@xml.apache.org Received: (qmail 89753 invoked by uid 500); 14 Jul 2002 15:05:38 -0000 Mailing-List: contact cocoon-cvs-help@xml.apache.org; run by ezmlm Precedence: bulk Reply-To: cocoon-dev@xml.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list cocoon-cvs@xml.apache.org Received: (qmail 89744 invoked by uid 500); 14 Jul 2002 15:05:37 -0000 Delivered-To: apmail-xml-cocoon2-cvs@apache.org Date: 14 Jul 2002 15:05:37 -0000 Message-ID: <20020714150537.35555.qmail@icarus.apache.org> From: haul@apache.org To: xml-cocoon2-cvs@apache.org Subject: cvs commit: xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp XSPUtil.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N haul 2002/07/14 08:05:37 Modified: src/java/org/apache/cocoon/components/language/markup/xsp Tag: cocoon_2_0_3_branch XSPUtil.java Log: backport fix util.xsl Revision Changes Path No revision No revision 1.6.2.1 +96 -2 xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/XSPUtil.java Index: XSPUtil.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/language/markup/xsp/XSPUtil.java,v retrieving revision 1.6 retrieving revision 1.6.2.1 diff -u -r1.6 -r1.6.2.1 --- XSPUtil.java 22 Feb 2002 07:00:08 -0000 1.6 +++ XSPUtil.java 14 Jul 2002 15:05:37 -0000 1.6.2.1 @@ -63,17 +63,24 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.apache.cocoon.environment.SourceResolver; +import org.apache.avalon.framework.component.ComponentManager; +import org.apache.avalon.framework.component.Component; +import org.apache.cocoon.environment.Source; +import java.lang.Long; + import java.io.*; import java.net.URLDecoder; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; +import java.util.HashMap; /** * The XSP Utility object helper * @author Ricardo Rocha - * @author Berin Loritsch * @version CVS $Id$ */ public class XSPUtil { @@ -270,4 +277,91 @@ Context context = ObjectModelHelper.getContext(objectModel); return context.getAttribute(name); } + + public static String getSourceAsString(String uri, SourceResolver resolver) throws RuntimeException { + + StringBuffer result = new StringBuffer(); + InputStream stream = null; + Source resource = null; + try { + Map mymap = new HashMap(); + resource = resolver.resolve(uri); + long length = resource.getContentLength(); + stream = new BufferedInputStream(resource.getInputStream()); + if (length != -1) { + byte[] buffer = new byte[(new Long(length)).intValue()]; + stream.read(buffer); + stream.close(); + if (buffer != null) result.append(new String(buffer)); + } else { + int readBytes = 0; + do { + byte[] buffer = new byte[4*1024]; + readBytes = stream.read(buffer); + if (readBytes == -1) break; + if (readBytes > 0) result.append(new String(buffer,0,readBytes)); + } while (true); + stream.close(); + } + } catch (Exception e) { + throw new RuntimeException(e.getMessage()); + } finally { + if ( stream != null ) + try {stream.close();} catch (Exception ase) { throw new RuntimeException(ase.getMessage()); } + if ( resource != null ) + resource.recycle(); + } + return result.toString(); + } + + + public static void includeSource(String uri, String base, SourceResolver resolver, ContentHandler contentHandler) + throws RuntimeException { + + base = (base == null? "" : base); + Source source = null; + try { + source = resolver.resolve(base+uri); + source.toSAX(new org.apache.cocoon.xml.IncludeXMLConsumer(contentHandler)); + } catch (Exception e) { + throw new RuntimeException("Error including source "+base+" "+uri+":"+e.getMessage()); + } finally { + if (source != null) + source.recycle(); + } + } + + public static void includeString(String string, ComponentManager manager, ContentHandler contentHandler) + throws RuntimeException { + + XSPUtil.includeInputSource(new InputSource( new StringReader( String.valueOf(string))), manager, contentHandler); + } + + public static void includeFile(String name, ComponentManager manager, ContentHandler contentHandler, Map objectModel) + throws RuntimeException { + + try { + XSPUtil.includeInputSource(new InputSource(new FileReader(XSPUtil.relativeFilename(name,objectModel))), + manager, contentHandler); + } catch (IOException e) { + throw new RuntimeException("Could not include file "+name+" : " + e.getMessage()); + } + } + + public static void includeInputSource(InputSource source, ComponentManager manager, ContentHandler contentHandler) + throws RuntimeException { + + Parser newParser = null; + + try { + newParser = (Parser) manager.lookup(Parser.ROLE); + XSPUtil.include(source, contentHandler, newParser); + } catch (Exception e) { + throw new RuntimeException("Could not include page " + e.getMessage()); + } finally { + if (newParser != null) manager.release((Component) newParser); + } + } + + } ---------------------------------------------------------------------- In case of troubles, e-mail: webmaster@xml.apache.org To unsubscribe, e-mail: cocoon-cvs-unsubscribe@xml.apache.org For additional commands, e-mail: cocoon-cvs-help@xml.apache.org