Replace StringBuffer out by Writer.

This commit is contained in:
Christian P. MOMON 2010-03-26 12:51:34 +01:00
parent ce92d363f4
commit cf6c2c775a
8 changed files with 39 additions and 65 deletions

View file

@ -1,5 +1,7 @@
package fr.devinsy.xid; package fr.devinsy.xid;
import java.io.StringWriter;
import org.w3c.dom.*; import org.w3c.dom.*;
/** /**
@ -116,8 +118,9 @@ public class DomPresenter extends Presenter
else else
{ {
// Build the web page. // Build the web page.
result = new StringBuffer (); StringWriter htmlCode = new StringWriter();
Presenter.doXid (result, this.doc, datas); Presenter.doXid (htmlCode, this.doc, datas);
result = new StringBuffer (htmlCode.toString());
} }
// //
@ -132,8 +135,9 @@ public class DomPresenter extends Presenter
{ {
StringBuffer result; StringBuffer result;
result = new StringBuffer(); StringWriter htmlCode = new StringWriter();
Presenter.process (result, doc, datas); Presenter.process (htmlCode, doc, datas);
result = htmlCode.getBuffer();
// //
return (result); return (result);

View file

@ -113,8 +113,9 @@ public class FilePresenter extends DomPresenter
} }
// Build the web page. // Build the web page.
result = new StringBuffer (Presenter.estimatedTargetLength(source.length())); StringWriter htmlCode = new StringWriter(Presenter.estimatedTargetLength(source.length()));
Presenter.doXid (result, doc, datas); Presenter.doXid (htmlCode, doc, datas);
result = htmlCode.getBuffer();
// //
return (result); return (result);

View file

@ -25,7 +25,7 @@ public class Presenter
/* /*
* Xid a file with data. * Xid a file with data.
*/ */
static public void doXid (StringBuffer result, Document doc, IdsDataById datas) throws Exception static public void doXid (Writer result, Document doc, IdsDataById datas) throws Exception
{ {
Presenter.process (result, doc, datas); Presenter.process (result, doc, datas);
} }
@ -82,11 +82,11 @@ public class Presenter
{ {
result = new Attributes (target); result = new Attributes (target);
Iterator iterator = source.entrySet().iterator(); Iterator<Map.Entry<String, String>> iterator = source.entrySet().iterator();
while (iterator.hasNext()) while (iterator.hasNext())
{ {
Map.Entry<String, String> attribute = (Map.Entry<String, String>) iterator.next(); Map.Entry<String, String> attribute = iterator.next();
String currentValue = target.get (attribute.getKey ()); String currentValue = target.get (attribute.getKey ());
@ -109,10 +109,11 @@ public class Presenter
return (result); return (result);
} }
/* /*
* *
*/ */
static protected void processChildren (StringBuffer result, Node node, IdsDataById datas) throws Exception static protected void processChildren (Writer result, Node node, IdsDataById datas) throws Exception
{ {
processChildren (result, node, datas, ""); processChildren (result, node, datas, "");
} }
@ -121,10 +122,7 @@ public class Presenter
/* /*
* *
*/ */
static protected void processChildren (StringBuffer result, static protected void processChildren (Writer result, Node node, IdsDataById datas, String suffix) throws Exception
Node node,
IdsDataById datas,
String suffix) throws Exception
{ {
// Get the iteration strategy. // Get the iteration strategy.
IdData.IterationStrategy strategy; IdData.IterationStrategy strategy;
@ -267,18 +265,10 @@ public class Presenter
* @param idAttr * @param idAttr
* The ID. * The ID.
*/ */
static protected StringBuffer processElementWithId (Node node, static protected void processElementWithId (Writer result, Node node, NamedNodeMap attrs, Node idAttr, IdsDataById datas) throws Exception
NamedNodeMap attrs,
Node idAttr,
IdsDataById datas) throws Exception
{ {
StringBuffer result; processElementWithId (result, node, attrs, idAttr, datas, "");
result = processElementWithId (node, attrs, idAttr, datas, "");
//
return (result);
} }
@ -293,18 +283,11 @@ public class Presenter
* @param idAttr * @param idAttr
* The ID. * The ID.
*/ */
static protected StringBuffer processElementWithId (Node node, static protected void processElementWithId (Writer result, Node node, NamedNodeMap attrs, Node idAttr, IdsDataById datas, String suffix) throws Exception
NamedNodeMap attrs,
Node idAttr,
IdsDataById datas,
String suffix) throws Exception
{ {
StringBuffer result;
result = new StringBuffer ();
String tag = node.getNodeName(); String tag = node.getNodeName();
String idValue = idAttr.getNodeValue(); // String idValue = idAttr.getNodeValue();
logger.debug ("tag=" + tag); logger.debug ("tag=" + tag);
@ -313,7 +296,7 @@ public class Presenter
if (dataCore == null) if (dataCore == null)
{ {
result.append (Presenter.processElementBasically (node, datas, suffix)); Presenter.processElementBasically (result, node, datas, suffix);
} }
else if (dataCore instanceof IdData) else if (dataCore instanceof IdData)
{ {
@ -420,7 +403,7 @@ public class Presenter
// Manage a Hashmap. // Manage a Hashmap.
IdsDataById data = (IdsDataById) tags.elementAt (nLine); IdsDataById data = (IdsDataById) tags.elementAt (nLine);
result.append (Presenter.processElementWithId (node, attrs, idAttr, data, Integer.toString (nLine))); Presenter.processElementWithId (result, node, attrs, idAttr, data, Integer.toString (nLine));
result.append ('\n'); result.append ('\n');
} }
} }
@ -432,7 +415,6 @@ public class Presenter
// //
logger.debug ("Exit"); logger.debug ("Exit");
return (result);
} }
@ -440,7 +422,7 @@ public class Presenter
/** /**
* *
*/ */
static protected void process (StringBuffer result, Node node, IdsDataById datas) throws Exception static protected void process (Writer result, Node node, IdsDataById datas) throws Exception
{ {
Presenter.process (result, node, datas, ""); Presenter.process (result, node, datas, "");
} }
@ -450,7 +432,7 @@ public class Presenter
* Recursive method that processes a node and any child nodes. * Recursive method that processes a node and any child nodes.
* *
*/ */
static protected void process (StringBuffer result, Node node, IdsDataById datas, String suffix) throws Exception static protected void process (Writer result, Node node, IdsDataById datas, String suffix) throws Exception
{ {
logger.debug ("process - started"); logger.debug ("process - started");
String TRANSITIONAL_DTD = "xhtml1-transitional.dtd"; String TRANSITIONAL_DTD = "xhtml1-transitional.dtd";
@ -479,7 +461,7 @@ public class Presenter
if (dt != null) if (dt != null)
{ {
String publicId = dt.getPublicId(); // String publicId = dt.getPublicId();
String systemId = dt.getSystemId(); String systemId = dt.getSystemId();
if (systemId.equals(TRANSITIONAL_DTD)) if (systemId.equals(TRANSITIONAL_DTD))
@ -505,15 +487,11 @@ public class Presenter
if (idAttr != null) if (idAttr != null)
{ {
result.append (Presenter.processElementWithId (node, Presenter.processElementWithId (result, node, attrs, idAttr, datas, suffix);
attrs,
idAttr,
datas,
suffix));
} }
else else
{ {
result.append (Presenter.processElementBasically (node, datas, suffix)); Presenter.processElementBasically (result, node, datas, suffix);
} }
break; break;
@ -579,25 +557,18 @@ public class Presenter
/* /*
* *
*/ */
static StringBuffer processElementBasically (Node node, IdsDataById datas) throws Exception static void processElementBasically (Writer result, Node node, IdsDataById datas) throws Exception
{ {
StringBuffer result; processElementBasically (result, node, datas, "");
result = processElementBasically (node, datas, "");
//
return (result);
} }
/* /*
* *
*/ */
static StringBuffer processElementBasically (Node node, IdsDataById datas, String suffix) throws Exception static void processElementBasically (Writer result, Node node, IdsDataById datas, String suffix) throws Exception
{ {
StringBuffer result;
logger.debug("processElementBasically - started"); logger.debug("processElementBasically - started");
result = new StringBuffer ();
// Open the tag. // Open the tag.
result.append ('<'); result.append ('<');
@ -605,7 +576,7 @@ public class Presenter
// Build the tag attributes. // Build the tag attributes.
Attributes tagAttributes; //Attributes tagAttributes;
result.append (processAttributes (node.getAttributes (), result.append (processAttributes (node.getAttributes (),
null, null,
@ -628,9 +599,6 @@ public class Presenter
} }
logger.debug("processElementBasically - ended"); logger.debug("processElementBasically - ended");
//
return (result);
} }
@ -981,11 +949,11 @@ public class Presenter
// Put model attributes in the merged attributes list. // Put model attributes in the merged attributes list.
if (dataAttributes != null) if (dataAttributes != null)
{ {
Iterator iterator = dataAttributes.entrySet().iterator(); Iterator<Map.Entry<String, String>> iterator = dataAttributes.entrySet().iterator();
while (iterator.hasNext()) while (iterator.hasNext())
{ {
Map.Entry<String, String> attribute = (Map.Entry<String, String>) iterator.next(); Map.Entry<String, String> attribute = iterator.next();
if (mergedAttributes.containsKey (attribute.getKey ())) if (mergedAttributes.containsKey (attribute.getKey ()))
{ {
@ -1006,10 +974,10 @@ public class Presenter
} }
// Display the attributes // Display the attributes
Iterator iterator = mergedAttributes.entrySet().iterator(); Iterator<Map.Entry<String, String>> iterator = mergedAttributes.entrySet().iterator();
while (iterator.hasNext ()) while (iterator.hasNext ())
{ {
Map.Entry<String, String> attribute = (Map.Entry<String, String>) iterator.next(); Map.Entry<String, String> attribute = iterator.next();
if ((attribute.getKey ().equals ("id")) && (suffix.length () != 0)) if ((attribute.getKey ().equals ("id")) && (suffix.length () != 0))
{ {

View file

@ -98,8 +98,9 @@ public class StringPresenter extends DomPresenter
doc = buildTree (new ByteArrayInputStream (htmlSource.getBytes ())); doc = buildTree (new ByteArrayInputStream (htmlSource.getBytes ()));
} }
StringBuffer htmlTarget = new StringBuffer (Presenter.estimatedTargetLength(this.html.length())); StringWriter htmlCode = new StringWriter(Presenter.estimatedTargetLength(this.html.length()));
Presenter.doXid (htmlTarget, doc, datas); Presenter.doXid (htmlCode, doc, datas);
StringBuffer htmlTarget = htmlCode.getBuffer();
if (htmlTarget == null) if (htmlTarget == null)
{ {