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