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 <code>Utility</code> object helper
    * @author <a href="mailto:[EMAIL PROTECTED]";>Ricardo Rocha</a>
  - * @author <a href="mailto:[EMAIL PROTECTED]>Berin Loritsch</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Berin Loritsch</a>
    * @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:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to