| 1 |
package org.astrogrid.samp.xmlrpc.internal; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.HashMap; |
| 5 |
import java.util.List; |
| 6 |
import java.util.Map; |
| 7 |
import java.util.logging.Logger; |
| 8 |
import javax.xml.parsers.DocumentBuilder; |
| 9 |
import javax.xml.parsers.DocumentBuilderFactory; |
| 10 |
import javax.xml.parsers.ParserConfigurationException; |
| 11 |
import org.w3c.dom.Element; |
| 12 |
import org.w3c.dom.Node; |
| 13 |
import org.w3c.dom.NodeList; |
| 14 |
import org.w3c.dom.Text; |
| 15 |
|
| 16 |
/** |
| 17 |
* Utilities for XML manipulations required by SAMP/XML-RPC. |
| 18 |
* |
| 19 |
* @author Mark Taylor |
| 20 |
* @since 26 Aug 2008 |
| 21 |
*/ |
| 22 |
public class XmlUtils { |
| 23 |
|
| 24 |
private static Logger logger_ = |
| 25 |
Logger.getLogger( XmlUtils.class.getName() ); |
| 26 |
private static DocumentBuilderFactory dbFact_; |
| 27 |
|
| 28 |
/** |
| 29 |
* Private constructor prevents instantiation. |
| 30 |
*/ |
| 31 |
private XmlUtils() { |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns an array of all the Element children of a DOM node. |
| 36 |
* |
| 37 |
* @param parent parent node |
| 38 |
* @return children array |
| 39 |
*/ |
| 40 |
public static Element[] getChildren( Node parent ) { |
| 41 |
NodeList nodeList = parent.getChildNodes(); |
| 42 |
int nnode = nodeList.getLength(); |
| 43 |
List elList = new ArrayList( nnode ); |
| 44 |
for ( int i = 0; i < nnode; i++ ) { |
| 45 |
Node node = nodeList.item( i ); |
| 46 |
if ( node instanceof Element ) { |
| 47 |
elList.add( (Element) node ); |
| 48 |
} |
| 49 |
} |
| 50 |
return (Element[]) elList.toArray( new Element[ 0 ] ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the single element child of a DOM node. |
| 55 |
* |
| 56 |
* @param parent parent node |
| 57 |
* @return sole child element |
| 58 |
* @throws XmlRpcFormatException if there is not exactly one child |
| 59 |
* per element |
| 60 |
*/ |
| 61 |
public static Element getChild( Node parent ) throws XmlRpcFormatException { |
| 62 |
Element[] els = getChildren( parent ); |
| 63 |
if ( els.length == 1 ) { |
| 64 |
return els[ 0 ]; |
| 65 |
} |
| 66 |
else if ( els.length == 0 ) { |
| 67 |
throw new XmlRpcFormatException( "No child element of " |
| 68 |
+ ((Element) parent).getTagName() ); |
| 69 |
} |
| 70 |
else { |
| 71 |
throw new XmlRpcFormatException( "Multiple children of " |
| 72 |
+ ((Element) parent).getTagName() ); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the single child element of a DOM node, which has a given |
| 78 |
* known name. |
| 79 |
* |
| 80 |
* @param parent parent node |
| 81 |
* @param tagName child node name |
| 82 |
* @return sole child element with name <code>tagName</code> |
| 83 |
* @throws XmlRpcFormatException if there is not exactly one child |
| 84 |
* element or if it does not have name <code>tagName</code> |
| 85 |
*/ |
| 86 |
public static Element getChild( Node parent, String tagName ) |
| 87 |
throws XmlRpcFormatException { |
| 88 |
Element child = getChild( parent ); |
| 89 |
if ( ! tagName.equals( child.getTagName() ) ) { |
| 90 |
throw new XmlRpcFormatException( "Unexpected child of " |
| 91 |
+ ((Element) parent).getTagName() |
| 92 |
+ ": " + child.getTagName() |
| 93 |
+ " is not " + tagName ); |
| 94 |
} |
| 95 |
return child; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns the text content of an element as a string. |
| 100 |
* |
| 101 |
* @param el parent node |
| 102 |
* @return text content |
| 103 |
* @throws XmlRpcFormatException if content is not just text |
| 104 |
*/ |
| 105 |
public static String getTextContent( Element el ) throws |
| 106 |
XmlRpcFormatException { |
| 107 |
StringBuffer sbuf = new StringBuffer(); |
| 108 |
for ( Node node = el.getFirstChild(); node != null; |
| 109 |
node = node.getNextSibling() ) { |
| 110 |
if ( node instanceof Text ) { |
| 111 |
sbuf.append( ((Text) node).getData() ); |
| 112 |
} |
| 113 |
else if ( node instanceof Element ) { |
| 114 |
throw new XmlRpcFormatException( "Unexpected node " + node |
| 115 |
+ " in " + el.getTagName() |
| 116 |
+ " content" ); |
| 117 |
} |
| 118 |
} |
| 119 |
return sbuf.toString(); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns the content of a DOM element representing a <code>value</code> |
| 124 |
* element of an XML-RPC document. |
| 125 |
* Note that some content which would be legal in XML-RPC, but is not |
| 126 |
* legal in SAMP, may result in an exception. |
| 127 |
* |
| 128 |
* @param valueEl value element |
| 129 |
* @return SAMP-friendly object (string, list or map) |
| 130 |
*/ |
| 131 |
public static Object parseSampValue( Element valueEl ) |
| 132 |
throws XmlRpcFormatException { |
| 133 |
if ( getChildren( valueEl ).length == 0 ) { |
| 134 |
return getTextContent( valueEl ); |
| 135 |
} |
| 136 |
Element el = getChild( valueEl ); |
| 137 |
String name = el.getTagName(); |
| 138 |
if ( "array".equals( name ) ) { |
| 139 |
Element[] valueEls = getChildren( getChild( el, "data" ) ); |
| 140 |
int nel = valueEls.length; |
| 141 |
List list = new ArrayList( nel ); |
| 142 |
for ( int i = 0; i < nel; i++ ) { |
| 143 |
list.add( parseSampValue( valueEls[ i ] ) ); |
| 144 |
} |
| 145 |
return list; |
| 146 |
} |
| 147 |
else if ( "struct".equals( name ) ) { |
| 148 |
Element[] memberEls = getChildren( el ); |
| 149 |
Map map = new HashMap(); |
| 150 |
for ( int i = 0; i < memberEls.length; i++ ) { |
| 151 |
Element member = memberEls[ i ]; |
| 152 |
if ( ! "member".equals( member.getTagName() ) ) { |
| 153 |
throw new XmlRpcFormatException( |
| 154 |
"Non-<member> child of <struct>: " |
| 155 |
+ member.getTagName() ); |
| 156 |
} |
| 157 |
Element[] memberChildren = getChildren( member ); |
| 158 |
String key = null; |
| 159 |
Object value = null; |
| 160 |
for ( int j = 0; j < memberChildren.length; j++ ) { |
| 161 |
Element memberChild = memberChildren[ j ]; |
| 162 |
String memberName = memberChild.getTagName(); |
| 163 |
if ( "name".equals( memberName ) ) { |
| 164 |
key = getTextContent( memberChild ); |
| 165 |
} |
| 166 |
else if ( "value".equals( memberName ) ) { |
| 167 |
value = parseSampValue( memberChild ); |
| 168 |
} |
| 169 |
} |
| 170 |
if ( key == null ) { |
| 171 |
throw new XmlRpcFormatException( "<name> missing" |
| 172 |
+ " in struct member" ); |
| 173 |
} |
| 174 |
if ( value == null ) { |
| 175 |
throw new XmlRpcFormatException( "<value> missing" |
| 176 |
+ " in struct member" ); |
| 177 |
} |
| 178 |
if ( map.containsKey( key ) ) { |
| 179 |
logger_.warning( "Re-used key " + key + " in map" ); |
| 180 |
} |
| 181 |
map.put( key, value ); |
| 182 |
} |
| 183 |
return map; |
| 184 |
} |
| 185 |
else if ( "string".equals( name ) ) { |
| 186 |
return getTextContent( el ); |
| 187 |
} |
| 188 |
else if ( "i4".equals( name ) || "int".equals( name ) ) { |
| 189 |
String text = getTextContent( el ); |
| 190 |
try { |
| 191 |
return Integer.valueOf( text ); |
| 192 |
} |
| 193 |
catch ( NumberFormatException e ) { |
| 194 |
throw new XmlRpcFormatException( "Bad int " + text ); |
| 195 |
} |
| 196 |
} |
| 197 |
else if ( "boolean".equals( name ) ) { |
| 198 |
String text = getTextContent( el ); |
| 199 |
if ( "0".equals( text ) ) { |
| 200 |
return Boolean.FALSE; |
| 201 |
} |
| 202 |
else if ( "1".equals( text ) ) { |
| 203 |
return Boolean.TRUE; |
| 204 |
} |
| 205 |
else { |
| 206 |
throw new XmlRpcFormatException( "Bad boolean " + text ); |
| 207 |
} |
| 208 |
} |
| 209 |
else if ( "double".equals( name ) ) { |
| 210 |
String text = getTextContent( el ); |
| 211 |
try { |
| 212 |
return Double.valueOf( text ); |
| 213 |
} |
| 214 |
catch ( NumberFormatException e ) { |
| 215 |
throw new XmlRpcFormatException( "Bad double " + text ); |
| 216 |
} |
| 217 |
} |
| 218 |
else if ( "dateTime.iso8601".equals( name ) || |
| 219 |
"base64".equals( name ) ) { |
| 220 |
throw new XmlRpcFormatException( name + " not used in SAMP" ); |
| 221 |
} |
| 222 |
else { |
| 223 |
throw new XmlRpcFormatException( "Unknown XML-RPC element " |
| 224 |
+ "<" + name + ">" ); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Returns a new DocumentBuilder with default characteristics. |
| 230 |
* |
| 231 |
* @return new document builder instance |
| 232 |
*/ |
| 233 |
static DocumentBuilder createDocumentBuilder() |
| 234 |
throws ParserConfigurationException { |
| 235 |
if ( dbFact_ == null ) { |
| 236 |
dbFact_ = DocumentBuilderFactory.newInstance(); |
| 237 |
} |
| 238 |
return dbFact_.newDocumentBuilder(); |
| 239 |
} |
| 240 |
} |