diff --git a/build/classes/fr/devinsy/xid/DomPresenter.class b/build/classes/fr/devinsy/xid/DomPresenter.class
index 0a5a7e0..b1eb8b0 100644
Binary files a/build/classes/fr/devinsy/xid/DomPresenter.class and b/build/classes/fr/devinsy/xid/DomPresenter.class differ
diff --git a/build/classes/fr/devinsy/xid/FilePresenter.class b/build/classes/fr/devinsy/xid/FilePresenter.class
index 2ae52cc..0e555c5 100644
Binary files a/build/classes/fr/devinsy/xid/FilePresenter.class and b/build/classes/fr/devinsy/xid/FilePresenter.class differ
diff --git a/build/classes/fr/devinsy/xid/Presenter.class b/build/classes/fr/devinsy/xid/Presenter.class
index 5108013..ff98787 100644
Binary files a/build/classes/fr/devinsy/xid/Presenter.class and b/build/classes/fr/devinsy/xid/Presenter.class differ
diff --git a/build/classes/fr/devinsy/xid/StringPresenter.class b/build/classes/fr/devinsy/xid/StringPresenter.class
index 1a672c7..8e160a5 100644
Binary files a/build/classes/fr/devinsy/xid/StringPresenter.class and b/build/classes/fr/devinsy/xid/StringPresenter.class differ
diff --git a/demo/XidDemo.java b/demo/XidDemo.java
index 43f795c..ab72782 100644
--- a/demo/XidDemo.java
+++ b/demo/XidDemo.java
@@ -44,22 +44,24 @@ class XidDemo
datas = new Data ();
datas.setContent ("name", "Superman");
- StringBuffer html;
+ String htmlSource = "
a name
";
+ StringBuffer htmlTarget;
try
{
- html = StringPresenter.doXid ("a name
", datas);
+ htmlTarget = StringPresenter.doXid (htmlSource, datas);
}
catch (Exception exception)
{
System.out.println(exception.getMessage());
- html = null;
+ htmlTarget = null;
}
System.out.println ("datas = new Data ();");
System.out.println ("datas.setContent (\"name\", \"Superman\");");
System.out.println ("+");
System.out.println ("a name
");
System.out.println ("=>");
- System.out.println (html);
+ System.out.println (htmlTarget);
+ System.out.println ("[" + htmlSource.length() + "] => [" + htmlTarget.length() + "]");
}
@@ -73,15 +75,16 @@ class XidDemo
datas.appendAttribute ("lastname", "style", "foreground: red;");
datas.setAttribute ("lastname", "class", "nameClass");
- StringBuffer html;
+ String htmlSource = "a last name
";
+ StringBuffer htmlTarget;
try
{
- html = StringPresenter.doXid ("a last name
", datas);
+ htmlTarget = StringPresenter.doXid (htmlSource, datas);
}
catch (Exception exception)
{
System.out.println(exception.getMessage());
- html = null;
+ htmlTarget = null;
}
System.out.println ("datas = new Data ();");
@@ -92,7 +95,8 @@ class XidDemo
System.out.println ("+");
System.out.println ("a last name
");
System.out.println ("=>");
- System.out.println (html);
+ System.out.println (htmlTarget);
+ System.out.println ("[" + htmlSource.length() + "] => [" + htmlTarget.length() + "]");
}
@@ -108,15 +112,16 @@ class XidDemo
datas.setContent ("words", 4, "echo");
datas.setContent ("words", 5, "fox");
- StringBuffer html;
+ String htmlSource = "";
+ StringBuffer htmlTarget;
try
{
- html = StringPresenter.doXid ("", datas);
+ htmlTarget = StringPresenter.doXid (htmlSource, datas);
}
catch (Exception exception)
{
System.out.println(exception.getMessage());
- html = null;
+ htmlTarget = null;
}
System.out.println ("datas = new Data ();");
@@ -131,7 +136,8 @@ class XidDemo
System.out.println (" a word");
System.out.println ("");
System.out.println ("=>");
- System.out.println (html);
+ System.out.println (htmlTarget);
+ System.out.println ("[" + htmlSource.length() + "] => [" + htmlTarget.length() + "]");
}
@@ -154,15 +160,15 @@ class XidDemo
source.append ("");
String htmlSource = source.toString ();
- StringBuffer html;
+ StringBuffer htmlTarget;
try
{
- html = StringPresenter.doXid (htmlSource, datas);
+ htmlTarget = StringPresenter.doXid (htmlSource, datas);
}
catch (Exception exception)
{
System.out.println(exception.getMessage());
- html = null;
+ htmlTarget = null;
}
System.out.println ("datas = new Data ();");
@@ -176,7 +182,8 @@ class XidDemo
System.out.println ("+");
System.out.println (htmlSource);
System.out.println ("=>");
- System.out.println (html);
+ System.out.println (htmlTarget);
+ System.out.println ("[" + htmlSource.length() + "] => [" + htmlTarget.length() + "]");
}
diff --git a/src/fr/devinsy/xid/DomPresenter.java b/src/fr/devinsy/xid/DomPresenter.java
index cd678a4..a79d0da 100644
--- a/src/fr/devinsy/xid/DomPresenter.java
+++ b/src/fr/devinsy/xid/DomPresenter.java
@@ -116,7 +116,8 @@ public class DomPresenter extends Presenter
else
{
// Build the web page.
- result = Presenter.doXid (this.doc, datas);
+ result = new StringBuffer ();
+ Presenter.doXid (result, this.doc, datas);
}
//
@@ -127,11 +128,12 @@ public class DomPresenter extends Presenter
/*
* Xid a file with data.
*/
- static public StringBuffer doXid (Document doc, IdsDataById datas, String webappPath) throws Exception
+ static public StringBuffer doXid (Document doc, IdsDataById datas) throws Exception
{
StringBuffer result;
- result = Presenter.process (doc, datas, webappPath);
+ result = new StringBuffer();
+ Presenter.process (result, doc, datas);
//
return (result);
diff --git a/src/fr/devinsy/xid/FilePresenter.java b/src/fr/devinsy/xid/FilePresenter.java
index a3e4eda..9bf879b 100644
--- a/src/fr/devinsy/xid/FilePresenter.java
+++ b/src/fr/devinsy/xid/FilePresenter.java
@@ -113,7 +113,8 @@ public class FilePresenter extends DomPresenter
}
// Build the web page.
- result = Presenter.doXid (doc, datas);
+ result = new StringBuffer (Presenter.estimatedTargetLength(source.length()));
+ Presenter.doXid (result, doc, datas);
//
return (result);
@@ -123,32 +124,7 @@ public class FilePresenter extends DomPresenter
/*
* Xid a file without data.
*/
- static public StringBuffer doXid (String fileName) throws Exception
- {
- StringBuffer result;
-
- Document doc = Presenter.fileToTree (fileName);
-
- if (doc == null)
- {
- result = null;
- }
- else
- {
- Presenter.addMetaTag (doc, "generator", "XID 0.0");
-
- result = Presenter.doXid (doc, null);
- }
-
- //
- return (result);
- }
-
-
- /*
- * Xid a file without data.
- */
- static public StringBuffer doXid2 (String filePathname) throws Exception
+ static public StringBuffer doXid (String filePathname) throws Exception
{
StringBuffer result;
diff --git a/src/fr/devinsy/xid/Presenter.java b/src/fr/devinsy/xid/Presenter.java
index c12bf52..19c24e6 100644
--- a/src/fr/devinsy/xid/Presenter.java
+++ b/src/fr/devinsy/xid/Presenter.java
@@ -25,14 +25,9 @@ public class Presenter
/*
* Xid a file with data.
*/
- static public StringBuffer doXid (Document doc, IdsDataById datas) throws Exception
+ static public void doXid (StringBuffer result, Document doc, IdsDataById datas) throws Exception
{
- StringBuffer result;
-
- result = Presenter.process (doc, datas);
-
- //
- return (result);
+ Presenter.process (result, doc, datas);
}
@@ -117,27 +112,20 @@ public class Presenter
/*
*
*/
- static protected StringBuffer processChildren (Node node, IdsDataById datas) throws Exception
+ static protected void processChildren (StringBuffer result, Node node, IdsDataById datas) throws Exception
{
- StringBuffer result;
-
- result = processChildren (node, datas, "");
-
- //
- return (result);
+ processChildren (result, node, datas, "");
}
/*
*
*/
- static protected StringBuffer processChildren (Node node,
+ static protected void processChildren (StringBuffer result,
+ Node node,
IdsDataById datas,
String suffix) throws Exception
{
- StringBuffer result;
- result = new StringBuffer ();
-
// Get the iteration strategy.
IdData.IterationStrategy strategy;
@@ -189,12 +177,12 @@ public class Presenter
lineCounter += 1;
if (lineCounter == 1)
{
- result.append (process (children.item (childIndex), datas, suffix));
+ process (result, children.item (childIndex), datas, suffix);
}
}
else
{
- result.append (process (children.item (childIndex), datas, suffix));
+ process (result, children.item (childIndex), datas, suffix);
}
}
break;
@@ -209,12 +197,12 @@ public class Presenter
if ((lineCounter == 1) || (lineCounter == 2))
{
- result.append (process (children.item (childIndex), datas, suffix));
+ process (result, children.item (childIndex), datas, suffix);
}
}
else
{
- result.append (process (children.item (childIndex), datas, suffix));
+ process (result, children.item (childIndex), datas, suffix);
}
}
break;
@@ -229,12 +217,12 @@ public class Presenter
if ((attrs2 != null) &&
(attrs2.getNamedItem ("id") != null))
{
- result.append (process (children.item (childIndex), datas, suffix));
+ process (result, children.item (childIndex), datas, suffix);
}
}
else
{
- result.append (process (children.item (childIndex), datas, suffix));
+ process (result, children.item (childIndex), datas, suffix);
}
}
break;
@@ -248,12 +236,12 @@ public class Presenter
if ((attrs2 == null) ||
(attrs2.getNamedItem ("id") == null))
{
- result.append (process (children.item(childIndex), datas, suffix));
+ process (result, children.item(childIndex), datas, suffix);
}
}
else
{
- result.append (process (children.item (childIndex), datas, suffix));
+ process (result, children.item (childIndex), datas, suffix);
}
}
break;
@@ -261,13 +249,10 @@ public class Presenter
case ALL_ROWS:
for (int childIndex = 0; childIndex < childrenCount; childIndex++)
{
- result.append (process (children.item(childIndex), datas, suffix));
+ process (result, children.item(childIndex), datas, suffix);
}
break;
}
-
- //
- return (result);
}
@@ -370,7 +355,7 @@ public class Presenter
if ((data == null) ||
(data.display () == null))
{
- result.append (processChildren (node, datas, suffix));
+ processChildren (result, node, datas, suffix);
}
else
{
@@ -417,7 +402,7 @@ public class Presenter
// Insert data.
if ((data == null) || (data.display () == null))
{
- result.append (processChildren (node, datas, suffix));
+ processChildren (result, node, datas, suffix);
}
else
{
@@ -455,14 +440,9 @@ public class Presenter
/**
*
*/
- static protected StringBuffer process (Node node, IdsDataById datas) throws Exception
+ static protected void process (StringBuffer result, Node node, IdsDataById datas) throws Exception
{
- StringBuffer result;
-
- result = Presenter.process (node, datas, "");
-
- //
- return (result);
+ Presenter.process (result, node, datas, "");
}
@@ -470,16 +450,13 @@ public class Presenter
* Recursive method that processes a node and any child nodes.
*
*/
- static protected StringBuffer process (Node node, IdsDataById datas, String suffix) throws Exception
+ static protected void process (StringBuffer result, Node node, IdsDataById datas, String suffix) throws Exception
{
logger.debug ("process - started");
String TRANSITIONAL_DTD = "xhtml1-transitional.dtd";
String TRANSITIONAL_DOCTYPE = "\n";
- StringBuffer result;
- result = new StringBuffer ();
-
// Is there anything to do?
if (node != null)
{
@@ -514,7 +491,7 @@ public class Presenter
// Log.write(Log.TRACE,"systemId = " + systemId);
}
- result.append (Presenter.process (((Document) node).getDocumentElement(), datas, suffix));
+ Presenter.process (result, ((Document) node).getDocumentElement(), datas, suffix);
break;
}
@@ -596,7 +573,6 @@ public class Presenter
//
//logger.info ("result=" + result);
logger.debug ("process - ended");
- return (result);
}
@@ -644,7 +620,7 @@ public class Presenter
{
result.append('>');
- result.append (processChildren (node, datas, suffix));
+ processChildren (result, node, datas, suffix);
result.append("");
result.append(node.getNodeName());
@@ -1101,4 +1077,33 @@ public class Presenter
//
return (result);
}
+
+
+ /**
+ * Good estimation of the target length able to optimize performance.
+ */
+ static int estimatedTargetLength(long sourceLength)
+ {
+ int result;
+
+ if (sourceLength < 1000)
+ {
+ result = (int) (sourceLength*5);
+ }
+ else if (sourceLength < 50000)
+ {
+ result = (int) (sourceLength*2);
+ }
+ else if (sourceLength < 800000)
+ {
+ result = (int) (100000);
+ }
+ else
+ {
+ result = (int) (sourceLength + 30000);
+ }
+
+ //
+ return(result);
+ }
}
diff --git a/src/fr/devinsy/xid/StringPresenter.java b/src/fr/devinsy/xid/StringPresenter.java
index 79ded2c..8091f2d 100644
--- a/src/fr/devinsy/xid/StringPresenter.java
+++ b/src/fr/devinsy/xid/StringPresenter.java
@@ -98,8 +98,8 @@ public class StringPresenter extends DomPresenter
doc = buildTree (new ByteArrayInputStream (htmlSource.getBytes ()));
}
- StringBuffer htmlTarget;
- htmlTarget = Presenter.doXid (doc, datas);
+ StringBuffer htmlTarget = new StringBuffer (Presenter.estimatedTargetLength(this.html.length()));
+ Presenter.doXid (htmlTarget, doc, datas);
if (htmlTarget == null)
{