Package org.apache.cocoon.environment

Examples of org.apache.cocoon.environment.Source


                                   MarkupLanguage markupLanguage,
                                   ProgrammingLanguage programmingLanguage,
                                   SourceResolver resolver)
        throws Exception {

        Source source = resolver.resolve(fileName);
        try {
            // Input Source
            InputSource is = source.getInputSource();
            // Generate code
            String code = markupLanguage.generateCode(is, normalizedName, programmingLanguage, resolver);
            String encoding = markupLanguage.getEncoding();
            // Format source code if applicable
            CodeFormatter codeFormatter = programmingLanguage.getCodeFormatter();
            if (codeFormatter != null) {
                code = codeFormatter.format(code, encoding);
            }
            // Store generated code
            String sourceFilename = new StringBuffer(normalizedName).append(".")
                                    .append(programmingLanguage.getSourceExtension()).toString();
            repository.store(sourceFilename, code);
            // [Compile]/Load generated program
            Class program = programmingLanguage.load(normalizedName, this.workDir, markupLanguage.getEncoding());
            // Store generated program in cache
            this.addCompiledComponent(newManager, normalizedName, program);
   
            // FIXME: Do we want this functionality?  All analysis says no.
            if (markupLanguage.getClass().equals(SitemapMarkupLanguage.class)) {
                try {
                    select("sitemap");
                } catch (Exception e) {
                    // If the root sitemap has not been compiled, add an alias here.
                    this.addCompiledComponent(newManager, "sitemap", program);
                }
            }
   
            return program;
        } finally {
            source.recycle();
        }
    }
