| 1 |
package org.astrogrid.samp.xmlrpc.internal; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.BufferedWriter; |
| 5 |
import java.io.OutputStream; |
| 6 |
import java.io.OutputStreamWriter; |
| 7 |
import java.io.Writer; |
| 8 |
import java.util.Iterator; |
| 9 |
import java.util.List; |
| 10 |
import java.util.Map; |
| 11 |
|
| 12 |
/** |
| 13 |
* Utility class for writing XML. |
| 14 |
* |
| 15 |
* @author Mark Taylor |
| 16 |
* @since 26 Aug 2008 |
| 17 |
*/ |
| 18 |
class XmlWriter { |
| 19 |
private final Writer out_; |
| 20 |
private final int indent_; |
| 21 |
private int iLevel_; |
| 22 |
private static final String ENCODING = "UTF-8"; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor. |
| 26 |
* |
| 27 |
* @param out destination stream |
| 28 |
* @param indent number of spaces to indent each element level |
| 29 |
*/ |
| 30 |
public XmlWriter( OutputStream out, int indent ) throws IOException { |
| 31 |
out_ = new BufferedWriter( new OutputStreamWriter( out, ENCODING ) ); |
| 32 |
indent_ = indent; |
| 33 |
literal( "<?xml version='1.0' encoding='" + ENCODING + "'?>" ); |
| 34 |
newline(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Start an element. |
| 39 |
* |
| 40 |
* @param element tag name |
| 41 |
*/ |
| 42 |
public void start( String element ) throws IOException { |
| 43 |
pad( iLevel_++ ); |
| 44 |
out_.write( '<' ); |
| 45 |
out_.write( element ); |
| 46 |
out_.write( '>' ); |
| 47 |
newline(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* End an element. |
| 52 |
* |
| 53 |
* @param element tag name |
| 54 |
*/ |
| 55 |
public void end( String element ) throws IOException { |
| 56 |
pad( --iLevel_ ); |
| 57 |
out_.write( "</" ); |
| 58 |
out_.write( element ); |
| 59 |
out_.write( '>' ); |
| 60 |
newline(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Write an element and its text content. |
| 65 |
* |
| 66 |
* @param element tag name |
| 67 |
* @param content element text content |
| 68 |
*/ |
| 69 |
public void inline( String element, String content ) throws IOException { |
| 70 |
pad( iLevel_ ); |
| 71 |
out_.write( '<' ); |
| 72 |
out_.write( element ); |
| 73 |
out_.write( '>' ); |
| 74 |
text( content ); |
| 75 |
out_.write( "</" ); |
| 76 |
out_.write( element ); |
| 77 |
out_.write( '>' ); |
| 78 |
newline(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Writes text. Any escaping required for XML output will be |
| 83 |
* taken care of. |
| 84 |
* |
| 85 |
* @param txt text to output |
| 86 |
*/ |
| 87 |
public void text( String txt ) throws IOException { |
| 88 |
int leng = txt.length(); |
| 89 |
for ( int i = 0; i < leng; i++ ) { |
| 90 |
char c = txt.charAt( i ); |
| 91 |
switch ( c ) { |
| 92 |
case '&': |
| 93 |
out_.write( "&" ); |
| 94 |
break; |
| 95 |
case '<': |
| 96 |
out_.write( "<" ); |
| 97 |
break; |
| 98 |
case '>': |
| 99 |
out_.write( ">" ); |
| 100 |
break; |
| 101 |
default: |
| 102 |
out_.write( c ); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Writes text with no escaping of XML special characters etc. |
| 109 |
* |
| 110 |
* @param txt raw text to output |
| 111 |
*/ |
| 112 |
public void literal( String txt ) throws IOException { |
| 113 |
out_.write( txt ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Writes a new line character. |
| 118 |
*/ |
| 119 |
public void newline() throws IOException { |
| 120 |
out_.write( '\n' ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Writes a SAMP-friendly object in XML-RPC form. |
| 125 |
* |
| 126 |
* @param value object to serialize; must be a string, list or map |
| 127 |
*/ |
| 128 |
public void sampValue( Object value ) throws IOException { |
| 129 |
if ( value instanceof String ) { |
| 130 |
inline( "value", (String) value ); |
| 131 |
} |
| 132 |
else if ( value instanceof List ) { |
| 133 |
start( "value" ); |
| 134 |
start( "array" ); |
| 135 |
start( "data" ); |
| 136 |
for ( Iterator it = ((List) value).iterator(); it.hasNext(); ) { |
| 137 |
sampValue( it.next() ); |
| 138 |
} |
| 139 |
end( "data" ); |
| 140 |
end( "array" ); |
| 141 |
end( "value" ); |
| 142 |
} |
| 143 |
else if ( value instanceof Map ) { |
| 144 |
start( "value" ); |
| 145 |
start( "struct" ); |
| 146 |
for ( Iterator it = ((Map) value).entrySet().iterator(); |
| 147 |
it.hasNext(); ) { |
| 148 |
Map.Entry entry = (Map.Entry) it.next(); |
| 149 |
start( "member" ); |
| 150 |
inline( "name", entry.getKey().toString() ); |
| 151 |
sampValue( entry.getValue() ); |
| 152 |
end( "member" ); |
| 153 |
} |
| 154 |
end( "struct" ); |
| 155 |
end( "value" ); |
| 156 |
} |
| 157 |
else if ( value == null ) { |
| 158 |
throw new XmlRpcFormatException( "Null value not permitted" ); |
| 159 |
} |
| 160 |
else { |
| 161 |
throw new XmlRpcFormatException( "Unsupported object type " |
| 162 |
+ value.getClass().getName() ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Closes the stream. |
| 168 |
*/ |
| 169 |
public void close() throws IOException { |
| 170 |
out_.close(); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Outputs start-of-line padding for a given level of indentation. |
| 175 |
* |
| 176 |
* @param level level of XML element ancestry |
| 177 |
*/ |
| 178 |
private void pad( int level ) throws IOException { |
| 179 |
int npad = level * indent_; |
| 180 |
for ( int i = 0; i < npad; i++ ) { |
| 181 |
out_.write( ' ' ); |
| 182 |
} |
| 183 |
} |
| 184 |
} |