protected String getTemplateText(String template, String folder) throws IOException {
    InputStream inputStream = null;
    if (resourceLoader != null && grailsApplication.isWarDeployed()) {
      inputStream = resourceLoader.getResource(folder + template).getInputStream();
    } else {
      AbstractResource templateFile = getTemplateResource(template);
      if (templateFile.exists()) {
        inputStream = templateFile.getInputStream();
      }
    }

    return inputStream == null ? null : IOGroovyMethods.getText(inputStream);
  }
Пример #2
0
  public ParsedResult parseCode(String uri, String gsp) throws IOException {
    // Simulate what the parser does so we get it in the encoding expected
    Object enc = GrailsWebUtil.currentConfiguration().get("grails.views.gsp.encoding");
    if ((enc == null) || (enc.toString().trim().length() == 0)) {
      enc = System.getProperty("file.encoding", "us-ascii");
    }

    InputStream gspIn = new ByteArrayInputStream(gsp.getBytes(enc.toString()));
    GroovyPageParser parse = new GroovyPageParser(uri, uri, uri, gspIn, enc.toString(), "HTML");

    InputStream in = parse.parse();
    ParsedResult result = new ParsedResult();
    result.parser = parse;
    result.generatedGsp = IOGroovyMethods.getText(in, enc.toString());
    result.htmlParts = parse.getHtmlPartsArray();
    return result;
  }
Пример #3
0
 /**
  * Get the class of the scriptName in question, so that you can instantiate Groovy objects with
  * caching and reloading.
  *
  * @param scriptName resource name pointing to the script
  * @return the loaded scriptName as a compiled class
  * @throws ResourceException if there is a problem accessing the script
  * @throws ScriptException if there is a problem parsing the script
  */
 public Class loadScriptByName(String scriptName) throws ResourceException, ScriptException {
   URLConnection conn = rc.getResourceConnection(scriptName);
   String path = conn.getURL().toExternalForm();
   ScriptCacheEntry entry = scriptCache.get(path);
   Class clazz = null;
   if (entry != null) clazz = entry.scriptClass;
   try {
     if (isSourceNewer(entry)) {
       try {
         String encoding = conn.getContentEncoding() != null ? conn.getContentEncoding() : "UTF-8";
         clazz =
             groovyLoader.parseClass(
                 IOGroovyMethods.getText(conn.getInputStream(), encoding), path);
       } catch (IOException e) {
         throw new ResourceException(e);
       }
     }
   } finally {
     forceClose(conn);
   }
   return clazz;
 }