src/share/classes/com/sun/xml/internal/messaging/saaj/util/RejectDoctypeSaxFilter.java

Mon, 04 May 2009 21:10:41 -0700

author
tbell
date
Mon, 04 May 2009 21:10:41 -0700
changeset 50
42dfec6871f6
parent 45
31822b475baa
child 78
860b95cc8d1d
permissions
-rw-r--r--

6658158: Mutable statics in SAAJ (findbugs)
6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs)
Reviewed-by: darcy

duke@1 1 /*
tbell@45 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
tbell@45 26
duke@1 27 package com.sun.xml.internal.messaging.saaj.util;
duke@1 28
duke@1 29 import java.util.logging.Logger;
duke@1 30
duke@1 31 import javax.xml.parsers.SAXParser;
duke@1 32 import javax.xml.soap.SOAPException;
duke@1 33
duke@1 34 import org.xml.sax.*;
duke@1 35 import org.xml.sax.ext.LexicalHandler;
duke@1 36 import org.xml.sax.helpers.XMLFilterImpl;
duke@1 37
duke@1 38 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
duke@1 39 import org.xml.sax.helpers.AttributesImpl;
duke@1 40
duke@1 41 /**
duke@1 42 * Users of this class see a SAX2 XMLReader (via XMLFilterImpl). This
duke@1 43 * class creates a parent XMLReader via JAXP and installs itself as a SAX2
duke@1 44 * extension LexicalHandler which rejects document type declarations
duke@1 45 * because they are not legal in SOAP. If the user of this class sets a
duke@1 46 * LexicalHandler, then it forwards events to that handler.
duke@1 47 *
tbell@50 48 *
duke@1 49 * @author Edwin Goei
duke@1 50 */
duke@1 51
duke@1 52 public class RejectDoctypeSaxFilter extends XMLFilterImpl implements XMLReader, LexicalHandler{
tbell@50 53 protected static final Logger log =
duke@1 54 Logger.getLogger(LogDomainConstants.UTIL_DOMAIN,
duke@1 55 "com.sun.xml.internal.messaging.saaj.util.LocalStrings");
duke@1 56
duke@1 57 /** Standard SAX 2.0 ext property */
duke@1 58 static final String LEXICAL_HANDLER_PROP =
duke@1 59 "http://xml.org/sax/properties/lexical-handler";
duke@1 60
duke@1 61 static final String WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd".intern();
duke@1 62 static final String SIGNATURE_LNAME = "Signature".intern();
duke@1 63 static final String ENCRYPTED_DATA_LNAME = "EncryptedData".intern();
duke@1 64 static final String DSIG_NS = "http://www.w3.org/2000/09/xmldsig#".intern();
duke@1 65 static final String XENC_NS = "http://www.w3.org/2001/04/xmlenc#".intern();
duke@1 66 static final String ID_NAME = "ID".intern();
duke@1 67
duke@1 68 /** LexicalHandler to forward events to, if any */
duke@1 69 private LexicalHandler lexicalHandler;
duke@1 70
duke@1 71 public RejectDoctypeSaxFilter(SAXParser saxParser) throws SOAPException {
duke@1 72 XMLReader xmlReader;
duke@1 73 try {
duke@1 74 xmlReader = saxParser.getXMLReader();
duke@1 75 } catch (Exception e) {
duke@1 76 log.severe("SAAJ0602.util.getXMLReader.exception");
duke@1 77 throw new SOAPExceptionImpl(
duke@1 78 "Couldn't get an XMLReader while constructing a RejectDoctypeSaxFilter",
duke@1 79 e);
duke@1 80 }
duke@1 81
duke@1 82 // Set ourselves up to be the SAX LexicalHandler
duke@1 83 try {
duke@1 84 xmlReader.setProperty(LEXICAL_HANDLER_PROP, this);
duke@1 85 } catch (Exception e) {
duke@1 86 log.severe("SAAJ0603.util.setProperty.exception");
duke@1 87 throw new SOAPExceptionImpl(
duke@1 88 "Couldn't set the lexical handler property while constructing a RejectDoctypeSaxFilter",
duke@1 89 e);
duke@1 90 }
duke@1 91
duke@1 92 // Set the parent XMLReader of this SAX filter
duke@1 93 setParent(xmlReader);
duke@1 94 }
duke@1 95
duke@1 96 /*
duke@1 97 * Override setProperty() to capture any LexicalHandler that is set for
duke@1 98 * forwarding of events.
duke@1 99 */
duke@1 100 public void setProperty(String name, Object value)
duke@1 101 throws SAXNotRecognizedException, SAXNotSupportedException {
duke@1 102 if (LEXICAL_HANDLER_PROP.equals(name)) {
duke@1 103 lexicalHandler = (LexicalHandler) value;
duke@1 104 } else {
duke@1 105 super.setProperty(name, value);
duke@1 106 }
duke@1 107 }
duke@1 108
duke@1 109 //
duke@1 110 // Beginning of SAX LexicalHandler callbacks...
duke@1 111 //
duke@1 112
duke@1 113 public void startDTD(String name, String publicId, String systemId)
duke@1 114 throws SAXException {
duke@1 115 throw new SAXException("Document Type Declaration is not allowed");
duke@1 116 }
duke@1 117
duke@1 118 public void endDTD() throws SAXException {
duke@1 119 }
duke@1 120
duke@1 121 public void startEntity(String name) throws SAXException {
duke@1 122 if (lexicalHandler != null) {
duke@1 123 lexicalHandler.startEntity(name);
duke@1 124 }
duke@1 125 }
duke@1 126
duke@1 127 public void endEntity(String name) throws SAXException {
duke@1 128 if (lexicalHandler != null) {
duke@1 129 lexicalHandler.endEntity(name);
duke@1 130 }
duke@1 131 }
duke@1 132
duke@1 133 public void startCDATA() throws SAXException {
duke@1 134 if (lexicalHandler != null) {
duke@1 135 lexicalHandler.startCDATA();
duke@1 136 }
duke@1 137 }
duke@1 138
duke@1 139 public void endCDATA() throws SAXException {
duke@1 140 if (lexicalHandler != null) {
duke@1 141 lexicalHandler.endCDATA();
duke@1 142 }
duke@1 143 }
duke@1 144
duke@1 145 public void comment(char[] ch, int start, int length) throws SAXException {
duke@1 146 if (lexicalHandler != null) {
duke@1 147 lexicalHandler.comment(ch, start, length);
duke@1 148 }
duke@1 149 }
duke@1 150
duke@1 151 //
duke@1 152 // End of SAX LexicalHandler callbacks
duke@1 153 //
duke@1 154
duke@1 155 public void startElement(String namespaceURI, String localName,
duke@1 156 String qName, Attributes atts) throws SAXException{
duke@1 157 if(atts != null ){
duke@1 158 boolean eos = false;
duke@1 159 if(namespaceURI == DSIG_NS || XENC_NS == namespaceURI){
duke@1 160 eos = true;
duke@1 161 }
duke@1 162 int length = atts.getLength();
duke@1 163 AttributesImpl attrImpl = new AttributesImpl();
duke@1 164 for(int i=0; i< length;i++){
duke@1 165 String name = atts.getLocalName(i);
duke@1 166 if(name!=null && (name.equals("Id"))){
duke@1 167 if(eos || atts.getURI(i) == WSU_NS ){
duke@1 168 attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
duke@1 169 atts.getQName(i), ID_NAME, atts.getValue(i));
duke@1 170 }else{
duke@1 171 attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i));
duke@1 172 }
duke@1 173 }else{
duke@1 174 attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
duke@1 175 atts.getQName(i), atts.getType(i), atts.getValue(i));
duke@1 176 }
duke@1 177 }
duke@1 178 super.startElement(namespaceURI,localName, qName,attrImpl);
duke@1 179 }else{
duke@1 180 super.startElement(namespaceURI,localName, qName, atts);
duke@1 181 }
duke@1 182 }
duke@1 183 }

mercurial