Author: ssolsagl
Date: 2007-04-27 07:48:20 +0200 (Fri, 27 Apr 2007)
New Revision: 4894
Modified:
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/URLVelocityTemplateLoaderDebug.java
Log:
cleaned up
Modified:
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/URLVelocityTemplateLoaderDebug.java
===================================================================
---
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/URLVelocityTemplateLoaderDebug.java
2007-04-26 17:48:12 UTC (rev 4893)
+++
trunk/core-api/src/main/java/no/schibstedsok/searchportal/view/velocity/URLVelocityTemplateLoaderDebug.java
2007-04-27 05:48:20 UTC (rev 4894)
@@ -4,93 +4,150 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
+import java.io.StringWriter;
+import java.io.Writer;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
import org.apache.log4j.Logger;
import org.apache.velocity.exception.ResourceNotFoundException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
/**
- * UrlVelocityTemplateLoaderDebug adds border around every
- * velocity template that is loaded. This is a nice way to
- * see wich templates are loaded and where they are loaded.
+ * UrlVelocityTemplateLoaderDebug adds border around every velocity template
+ * that is loaded. This is a nice way to see wich templates are loaded and
where
+ * they are loaded.
*
* @author Ola M Sagli <a mailto="[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>
- *
+ *
*/
-public class URLVelocityTemplateLoaderDebug extends URLVelocityTemplateLoader {
-
- private static Logger log =
Logger.getLogger(URLVelocityTemplateLoaderDebug.class);
- static String HTML_DIV = "<div style=\"margin: 3px; border: 1px solid
#C0C0C0\">\n";
- static int NOT_MATCH = -1;
-
- private String header(String content) {
- return "<div style=\"background:#DDDDDD; font-size:10px\">" +
content + "</div>\n";
- }
+public class URLVelocityTemplateLoaderDebug extends URLResourceLoader {
+ static String DIV_TAG = "div";
+ static String STYLE_ATTRIB = "style";
+ static String STYLE_BORDER = "margin:3px;border:1px solid #C0C0C0";
+ static String STYLE_HEADING="background:#DDDDDD;font-size:10px";
+
/**
- * getResourceStream() loads resource from url. Then add frame and
display velocity filename
- * before returning the content.
+ * getResourceStream() loads resource from url. Then add border around
the
+ * template so its easy to see wich templates are loaded.
*/
- @Override
- public InputStream getResourceStream(String url) throws
ResourceNotFoundException {
- log.info("getResourceStream() " + url);
+ public InputStream getResourceStreamDebug(String url)
+ throws ResourceNotFoundException {
- String showBorder =
System.getProperty("VELOCITY_DEBUG_BORDER");
-
- if("false".equals(showBorder)) {
+ boolean VELOCITY_DEBUG =
"true".equals(System.getProperty("VELOCITY_DEBUG"));
+
+ if (!VELOCITY_DEBUG) {
return super.getResourceStream(url);
}
+ InputStream stream = null;
+ String filePath = url.replaceAll("http://(.*?)/",
"/").replace("locahost", "");
+ String templatesDir =
System.getProperty("VELOCITY_TEMPLATES_DIR");
+
+ stream = getStream(templatesDir, filePath, url);
+ // If rss, means the output is xml.
+ if (url.indexOf("rss") != -1) {
+ return stream;
+ }
+
+ byte byteContent[];
+ try {
+ byteContent = new byte[stream.available()];
+ stream.read(byteContent);
+ stream.close();
+ } catch (IOException e) {
+ throw new ResourceNotFoundException(e.getMessage());
+ }
+
+ // Create html
+ String template= new String(byteContent);
+ StringWriter writer = new StringWriter();
+ Document doc = createDocument();
- String relUrl = url.replaceAll("http://(.*?)/", "/");
+ Element border = doc.createElement(DIV_TAG);
+ border.setAttribute(STYLE_ATTRIB, STYLE_BORDER);
+
+ Element divHeader = doc.createElement(DIV_TAG);
+ divHeader.setAttribute(STYLE_ATTRIB, STYLE_HEADING);
+ divHeader.appendChild(doc.createTextNode(filePath));
+ border.appendChild(divHeader);
+
+ border.appendChild(doc.createCDATASection(template));
+ doc.appendChild(border);
- InputStream stream = null;
- String base = System.getProperty("VELOCITY_DEBUG_TEMPLATES");
+ internalWriteDocument(doc, writer);
- if(base == null) {
- stream = super.getResourceStream(url);
- } else {
+ String result = writer.getBuffer().toString();
+ result = result.replace("<![CDATA[", "");
+ result = result.replace("]]>", "");
+ System.out.println("Tamplate =" + result);
+ // log.info("Result: " + buffer.toString());
+ return new ByteArrayInputStream(result.getBytes());
+ }
+
+ /*
+ * Get stream from file of or url.
+ */
+ private InputStream getStream(String templatesDir, String
filePath,String url) {
+
+ System.out.println("*** Loading: " + templatesDir + filePath +
", or url: " + url);
+
+ if(templatesDir == null) {
+ return super.getResourceStream(url);
+ }
+
+ File file = new File(templatesDir + filePath);
+ if (file.exists()) {
try {
- relUrl = relUrl.replace("localhost", "");
- File file = new File(base + relUrl);
- log.info("Load template from " +
file.getAbsolutePath()+ "(exist =" + file.exists() + ")");
- if(file.exists()) {
-
- stream = new FileInputStream(new
File(base + relUrl));
- }else {
- stream = super.getResourceStream(url);
- }
+ return new FileInputStream(file);
} catch (FileNotFoundException ignore) {
- System.err.println("Error: " + ignore);
+ return super.getResourceStream(url);
}
+ } else {
+ return super.getResourceStream(url);
}
- if(url.indexOf("rss") != -1) {
- return stream;
- }
-
- StringBuffer buffer = new StringBuffer();
- try {
+ }
+ // -- Write the document to the writer
+ private void internalWriteDocument(Document d, Writer w) {
+ DOMSource source = new DOMSource(d);
+ StreamResult result = new StreamResult(w);
- byte b[] = new byte[stream.available()];
- stream.read(b);
- stream.close();
- String content = new String(b);
-
- if(content.indexOf("<content") != NOT_MATCH) {
- String output =
content.toString().replaceAll("<content(.*?)>",
- "<content $1>\n" + HTML_DIV + " " +
header(url) + " $1 ");
- output = output.replaceAll("</content>",
"</div>\n</content>");
- return new ByteArrayInputStream(output.getBytes());
- }
- // means new templatestyle
- buffer.append(HTML_DIV);
- buffer.append(header(relUrl));
- buffer.append(content);
- buffer.append(" \n</div>\n");
- //log.info("Result: " + buffer.toString());
- return new ByteArrayInputStream(buffer.toString().getBytes());
-
- }catch(Exception e) {
- throw new RuntimeException(e.getMessage());
- }
- }
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer;
+ try {
+ transformer = factory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
+ "yes");
+ transformer.transform(source, result);
+ } catch (TransformerConfigurationException e) {
+ throw new RuntimeException("Xml Parser: " + e);
+ } catch (TransformerException ignore) {
+ }
+ }
+
+ // -- Create a DOM document
+ private Document createDocument() {
+ DocumentBuilderFactory docFactory = DocumentBuilderFactory
+ .newInstance();
+ DocumentBuilder builder = null;
+ try {
+ builder = docFactory.newDocumentBuilder();
+ } catch (ParserConfigurationException e) {
+ throw new RuntimeException(e);
+ }
+ Document doc = builder.newDocument();
+ return doc;
+ }
}
_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits