325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.messaging.saaj.util;
325N/A
325N/Aimport java.util.logging.Logger;
325N/A
325N/Aimport javax.xml.parsers.SAXParser;
325N/Aimport javax.xml.soap.SOAPException;
325N/A
325N/Aimport org.xml.sax.*;
325N/Aimport org.xml.sax.ext.LexicalHandler;
325N/Aimport org.xml.sax.helpers.XMLFilterImpl;
325N/A
325N/Aimport com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
325N/Aimport org.xml.sax.helpers.AttributesImpl;
325N/A
325N/A/**
325N/A * Users of this class see a SAX2 XMLReader (via XMLFilterImpl). This
325N/A * class creates a parent XMLReader via JAXP and installs itself as a SAX2
325N/A * extension LexicalHandler which rejects document type declarations
325N/A * because they are not legal in SOAP. If the user of this class sets a
325N/A * LexicalHandler, then it forwards events to that handler.
325N/A *
325N/A *
325N/A * @author Edwin Goei
325N/A */
325N/A
325N/Apublic class RejectDoctypeSaxFilter extends XMLFilterImpl implements XMLReader, LexicalHandler{
325N/A protected static final Logger log =
325N/A Logger.getLogger(LogDomainConstants.UTIL_DOMAIN,
325N/A "com.sun.xml.internal.messaging.saaj.util.LocalStrings");
325N/A
325N/A /** Standard SAX 2.0 ext property */
325N/A static final String LEXICAL_HANDLER_PROP =
325N/A "http://xml.org/sax/properties/lexical-handler";
325N/A
325N/A static final String WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd".intern();
325N/A static final String SIGNATURE_LNAME = "Signature".intern();
325N/A static final String ENCRYPTED_DATA_LNAME = "EncryptedData".intern();
325N/A static final String DSIG_NS = "http://www.w3.org/2000/09/xmldsig#".intern();
325N/A static final String XENC_NS = "http://www.w3.org/2001/04/xmlenc#".intern();
325N/A static final String ID_NAME = "ID".intern();
325N/A
325N/A /** LexicalHandler to forward events to, if any */
325N/A private LexicalHandler lexicalHandler;
325N/A
325N/A public RejectDoctypeSaxFilter(SAXParser saxParser) throws SOAPException {
325N/A XMLReader xmlReader;
325N/A try {
325N/A xmlReader = saxParser.getXMLReader();
325N/A } catch (Exception e) {
325N/A log.severe("SAAJ0602.util.getXMLReader.exception");
325N/A throw new SOAPExceptionImpl(
325N/A "Couldn't get an XMLReader while constructing a RejectDoctypeSaxFilter",
325N/A e);
325N/A }
325N/A
325N/A // Set ourselves up to be the SAX LexicalHandler
325N/A try {
325N/A xmlReader.setProperty(LEXICAL_HANDLER_PROP, this);
325N/A } catch (Exception e) {
325N/A log.severe("SAAJ0603.util.setProperty.exception");
325N/A throw new SOAPExceptionImpl(
325N/A "Couldn't set the lexical handler property while constructing a RejectDoctypeSaxFilter",
325N/A e);
325N/A }
325N/A
325N/A // Set the parent XMLReader of this SAX filter
325N/A setParent(xmlReader);
325N/A }
325N/A
325N/A /*
325N/A * Override setProperty() to capture any LexicalHandler that is set for
325N/A * forwarding of events.
325N/A */
325N/A public void setProperty(String name, Object value)
325N/A throws SAXNotRecognizedException, SAXNotSupportedException {
325N/A if (LEXICAL_HANDLER_PROP.equals(name)) {
325N/A lexicalHandler = (LexicalHandler) value;
325N/A } else {
325N/A super.setProperty(name, value);
325N/A }
325N/A }
325N/A
325N/A //
325N/A // Beginning of SAX LexicalHandler callbacks...
325N/A //
325N/A
325N/A public void startDTD(String name, String publicId, String systemId)
325N/A throws SAXException {
325N/A throw new SAXException("Document Type Declaration is not allowed");
325N/A }
325N/A
325N/A public void endDTD() throws SAXException {
325N/A }
325N/A
325N/A public void startEntity(String name) throws SAXException {
325N/A if (lexicalHandler != null) {
325N/A lexicalHandler.startEntity(name);
325N/A }
325N/A }
325N/A
325N/A public void endEntity(String name) throws SAXException {
325N/A if (lexicalHandler != null) {
325N/A lexicalHandler.endEntity(name);
325N/A }
325N/A }
325N/A
325N/A public void startCDATA() throws SAXException {
325N/A if (lexicalHandler != null) {
325N/A lexicalHandler.startCDATA();
325N/A }
325N/A }
325N/A
325N/A public void endCDATA() throws SAXException {
325N/A if (lexicalHandler != null) {
325N/A lexicalHandler.endCDATA();
325N/A }
325N/A }
325N/A
325N/A public void comment(char[] ch, int start, int length) throws SAXException {
325N/A if (lexicalHandler != null) {
325N/A lexicalHandler.comment(ch, start, length);
325N/A }
325N/A }
325N/A
325N/A //
325N/A // End of SAX LexicalHandler callbacks
325N/A //
325N/A
325N/A public void startElement(String namespaceURI, String localName,
325N/A String qName, Attributes atts) throws SAXException{
325N/A if(atts != null ){
325N/A boolean eos = false;
325N/A if(namespaceURI == DSIG_NS || XENC_NS == namespaceURI){
325N/A eos = true;
325N/A }
325N/A int length = atts.getLength();
325N/A AttributesImpl attrImpl = new AttributesImpl();
325N/A for(int i=0; i< length;i++){
325N/A String name = atts.getLocalName(i);
325N/A if(name!=null && (name.equals("Id"))){
325N/A if(eos || atts.getURI(i) == WSU_NS ){
325N/A attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
325N/A atts.getQName(i), ID_NAME, atts.getValue(i));
325N/A }else{
325N/A attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i));
325N/A }
325N/A }else{
325N/A attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i),
325N/A atts.getQName(i), atts.getType(i), atts.getValue(i));
325N/A }
325N/A }
325N/A super.startElement(namespaceURI,localName, qName,attrImpl);
325N/A }else{
325N/A super.startElement(namespaceURI,localName, qName, atts);
325N/A }
325N/A }
325N/A}