View Full Code Here


  {
    getLogger().debug("XSLTProcessorImpl: resolve(href = " + href
                      + ", base = " + base + "); resolver = " + resolver);

    try {
      Source xslSource;
      if (href.indexOf(":") > 1) {
        xslSource = resolver.resolve(href);
      }
      else {
        // patch for a null pointer passed as base
        if (base == null)
          throw new IllegalArgumentException("Null pointer passed as base");

        // is the base a file or a real url
        if (!base.startsWith("file:")) {
          int lastPathElementPos = base.lastIndexOf('/');
          if (lastPathElementPos == -1) {
            // this should never occur as the base should
            // always be protocol:/....
            return null; // we can't resolve this
          }
          else {
            xslSource = resolver.resolve(new StringBuffer(base.substring(0, lastPathElementPos))
                                         .append("/").append(href).toString());
          }
        }
        else {
          File parent = new File(base.substring(5));
          File parent2 = new File(parent.getParentFile(), href);
          xslSource = resolver.resolve(parent2.toURL().toExternalForm());
        }
      }
      InputSource is = xslSource.getInputSource();
      getLogger().debug("xslSource = " + xslSource
                        + ", system id = " + is.getSystemId());
      return new StreamSource(is.getByteStream(),
                              is.getSystemId());
    } catch (ResourceNotFoundException rnfe) {
View Full Code Here

     * @return The generated key hashes the src
     */
    public long generateKey() {
        try {
            long key = 0;
            Source current;
            for (int i = 0; i < this.parts.size(); i++) {
                current = ((Part)this.parts.get(i)).source;
                if (current.getLastModified() == 0) {
                        return 0;
                } else {
                    key += HashUtil.hash(current.getSystemId());
                }
            }
            return key;
        } catch (Exception e) {
            getLogger().error("ContentAggregator: could not generateKey", e);
View Full Code Here

     *         component is currently not cacheable.
     */
    public CacheValidity generateValidity() {
        try {
            AggregatedCacheValidity v = new AggregatedCacheValidity();
            Source current;
            for (int i = 0; i < this.parts.size(); i++) {
                current = ((Part)this.parts.get(i)).source;
                if (current.getLastModified() == 0) {
                        return null;
                } else {
                    v.add(new TimeStampCacheValidity(current.getLastModified()));
                }
            }
            return v;
        } catch (Exception e) {
            getLogger().error("ContentAggregator: could not generateKey", e);
View Full Code Here

        this.manager = manager;
    }
   
    public Source getSource(Environment environment, String location)
      throws ProcessingException, MalformedURLException, IOException {
        Source result = new FileSource(location, this.manager);
        setupLogger(result);
        return result;
    }
View Full Code Here

                return sourceFactory.getSource(environment, location);
            }
        }

        // default implementation
        Source result = new URLSource(this.urlFactory.getURL(location), this.manager);
        if (result instanceof LogEnabled) {
            ((LogEnabled)result).enableLogging(getLogger());
        }
        return result;
    }
View Full Code Here

        Logicsheet logicsheet = (Logicsheet)logicsheetCache.get(CACHE_PREFIX + logicsheetLocation);
        if (logicsheet == null) {
            // Logicsheet is reusable (across multiple XSPs) object,
            // and it is resolved via urlResolver, and not via per-request
            // temporary resolver.
            Source inputSource = this.urlResolver.resolve(logicsheetLocation);

            // Resolver (local) could not be used as it is temporary
            // (per-request) object, yet Logicsheet is being cached and reused
            // across multiple requests. "Global" url-factory-based resolver
            // passed to the Logicsheet.
View Full Code Here

        if (httpResponse == null || httpRequest == null || httpContext == null) {
            throw new ProcessingException("JSPReader can be used only in a Servlet/JSP environment");
        }

        JSPEngine engine = null;
        Source src = null;
        try {
            // KP: A hacky way of source resolving.
            // Why context:// protocol returns not a string in URL format,
            // but a system-dependent path with 'file:' prefix?
            String contextDir = new File(httpContext.getRealPath("/")).toURL().toExternalForm();
            src = this.resolver.resolve(this.source);
            String url = src.getSystemId();
            if(url.startsWith(contextDir)) {
                // File is located under contextDir, using relative file name
                url = url.substring(contextDir.length());
            }
            if (url.startsWith("file:")) {
                // we need a relative path
                url = url.substring(5);
            }

            getLogger().debug("JSPReader executing JSP:" + url);

            engine = (JSPEngine)this.manager.lookup(JSPEngine.ROLE);
            byte[] bytes = engine.executeJSP(url, httpRequest, httpResponse, httpContext);

            byte[] buffer = new byte[8192];
            int length = -1;

            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            while ((length = bais.read(buffer)) > -1) {
                out.write(buffer, 0, length);
            }
            bais.close();
            bais = null;
            out.flush();
            //
        } catch (ServletException e) {
            getLogger().debug("ServletException in JSPReader.generate()", e);
            getLogger().debug("Embedded ServletException JSPReader.generate()", e.getRootCause());
            throw new ProcessingException("ServletException in JSPReader.generate()",e.getRootCause());
        } catch (IOException e) {
            getLogger().debug("IOException in JSPReader.generate()", e);
            throw new ProcessingException("IOException JSPReader.generate()",e);
        } catch (ProcessingException e) {
            throw e;
        } catch (Exception e) {
            getLogger().debug("Exception in JSPReader.generate()", e);
            throw new ProcessingException("Exception JSPReader.generate()",e);
        } finally {
            if (src != null) src.recycle();
            if (engine != null) this.manager.release(engine);
        }
    }
View Full Code Here

                                  String markupLanguageName,
                                  String programmingLanguageName,
                                  SourceResolver resolver)
        throws Exception {

        Source source = resolver.resolve(fileName);
        try {
            // Set filenames
            StringBuffer contextFilename = new StringBuffer(this.rootPackage.replace('.', File.separatorChar));
            contextFilename.append(File.separator);
            String id = source.getSystemId();
            if(id.startsWith(this.contextDir)) {
                // VG: File is located under contextDir, using relative file name
                contextFilename.append(id.substring(this.contextDir.length()));
            } else {
                // VG: File is located outside of contextDir, using systemId
                getLogger().debug("Loading from external source " + id);
                contextFilename.append(id);
            }
            String normalizedName = IOUtils.normalizedFilename(contextFilename.toString());

            // Ensure no 2 requests for the same file overlap
            Class program = null;
            CompiledComponent programInstance = null;

            // Attempt to load program object from cache
            try {
                programInstance = (CompiledComponent) select(normalizedName);
            } catch (Exception e) {
                getLogger().debug("The instance was not accessible from the internal cache. Proceeding.");
            }

            if ((programInstance == null) && this.preload) {
                String className = normalizedName.replace(File.separatorChar, '.');
                try {
                    program = this.classManager.loadClass(className);
                    this.addCompiledComponent(newManager, normalizedName, program);
                    programInstance = (CompiledComponent) select(normalizedName);
                } catch (Exception e) {
                    getLogger().debug("The class was not preloaded");
                }
            }

            if (programInstance == null) {
              programInstance =
                    this.createResource(
                        newManager, fileName, normalizedName,
                        markupLanguageName, programmingLanguageName, resolver
                    );
            }

            if (!this.autoReload) {
                return programInstance;
            }

            /*
             * FIXME: It's the program (not the instance) that must
             * be queried for changes!!!
             */
            long lastModified = source.getLastModified();
            if (programInstance != null &&
                (lastModified == 0 || programInstance.modifiedSince(lastModified)))
            {
                // Release the component.
                release(programInstance);

                // Unload program
                ProgrammingLanguage programmingLanguage = (ProgrammingLanguage)this.languageSelector.select(programmingLanguageName);
                programmingLanguage.setLanguageName(programmingLanguageName);
                programmingLanguage.unload(program, normalizedName, this.workDir);
                this.cache.removeGenerator(normalizedName);

                // Invalidate previous program/instance pair
                program = null;
                programInstance = null;
            }

            if (programInstance == null) {
                if (program == null) {
                    programInstance =
                        this.createResource(
                            newManager, fileName, normalizedName,
                            markupLanguageName, programmingLanguageName,
                            resolver
                        );
                } else {
                    programInstance = (CompiledComponent) select(normalizedName);
                }
            }

            return programInstance;
        } finally {
            source.recycle();
        }
    }
View Full Code Here

                                   MarkupLanguage markupLanguage,
                                   ProgrammingLanguage programmingLanguage,
                                   SourceResolver resolver)
        throws Exception {

        Source source = resolver.resolve(fileName);
        try {
            // Input Source
            InputSource is = source.getInputSource();
            // Generate code
            String code = markupLanguage.generateCode(is, normalizedName, programmingLanguage, resolver);
            String encoding = markupLanguage.getEncoding();
            // Format source code if applicable
            CodeFormatter codeFormatter = programmingLanguage.getCodeFormatter();
            if (codeFormatter != null) {
                code = codeFormatter.format(code, encoding);
            }
            // Store generated code
            String sourceFilename = new StringBuffer(normalizedName).append(".")
                                    .append(programmingLanguage.getSourceExtension()).toString();
            repository.store(sourceFilename, code);
            // [Compile]/Load generated program
            Class program = programmingLanguage.load(normalizedName, this.workDir, markupLanguage.getEncoding());
            // Store generated program in cache
            this.addCompiledComponent(newManager, normalizedName, program);

            // FIXME: Do we want this functionality?  All analysis says no.
            if (markupLanguage.getClass().equals(SitemapMarkupLanguage.class)) {
                try {
                    select("sitemap");
                } catch (Exception e) {
                    // If the root sitemap has not been compiled, add an alias here.
                    this.addCompiledComponent(newManager, "sitemap", program);
                }
            }

            return program;
        } finally {
            source.recycle();
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.cocoon.environment.Source

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.