diff options
Diffstat (limited to 'java')
-rw-r--r-- | java/src/IceUtil/OutputBase.java | 40 | ||||
-rw-r--r-- | java/src/IceUtil/XMLOutput.java | 228 |
2 files changed, 179 insertions, 89 deletions
diff --git a/java/src/IceUtil/OutputBase.java b/java/src/IceUtil/OutputBase.java index e801ea3a1a7..beb87409879 100644 --- a/java/src/IceUtil/OutputBase.java +++ b/java/src/IceUtil/OutputBase.java @@ -18,9 +18,9 @@ public class OutputBase _out = null; _pos = 0; _indent = 0; - _separator = true; - _useTab = true; _indentSize = 4; + _useTab = true; + _separator = true; } public @@ -29,9 +29,9 @@ public class OutputBase _out = out; _pos = 0; _indent = 0; - _separator = true; - _useTab = true; _indentSize = 4; + _useTab = true; + _separator = true; } public @@ -40,14 +40,26 @@ public class OutputBase _out = null; _pos = 0; _indent = 0; - _separator = true; - _useTab = true; _indentSize = 4; + _useTab = true; + _separator = true; open(s); } public void + setIndent(int indentSize) + { + _indentSize = indentSize; + } + + public void + setUseTab(boolean useTab) + { + _useTab = useTab; + } + + public void open(String s) { try @@ -115,18 +127,6 @@ public class OutputBase } public void - setIndent(int indentSize) - { - _indentSize = indentSize; - } - - public void - setUseTab(boolean useTab) - { - _useTab = useTab; - } - - public void nl() { _out.println(); @@ -182,9 +182,9 @@ public class OutputBase protected java.io.PrintWriter _out; protected int _pos; protected int _indent; + protected int _indentSize; protected java.util.LinkedList _indentSave = new java.util.LinkedList(); + protected boolean _useTab; protected boolean _separator; - protected boolean _useTab; - protected int _indentSize; } diff --git a/java/src/IceUtil/XMLOutput.java b/java/src/IceUtil/XMLOutput.java index d6d46f5db98..baeb9d9f4d8 100644 --- a/java/src/IceUtil/XMLOutput.java +++ b/java/src/IceUtil/XMLOutput.java @@ -16,24 +16,30 @@ public class XMLOutput extends OutputBase XMLOutput() { super(); - _printed = true; + _se = false; + _text = false; _sgml = false; + _escape = false; } public XMLOutput(java.io.PrintWriter writer) { super(writer); - _printed = true; + _se = false; + _text = false; _sgml = false; + _escape = false; } public XMLOutput(String s) { super(s); - _printed = true; + _se = false; + _text = false; _sgml = false; + _escape = false; } public void @@ -45,79 +51,43 @@ public class XMLOutput extends OutputBase public void print(String s) { - if(!_printed) + if(_se) { _out.print('>'); - _printed = true; + _se = false; } - super.print(s); - } + _text = true; - public void - printEscaped(String s) - { - String v = s; - - // - // Find out whether there is a reserved character to avoid - // conversion if not necessary. - // - final String allReserved = "<>'\"&"; - boolean hasReserved = false; - char[] arr = s.toCharArray(); - for(int i = 0; i < arr.length; i++) + if(_escape) { - if(allReserved.indexOf(arr[i]) != -1) - { - hasReserved = true; - break; - } + String escaped = escape(s); + super.print(escaped); } - if(hasReserved) + else { - // - // First convert all & to & - // - if(v.indexOf('&') != -1) - { - v = v.replaceAll("&", "&"); - } - - // - // Next convert remaining reserved characters. - // - if(v.indexOf('>') != -1) - { - v = v.replaceAll(">", ">"); - } - if(v.indexOf('<') != -1) - { - v = v.replaceAll("<", "<"); - } - if(v.indexOf('\'') != -1) - { - v = v.replaceAll("'", "'"); - } - if(v.indexOf('"') != -1) - { - v = v.replaceAll("\"", """); - } + super.print(s); } - print(v); + } + + public XMLOutput + write(String s) + { + print(s); + return this; } public void nl() { - if(!_printed) + if(_se) { + _se = false; _out.print('>'); - _printed = true; } super.nl(); } - public void + public XMLOutput se(String element) { nl(); @@ -127,16 +97,18 @@ public class XMLOutput extends OutputBase // deferred until either the end-element (in which case a /> is // emitted) or until something is displayed. // - _out.print('<'); - _out.print(element); - if(_sgml) + if(_escape) { - _out.print('>'); + _out.print('<'); + _out.print(escape(element)); } else { - _printed = false; + _out.print('<'); + _out.print(element); } + _se = true; + _text = false; int pos = element.indexOf(' '); if(pos == -1) @@ -155,30 +127,148 @@ public class XMLOutput extends OutputBase ++_pos; // TODO: ??? inc(); _separator = false; + return this; } - public void + public XMLOutput ee() { String element = (String)_elementStack.removeFirst(); dec(); - if(!_printed) + if(_se) { - _out.print("/>"); + // + // SGML (docbook) doesn't support <foo/> + // + if(_sgml) + { + _out.print("><"); + _out.print(element); + _out.print(">"); + } + else + { + _out.print("/>"); + } } else { - nl(); + if(!_text) + { + nl(); + } _out.print("</"); _out.print(element); - _out.print('>'); + _out.print(">"); } --_pos; // TODO: ??? - _printed = true; + + _se = false; + _text = false; + return this; + } + + public XMLOutput + attr(String name, String value) + { + // + // Precondition: Attributes can only be attached to elements. + // + assert(_se); + _out.print(" "); + _out.print(name); + _out.print("=\""); + _out.print(escape(value)); + _out.print("\""); + return this; + } + + public XMLOutput + startEscapes() + { + _escape = true; + return this; + } + + public XMLOutput + endEscapes() + { + _escape = false; + return this; + } + + public String + currentElement() + { + if(_elementStack.size() > 0) + { + return (String)_elementStack.getFirst(); + } + else + { + return ""; + } + } + + private String + escape(String input) + { + String v = input; + + // + // Find out whether there is a reserved character to avoid + // conversion if not necessary. + // + final String allReserved = "<>'\"&"; + boolean hasReserved = false; + char[] arr = input.toCharArray(); + for(int i = 0; i < arr.length; i++) + { + if(allReserved.indexOf(arr[i]) != -1) + { + hasReserved = true; + break; + } + } + if(hasReserved) + { + // + // First convert all & to & + // + if(v.indexOf('&') != -1) + { + v = v.replaceAll("&", "&"); + } + + // + // Next convert remaining reserved characters. + // + if(v.indexOf('>') != -1) + { + v = v.replaceAll(">", ">"); + } + if(v.indexOf('<') != -1) + { + v = v.replaceAll("<", "<"); + } + if(v.indexOf('\'') != -1) + { + v = v.replaceAll("'", "'"); + } + if(v.indexOf('"') != -1) + { + v = v.replaceAll("\"", """); + } + } + return v; } private java.util.LinkedList _elementStack = new java.util.LinkedList(); - private boolean _printed; + + boolean _se; + boolean _text; + private boolean _sgml; + private boolean _escape; } |