1 17 package com.sun.org.apache.xml.internal.security.utils; 18 19 20 21 import java.math.BigInteger ; 22 import java.util.HashMap ; 23 24 import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException; 25 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 26 import org.w3c.dom.Attr ; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 import org.w3c.dom.Text ; 32 33 34 40 public abstract class ElementProxy { 41 42 43 static java.util.logging.Logger log = 44 java.util.logging.Logger.getLogger(ElementProxy.class.getName()); 45 47 public static final int MODE_CREATE = 0; 48 49 public static final int MODE_PROCESS = 1; 50 51 public static final int MODE_UNKNOWN = 2; 52 53 54 public static final int MODE_SIGN = MODE_CREATE; 55 56 public static final int MODE_VERIFY = MODE_PROCESS; 57 58 59 public static final int MODE_ENCRYPT = MODE_CREATE; 60 61 public static final int MODE_DECRYPT = MODE_PROCESS; 62 63 protected int _state = MODE_UNKNOWN; 64 66 71 public abstract String getBaseNamespace(); 72 73 78 public abstract String getBaseLocalName(); 79 80 81 protected Element _constructionElement = null; 82 83 84 protected String _baseURI = null; 85 86 87 protected Document _doc = null; 88 89 93 public ElementProxy() { 94 95 this._doc = null; 96 this._state = ElementProxy.MODE_UNKNOWN; 97 this._baseURI = null; 98 this._constructionElement = null; 99 } 100 101 106 public ElementProxy(Document doc) { 107 108 this(); 109 110 if (doc == null) { 111 throw new RuntimeException ("Document is null"); 112 } 113 114 this._doc = doc; 115 this._state = ElementProxy.MODE_CREATE; 116 this._constructionElement = ElementProxy.createElementForFamily(this._doc, 117 this.getBaseNamespace(), this.getBaseLocalName()); 118 } 119 120 132 public static Element createElementForFamily(Document doc, String namespace, 133 String localName) { 134 Element result = null; 136 String prefix = ElementProxy.getDefaultPrefix(namespace); 137 138 if (namespace == null) { 139 result = doc.createElementNS(null, localName); 140 } else { 141 if ((prefix == null) || (prefix.length() == 0)) { 142 result = doc.createElementNS(namespace, localName); 143 144 result.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", 145 namespace); 146 } else { 147 result = doc.createElementNS(namespace, prefix + ":" + localName); 148 149 result.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix, 150 namespace); 151 } 152 } 153 154 return result; 155 } 156 157 164 public void setElement(Element element, String BaseURI) 165 throws XMLSecurityException { 166 167 if (element == null) { 168 throw new XMLSecurityException("ElementProxy.nullElement"); 169 } 170 if (true) { 171 } 172 173 if (true) { 174 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "setElement(" + element.getTagName() + ", \"" + BaseURI + "\""); 175 } 176 177 this._doc = element.getOwnerDocument(); 178 this._state = ElementProxy.MODE_PROCESS; 179 this._constructionElement = element; 180 this._baseURI = BaseURI; 181 } 182 183 190 public ElementProxy(Element element, String BaseURI) 191 throws XMLSecurityException { 192 193 this(); 194 195 if (element == null) { 196 throw new XMLSecurityException("ElementProxy.nullElement"); 197 } 198 199 if (true) { 200 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "setElement(\"" + element.getTagName() + "\", \"" + BaseURI 201 + "\")"); 202 } 203 204 this._doc = element.getOwnerDocument(); 205 this._state = ElementProxy.MODE_PROCESS; 206 this._constructionElement = element; 207 this._baseURI = BaseURI; 208 209 this.guaranteeThatElementInCorrectSpace(); 210 } 211 212 217 public final Element getElement() { 218 return this._constructionElement; 219 } 220 221 226 public final NodeList getElementPlusReturns() { 227 228 HelperNodeList nl = new HelperNodeList(); 229 230 nl.appendChild(this._doc.createTextNode("\n")); 231 nl.appendChild(this.getElement()); 232 nl.appendChild(this._doc.createTextNode("\n")); 233 234 return nl; 235 } 236 237 242 public Document getDocument() { 243 return this._doc; 244 } 245 246 251 public String getBaseURI() { 252 return this._baseURI; 253 } 254 255 260 public void guaranteeThatElementInCorrectSpace() 261 throws XMLSecurityException { 262 263 String localnameSHOULDBE = this.getBaseLocalName(); 264 String namespaceSHOULDBE = this.getBaseNamespace(); 265 266 String localnameIS = this._constructionElement.getLocalName(); 267 String namespaceIS = this._constructionElement.getNamespaceURI(); 268 if ( !localnameSHOULDBE.equals(localnameIS) || 269 !namespaceSHOULDBE.equals(namespaceIS)) { 270 Object exArgs[] = { namespaceIS +":"+ localnameIS, 271 namespaceSHOULDBE +":"+ localnameSHOULDBE}; 272 throw new XMLSecurityException("xml.WrongElement", exArgs); 273 } 274 } 275 276 282 public void addBigIntegerElement(BigInteger bi, String localname) { 283 284 if (bi != null) { 285 Element e = XMLUtils.createElementInSignatureSpace(this._doc, 286 localname); 287 288 Base64.fillElementWithBigInteger(e, bi); 289 this._constructionElement.appendChild(e); 290 XMLUtils.addReturnToElement(this._constructionElement); 291 } 292 } 293 294 300 public void addBase64Element(byte[] bytes, String localname) { 301 302 if (bytes != null) { 303 304 Element e = Base64.encodeToElement(this._doc, localname, bytes); 305 306 this._constructionElement.appendChild(e); 307 this._constructionElement.appendChild(this._doc.createTextNode("\n")); 308 } 309 } 310 311 317 public void addTextElement(String text, String localname) { 318 319 Element e = XMLUtils.createElementInSignatureSpace(this._doc, localname); 320 Text t = this._doc.createTextNode(text); 321 322 e.appendChild(t); 323 this._constructionElement.appendChild(e); 324 XMLUtils.addReturnToElement(this._constructionElement); 325 } 326 327 332 public void addBase64Text(byte[] bytes) { 333 334 if (bytes != null) { 335 Text t = this._doc.createTextNode("\n" + Base64.encode(bytes) + "\n"); 336 337 this._constructionElement.appendChild(t); 338 } 339 } 340 341 346 public void addText(String text) { 347 348 if (text != null) { 349 Text t = this._doc.createTextNode(text); 350 351 this._constructionElement.appendChild(t); 352 } 353 } 354 355 363 public BigInteger getBigIntegerFromChildElement( 364 String localname, String namespace) throws Base64DecodingException { 365 366 return Base64.decodeBigIntegerFromText( 367 XMLUtils.selectNodeText(this._constructionElement.getFirstChild(), 368 namespace,localname,0)); 369 370 } 371 372 380 public byte[] getBytesFromChildElement(String localname, String namespace) 381 throws XMLSecurityException { 382 383 Element e = 384 XMLUtils.selectNode( 385 this._constructionElement.getFirstChild(), 386 namespace, 387 localname, 388 0); 389 390 return Base64.decode(e); 391 } 392 393 400 public String getTextFromChildElement(String localname, String namespace) { 401 402 Text t = 403 (Text ) XMLUtils.selectNode( 404 this._constructionElement.getFirstChild(), 405 namespace, 406 localname, 407 0).getFirstChild(); 408 409 return t.getData(); 410 } 411 412 418 public byte[] getBytesFromTextChild() throws XMLSecurityException { 419 420 Text t = (Text )this._constructionElement.getFirstChild(); 421 422 423 return Base64.decode(t.getData()); 424 } 425 426 431 public String getTextFromTextChild() { 432 return XMLUtils.getFullTextChildrenFromElement(this._constructionElement); 433 } 434 435 436 437 444 public int length(String namespace, String localname) { 445 int number=0; 446 Node sibling=this._constructionElement.getFirstChild(); 447 while (sibling!=null) { 448 if (localname.equals(sibling.getLocalName()) 449 && 450 namespace.equals(sibling.getNamespaceURI())) { 451 number++; 452 } 453 sibling=sibling.getNextSibling(); 454 } 455 return number; 456 } 457 458 471 public void setXPathNamespaceContext(String prefix, String uri) 472 throws XMLSecurityException { 473 474 String ns; 475 476 if ((prefix == null) || (prefix.length() == 0)) { 477 throw new XMLSecurityException("defaultNamespaceCannotBeSetHere"); 478 } else if (prefix.equals("xmlns")) { 479 throw new XMLSecurityException("defaultNamespaceCannotBeSetHere"); 480 } else if (prefix.startsWith("xmlns:")) { 481 ns = prefix; } else { 483 ns = "xmlns:" + prefix; 484 } 485 486 487 488 Attr a = this._constructionElement.getAttributeNodeNS(Constants.NamespaceSpecNS, ns); 489 490 if (a != null) { 491 if (!a.getNodeValue().equals(uri)) { 492 Object exArgs[] = { ns, 493 this._constructionElement.getAttributeNS(null, 494 ns) }; 495 496 throw new XMLSecurityException("namespacePrefixAlreadyUsedByOtherURI", 497 exArgs); 498 } 499 return; 500 } 501 502 this._constructionElement.setAttributeNS(Constants.NamespaceSpecNS, ns, 503 uri); 504 } 505 506 507 static HashMap _prefixMappings = new HashMap (); 508 509 516 public static void setDefaultPrefix(String namespace, String prefix) 517 throws XMLSecurityException { 518 519 if (ElementProxy._prefixMappings.containsValue(prefix)) { 520 521 Object storedNamespace=ElementProxy._prefixMappings.get(namespace); 522 if (!storedNamespace.equals(prefix)) { 523 Object exArgs[] = { prefix, namespace, storedNamespace }; 524 525 throw new XMLSecurityException("prefix.AlreadyAssigned", exArgs); 526 } 527 } 528 ElementProxy._prefixMappings.put(namespace, prefix); 529 } 530 531 537 public static String getDefaultPrefix(String namespace) { 538 539 String prefix = (String ) ElementProxy._prefixMappings.get(namespace); 540 541 return prefix; 542 } 543 } 544 | Popular Tags |