Skip to content

Commit

Permalink
Implement Appendable
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelstaldal committed Aug 1, 2006
1 parent 50822f4 commit 66a453b
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 126 deletions.
2 changes: 2 additions & 0 deletions HISTORY
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,5 @@
-----
* Fixed formatting of float values.
* Fixed escaping in HTML serialization.
* New methods for convenience and efficiency in Serializer,
send a String/CharSequence directly without converting to char[].
180 changes: 118 additions & 62 deletions src/nu/staldal/xodus/HTMLSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,27 @@ public void startElement(String namespaceURI, String localName,


private void fixTag()
throws SAXException
throws SAXException
{
try {
if (emptyElement)
{
out.write('>');
emptyElement = false;
}
_fixTag();
}
catch (IOException e)
{
throw new SAXException(e);
}
}


private void _fixTag()
throws IOException
{
if (emptyElement)
{
out.write('>');
emptyElement = false;
}
}


public void endElement(String namespaceURI, String localName, String qName)
Expand Down Expand Up @@ -374,47 +381,72 @@ public void endElement(String namespaceURI, String localName, String qName)
wasEndTag = true;
}


public void characters(CharSequence cs)
throws SAXException
private void doFirstInCharacters()
throws SAXException
{
wasEndTag = false;
wasStartTag = false;

fixTag();
}

private void _doFirstInCharacters()
throws IOException
{
wasEndTag = false;
wasStartTag = false;

_fixTag();
}

fixTag();
private void outputEscapedCharacter(char c)
throws IOException
{
switch (c)
{
case '<':
out.write("&lt;");
break;

case '>':
out.write("&gt;");
break;

try {
if (disableOutputEscaping || nestedCDATA > 0
|| inNotEscapeElement > 0)
{
out.append(cs);
}
else
{
out.enableEscaping();
for (int i = 0; i<cs.length(); i++)
{
char c = cs.charAt(i);
switch (c)
{
case '<':
out.write("&lt;");
break;
case '&':
out.write("&amp;");
break;

default:
out.write(c);
}
}

case '>':
out.write("&gt;");
break;

case '&':
out.write("&amp;");
break;

default:
out.write(c);
}
}
out.disableEscaping();
private void outputCharacters(CharSequence cs, int start, int end)
throws IOException
{
if (disableOutputEscaping || nestedCDATA > 0
|| inNotEscapeElement > 0)
{
out.append(cs, start, end);
}
else
{
out.enableEscaping();
for (int i = start; i<end; i++)
{
outputEscapedCharacter(cs.charAt(i));
}
out.disableEscaping();
}
}

public void characters(CharSequence cs)
throws SAXException
{
doFirstInCharacters();

try {
outputCharacters(cs, 0, cs.length());
}
catch (IOException e)
{
Expand All @@ -426,10 +458,7 @@ public void characters(CharSequence cs)
public void characters(char ch[], int start, int length)
throws SAXException
{
wasEndTag = false;
wasStartTag = false;

fixTag();
doFirstInCharacters();

try {
if (disableOutputEscaping || nestedCDATA > 0
Expand All @@ -442,24 +471,7 @@ public void characters(char ch[], int start, int length)
out.enableEscaping();
for (int i = start; i<start+length; i++)
{
char c = ch[i];
switch (c)
{
case '<':
out.write("&lt;");
break;

case '>':
out.write("&gt;");
break;

case '&':
out.write("&amp;");
break;

default:
out.write(c);
}
outputEscapedCharacter(ch[i]);
}
out.disableEscaping();
}
Expand Down Expand Up @@ -941,6 +953,50 @@ public void externalEntityDecl(String name, String publicId,
throw new SAXException(e);
}
}


// Appendable

public Appendable append(CharSequence cs)
throws IOException
{
_doFirstInCharacters();

outputCharacters(cs, 0, cs.length());

return this;
}


public Appendable append(char c)
throws IOException
{
_doFirstInCharacters();

if (disableOutputEscaping || nestedCDATA > 0)
{
out.append(c);
}
else
{
out.enableEscaping();
outputEscapedCharacter(c);
out.disableEscaping();
}

return this;
}


public Appendable append(CharSequence cs, int start, int end)
throws IOException
{
_doFirstInCharacters();

outputCharacters(cs, start, end);

return this;
}

}

5 changes: 4 additions & 1 deletion src/nu/staldal/xodus/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@
* and
* <code>javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING</code>
* can be used as processingInstruction targets to disable output escaping.
*<p>
* The methods specified in the {@link java.lang.Appendable} interface will
* act like the {@link #characters} methods.
*/
public abstract class Serializer implements ContentHandler, LexicalHandler,
DTDHandler, DeclHandler
DTDHandler, DeclHandler, Appendable
{
protected final OutputConfig outputConfig;
protected final String systemId;
Expand Down
27 changes: 26 additions & 1 deletion src/nu/staldal/xodus/TextSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,31 @@ public void externalEntityDecl(String name, String publicId,
throws SAXException
{
}


// Appendable

}
public Appendable append(CharSequence cs)
throws IOException
{
out.append(cs);
return this;
}


public Appendable append(char c)
throws IOException
{
out.append(c);
return this;
}


public Appendable append(CharSequence cs, int start, int end)
throws IOException
{
out.append(cs, start, end);
return this;
}

}
Loading

0 comments on commit 66a453b

Please sign in to comment.