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

View file

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

View file

@ -25,7 +25,7 @@ public class Presenter
/*
* 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);
}
@ -82,11 +82,11 @@ public class Presenter
{
result = new Attributes (target);
Iterator iterator = source.entrySet().iterator();
Iterator<Map.Entry<String, String>> iterator = source.entrySet().iterator();
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 ());
@ -109,10 +109,11 @@ public class Presenter
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, "");
}
@ -121,10 +122,7 @@ public class Presenter
/*
*
*/
static protected void processChildren (StringBuffer result,
Node node,
IdsDataById datas,
String suffix) throws Exception
static protected void processChildren (Writer result, Node node, IdsDataById datas, String suffix) throws Exception
{
// Get the iteration strategy.
IdData.IterationStrategy strategy;
@ -267,18 +265,10 @@ public class Presenter
* @param idAttr
* The ID.
*/
static protected StringBuffer processElementWithId (Node node,
NamedNodeMap attrs,
Node idAttr,
IdsDataById datas) throws Exception
static protected void processElementWithId (Writer result, Node node, NamedNodeMap attrs, Node idAttr, IdsDataById datas) throws Exception
{
StringBuffer result;
result = processElementWithId (node, attrs, idAttr, datas, "");
//
return (result);
processElementWithId (result, node, attrs, idAttr, datas, "");
}
@ -293,18 +283,11 @@ public class Presenter
* @param idAttr
* The ID.
*/
static protected StringBuffer processElementWithId (Node node,
NamedNodeMap attrs,
Node idAttr,
IdsDataById datas,
String suffix) throws Exception
static protected void processElementWithId (Writer result, Node node, NamedNodeMap attrs, Node idAttr, IdsDataById datas, String suffix) throws Exception
{
StringBuffer result;
result = new StringBuffer ();
String tag = node.getNodeName();
String idValue = idAttr.getNodeValue();
// String idValue = idAttr.getNodeValue();
logger.debug ("tag=" + tag);
@ -313,7 +296,7 @@ public class Presenter
if (dataCore == null)
{
result.append (Presenter.processElementBasically (node, datas, suffix));
Presenter.processElementBasically (result, node, datas, suffix);
}
else if (dataCore instanceof IdData)
{
@ -420,7 +403,7 @@ public class Presenter
// Manage a Hashmap.
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');
}
}
@ -432,7 +415,6 @@ public class Presenter
//
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, "");
}
@ -450,7 +432,7 @@ public class Presenter
* 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");
String TRANSITIONAL_DTD = "xhtml1-transitional.dtd";
@ -479,7 +461,7 @@ public class Presenter
if (dt != null)
{
String publicId = dt.getPublicId();
// String publicId = dt.getPublicId();
String systemId = dt.getSystemId();
if (systemId.equals(TRANSITIONAL_DTD))
@ -505,15 +487,11 @@ public class Presenter
if (idAttr != null)
{
result.append (Presenter.processElementWithId (node,
attrs,
idAttr,
datas,
suffix));
Presenter.processElementWithId (result, node, attrs, idAttr, datas, suffix);
}
else
{
result.append (Presenter.processElementBasically (node, datas, suffix));
Presenter.processElementBasically (result, node, datas, suffix);
}
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;
result = processElementBasically (node, datas, "");
//
return (result);
processElementBasically (result, node, datas, "");
}
/*
*
*/
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");
result = new StringBuffer ();
// Open the tag.
result.append ('<');
@ -605,7 +576,7 @@ public class Presenter
// Build the tag attributes.
Attributes tagAttributes;
//Attributes tagAttributes;
result.append (processAttributes (node.getAttributes (),
null,
@ -628,9 +599,6 @@ public class Presenter
}
logger.debug("processElementBasically - ended");
//
return (result);
}
@ -981,11 +949,11 @@ public class Presenter
// Put model attributes in the merged attributes list.
if (dataAttributes != null)
{
Iterator iterator = dataAttributes.entrySet().iterator();
Iterator<Map.Entry<String, String>> iterator = dataAttributes.entrySet().iterator();
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 ()))
{
@ -1006,10 +974,10 @@ public class Presenter
}
// Display the attributes
Iterator iterator = mergedAttributes.entrySet().iterator();
Iterator<Map.Entry<String, String>> iterator = mergedAttributes.entrySet().iterator();
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))
{

View file

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