Author: maksimkaszynski
Date: 2008-03-21 07:38:14 -0400 (Fri, 21 Mar 2008)
New Revision: 7039
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/ParserFacade.java
trunk/framework/impl/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java
Log:
fixed TemplateEncoder
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/ParserFacade.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/ParserFacade.java 2008-03-21
08:57:58 UTC (rev 7038)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/ParserFacade.java 2008-03-21
11:38:14 UTC (rev 7039)
@@ -16,24 +16,33 @@
*/
public class ParserFacade {
+ private HtmlParser parser;
+
private BaseXMLFilter filter;
- private FacesContext context;
-
public static ParserFacade newInstance(FacesContext context) {
return new ParserFacade(context);
}
- private ParserFacade(FacesContext context) {
+ public ParserFacade(FacesContext context) {
super();
- this.context = context;
this.filter = BaseXMLFilter.getInstance(context);
+ this.parser = getParser(context, filter);
+
}
public void parseHtml(Reader input, Writer output) throws IOException {
+ parser.parseHtml(input, output);
+ reuseParser();
+ }
+
+ protected HtmlParser getParser(FacesContext context, BaseXMLFilter filter) {
UIViewRoot viewRoot = context.getViewRoot();
HtmlParser parser = filter.getParser(filter.getMimetype(), true,
viewRoot.getViewId());
- parser.parseHtml(input, output);
+ return parser;
+ }
+
+ protected void reuseParser() {
filter.reuseParser(parser);
}
Modified:
trunk/framework/impl/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java
===================================================================
---
trunk/framework/impl/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java 2008-03-21
08:57:58 UTC (rev 7038)
+++
trunk/framework/impl/src/main/java/org/richfaces/renderkit/TemplateEncoderRendererBase.java 2008-03-21
11:38:14 UTC (rev 7039)
@@ -5,13 +5,11 @@
import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
-import java.util.Properties;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
@@ -30,10 +28,7 @@
import org.ajax4jsf.javascript.JSReference;
import org.ajax4jsf.javascript.ScriptUtils;
import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
-import org.ajax4jsf.resource.util.URLToStreamHelper;
import org.ajax4jsf.webapp.ParserFacade;
-import org.ajax4jsf.webapp.tidy.TidyParser;
-import org.ajax4jsf.webapp.tidy.TidyXMLFilter;
import org.richfaces.component.TemplateComponent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -65,57 +60,71 @@
return true;
}
+ protected ParserFacade getParserFacade(FacesContext context) {
+ return ParserFacade.newInstance(context);
+ }
+
public void writeScriptBody(Writer writer, String string) throws IOException {
- ParserFacade parserFacade =
ParserFacade.newInstance(FacesContext.getCurrentInstance());
+ ParserFacade parserFacade = getParserFacade(FacesContext.getCurrentInstance());
StringReader stringReader = new StringReader(string);
StringWriter stringWriter = new StringWriter();
parserFacade.parseHtml(stringReader, stringWriter);
-
- Document document = parse(stringWriter.toString());
+ boolean wroteAnything = false;
+ String wellFormed = stringWriter.toString();
+ if (wellFormed != null && wellFormed.length() > 0) {
+ Document document = parse(wellFormed);
- Element documentElement = null;
-
- if (document != null) {
- documentElement = document.getDocumentElement();
- }
-
- if (documentElement != null) {
- writer.write("[");
+ Element documentElement = null;
- NodeList nodeList = documentElement.getChildNodes();
- Node bodyNode = nodeList.item(nodeList.getLength() - 1);
- NodeList bodyChildren = bodyNode.getChildNodes();
- int bodyChildrenLength = bodyChildren.getLength();
+ if (document != null) {
+ documentElement = document.getDocumentElement();
+ }
+
+ if (documentElement != null) {
+ writer.write("[");
+
+ NodeList nodeList = documentElement.getChildNodes();
+ Node bodyNode = nodeList.item(nodeList.getLength() - 1);
+ NodeList bodyChildren = bodyNode.getChildNodes();
+ int bodyChildrenLength = bodyChildren.getLength();
- try {
- Transformer transformer;
+ try {
+ Transformer transformer;
- TransformerFactory factory = getTransformerFactory();
-
- synchronized (factory) {
- transformer = factory.newTransformer();
- }
+ TransformerFactory factory = getTransformerFactory();
+
+ synchronized (factory) {
+ transformer = factory.newTransformer();
+ }
- transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
- ContentHandler contentHandler = createContentHandler(writer);
- Result result = new SAXResult(contentHandler);
+ ContentHandler contentHandler = createContentHandler(writer);
+ Result result = new SAXResult(contentHandler);
- for (int i = 0; i < bodyChildrenLength; i++) {
- if (i != 0) {
- writer.write(", ");
+ for (int i = 0; i < bodyChildrenLength; i++) {
+ if (i != 0) {
+ writer.write(", ");
+ }
+ transformer.transform(new DOMSource(bodyChildren.item(i)), result);
}
- transformer.transform(new DOMSource(bodyChildren.item(i)), result);
+
+
+ } catch (TransformerException e) {
+ throw new IOException(e.getMessage());
}
- } catch (TransformerException e) {
- throw new IOException(e.getMessage());
+
+ writer.write("]");
+
+ wroteAnything = true;
+
}
-
- writer.write("]");
+ }
- } else {
+
+ if (!wroteAnything) {
writer.write(ScriptUtils.toScript(JSReference.NULL));
}
}
Show replies by date