001: /*
002: * This file is not part of the ItsNat framework.
003: *
004: * Original source code use and closed source derivatives are authorized
005: * to third parties with no restriction or fee.
006: * The original source code is owned by the author.
007: *
008: * This program is distributed AS IS in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: *
012: * Author: Jose Maria Arranz Santamaria
013: * (C) Innowhere Software Services S.L., Spanish company, year 2007
014: */
015:
016: package org.itsnat.feashow;
017:
018: import java.io.BufferedReader;
019: import java.io.FileInputStream;
020: import java.io.FileNotFoundException;
021: import java.io.IOException;
022: import java.io.InputStreamReader;
023: import java.io.UnsupportedEncodingException;
024: import javax.servlet.ServletContext;
025: import org.itsnat.core.ItsNatDocument;
026: import org.itsnat.core.ItsNatServlet;
027: import org.itsnat.core.MarkupTemplate;
028: import org.itsnat.core.domutil.ItsNatDOMUtil;
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Element;
031: import org.w3c.dom.html.HTMLTextAreaElement;
032: import org.xml.sax.InputSource;
033:
034: public class SyntaxHighlighter {
035: public SyntaxHighlighter() {
036: }
037:
038: public static void highlightJava(HTMLTextAreaElement textAreaElem,
039: ItsNatDocument itsNatDoc) {
040: String javaPath = textAreaElem.getAttribute("name");
041: String path = javaPath.replace('.', '/') + ".java";
042:
043: ServletContext context = itsNatDoc.getDocumentTemplate()
044: .getItsNatServlet().getItsNatServletContext()
045: .getServletContext();
046: String pathPrefix = context.getRealPath("/");
047: path = pathPrefix + "WEB-INF/src/" + path;
048:
049: highlightFile(javaPath, path, "UTF-8", textAreaElem, itsNatDoc);
050: }
051:
052: public static void highlightMarkup(
053: HTMLTextAreaElement textAreaElem, ItsNatDocument itsNatDoc) {
054: String markupName = textAreaElem.getAttribute("name");
055:
056: ItsNatServlet servlet = itsNatDoc.getDocumentTemplate()
057: .getItsNatServlet();
058:
059: MarkupTemplate template;
060: template = servlet.getDocFragmentTemplate(markupName);
061: if (template == null)
062: template = servlet.getDocumentTemplate(markupName);
063:
064: InputSource input = template.getInputSource();
065: String path = input.getSystemId().substring("file:".length());
066: String encoding = template.getEncoding();
067:
068: highlightFile(markupName, path, encoding, textAreaElem,
069: itsNatDoc);
070: }
071:
072: private static void highlightFile(String name, String path,
073: String encoding, HTMLTextAreaElement textAreaElem,
074: ItsNatDocument itsNatDoc) {
075: StringBuffer code = new StringBuffer();
076: try {
077: BufferedReader reader = new BufferedReader(
078: new InputStreamReader(new FileInputStream(path),
079: encoding));
080: String line = reader.readLine();
081: while (line != null) {
082: code.append(line);
083: code.append('\n');
084: line = reader.readLine();
085: }
086: reader.close();
087: } catch (FileNotFoundException ex) {
088: throw new RuntimeException(ex);
089: } catch (UnsupportedEncodingException ex) {
090: throw new RuntimeException(ex);
091: } catch (IOException ex) {
092: throw new RuntimeException(ex);
093: }
094:
095: ItsNatDOMUtil.setTextContent(textAreaElem, code.toString());
096:
097: Document doc = itsNatDoc.getDocument();
098: Element script = doc.createElement("script");
099: ItsNatDOMUtil.setTextContent(script,
100: "window.dp.SyntaxHighlighter.HighlightAll(\"" + name
101: + "\"); \n");
102:
103: textAreaElem.getParentNode().appendChild(script);
104: }
105:
106: }
|