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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

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

mercurial