1 12 package mondrian.xmla; 13 14 import mondrian.olap.MondrianException; 15 import mondrian.util.Base64; 16 import org.apache.log4j.Logger; 17 import org.w3c.dom.*; 18 import org.xml.sax.InputSource ; 19 20 import javax.xml.parsers.DocumentBuilder ; 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import javax.xml.transform.Transformer ; 23 import javax.xml.transform.TransformerFactory ; 24 import javax.xml.transform.dom.DOMSource ; 25 import javax.xml.transform.stream.StreamResult ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.StringReader ; 29 import java.io.StringWriter ; 30 import java.util.ArrayList ; 31 import java.util.List ; 32 33 39 public class XmlaUtil implements XmlaConstants { 40 41 private static final Logger LOGGER = Logger.getLogger(XmlaUtil.class); 42 56 private static final String [] CHAR_TABLE = new String [256]; 57 58 static { 59 initCharTable(" \t\r\n(){}[]+/*%!,?"); 60 } 61 62 private static void initCharTable(String charStr) { 63 char[] chars = charStr.toCharArray(); 64 for (int i = 0; i < chars.length; i++) { 65 char c = chars[i]; 66 CHAR_TABLE[c] = encodeChar(c); 67 } 68 } 69 70 private static String encodeChar(char c) { 71 StringBuilder buf = new StringBuilder (); 72 buf.append("_x"); 73 String str = Integer.toHexString(c); 74 for (int i = 4 - str.length(); i > 0; i--) { 75 buf.append("0"); 76 } 77 return buf.append(str).append("_").toString(); 78 } 79 80 100 public static String encodeElementName(String name) { 101 StringBuilder buf = new StringBuilder (); 102 char[] nameChars = name.toCharArray(); 103 for (int i = 0; i < nameChars.length; i++) { 104 char ch = nameChars[i]; 105 String encodedStr = (ch >= CHAR_TABLE.length ? null : CHAR_TABLE[ch]); 106 if (encodedStr == null) { 107 buf.append(ch); 108 } else { 109 buf.append(encodedStr); 110 } 111 } 112 return buf.toString(); 113 } 114 115 116 public static String element2Text(Element elem) 117 throws XmlaException { 118 StringWriter writer = new StringWriter (); 119 try { 120 TransformerFactory factory = TransformerFactory.newInstance(); 121 Transformer transformer = factory.newTransformer(); 122 transformer.transform(new DOMSource (elem), new StreamResult (writer)); 123 } catch (Exception e) { 124 throw new XmlaException( 125 CLIENT_FAULT_FC, 126 USM_DOM_PARSE_CODE, 127 USM_DOM_PARSE_FAULT_FS, 128 e); 129 } 130 return writer.getBuffer().toString(); 131 } 132 133 public static Element text2Element(String text) 134 throws XmlaException { 135 return _2Element(new InputSource (new StringReader (text))); 136 } 137 138 public static Element stream2Element(InputStream stream) 139 throws XmlaException { 140 return _2Element(new InputSource (stream)); 141 } 142 143 private static Element _2Element(InputSource source) 144 throws XmlaException { 145 try { 146 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 147 factory.setIgnoringElementContentWhitespace(true); 148 factory.setIgnoringComments(true); 149 factory.setNamespaceAware(true); 150 DocumentBuilder builder = factory.newDocumentBuilder(); 151 Document doc = builder.parse(source); 152 Element elem = doc.getDocumentElement(); 153 return elem; 154 155 } catch (Exception e) { 156 throw new XmlaException( 157 CLIENT_FAULT_FC, 158 USM_DOM_PARSE_CODE, 159 USM_DOM_PARSE_FAULT_FS, 160 e); 161 } 162 } 163 164 167 public static Element firstChildElement(Element parent, 168 String ns, 169 String lname) { 170 if (LOGGER.isDebugEnabled()) { 171 StringBuilder buf = new StringBuilder (100); 172 buf.append("XmlaUtil.firstChildElement: "); 173 buf.append(" ns=\""); 174 buf.append(ns); 175 buf.append("\", lname=\""); 176 buf.append(lname); 177 buf.append("\""); 178 LOGGER.debug(buf.toString()); 179 } 180 NodeList nlst = parent.getChildNodes(); 181 for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) { 182 Node n = nlst.item(i); 183 if (n instanceof Element) { 184 Element e = (Element) n; 185 186 if (LOGGER.isDebugEnabled()) { 187 StringBuilder buf = new StringBuilder (100); 188 buf.append("XmlaUtil.firstChildElement: "); 189 buf.append(" e.getNamespaceURI()=\""); 190 buf.append(e.getNamespaceURI()); 191 buf.append("\", e.getLocalName()=\""); 192 buf.append(e.getLocalName()); 193 buf.append("\""); 194 LOGGER.debug(buf.toString()); 195 } 196 197 if ((ns == null || ns.equals(e.getNamespaceURI())) && 198 (lname == null || lname.equals(e.getLocalName()))) { 199 return e; 200 } 201 } 202 } 203 return null; 204 } 205 206 public static Element[] filterChildElements(Element parent, 207 String ns, 208 String lname) { 209 210 223 224 List elems = new ArrayList (); 225 NodeList nlst = parent.getChildNodes(); 226 for (int i = 0, nlen = nlst.getLength(); i < nlen; i++) { 227 Node n = nlst.item(i); 228 if (n instanceof Element) { 229 Element e = (Element) n; 230 231 243 244 if ((ns == null || ns.equals(e.getNamespaceURI())) && 245 (lname == null || lname.equals(e.getLocalName()))) { 246 elems.add(e); 247 } 248 } 249 } 250 return (Element[]) elems.toArray(new Element[0]); 251 } 252 253 public static String textInElement(Element elem) { 254 StringBuilder buf = new StringBuilder (100); 255 elem.normalize(); 256 NodeList nlst = elem.getChildNodes(); 257 for (int i = 0, nlen = nlst.getLength(); i < nlen ; i++) { 258 Node n = nlst.item(i); 259 if (n instanceof Text) { 260 final String data = ((Text) n).getData(); 261 buf.append(data); 262 } 263 } 264 return buf.toString(); 265 } 266 267 271 public static Throwable rootThrowable(Throwable throwable) { 272 Throwable rootThrowable = throwable.getCause(); 273 if (rootThrowable != null && rootThrowable instanceof MondrianException) { 274 return rootThrowable(rootThrowable); 275 } 276 return throwable; 277 } 278 279 290 public static String normalizeNumericString(String numericStr) { 291 int index = numericStr.indexOf('.'); 292 if (index > 0) { 293 boolean found = false; 294 int p = numericStr.length(); 295 char c = numericStr.charAt(p - 1); 296 while (c == '0') { 297 found = true; 298 p--; 299 c = numericStr.charAt(p - 1); 300 } 301 if (c == '.') { 302 p--; 303 } 304 if (found) { 305 return numericStr.substring(0, p); 306 } 307 } 308 return numericStr; 309 } 310 } 311 312 | Popular Tags |