Example usage for org.xml.sax SAXException SAXException

List of usage examples for org.xml.sax SAXException SAXException

Introduction

In this page you can find the example usage for org.xml.sax SAXException SAXException.

Prototype

public SAXException(Exception e) 

Source Link

Document

Create a new SAXException wrapping an existing exception.

Usage

From source file:org.apache.axiom.om.impl.serialize.StreamWriterToContentHandlerConverter.java

/**
 * Method startElement./*  www  . j  a  v  a2s .co m*/
 *
 * @param namespaceURI
 * @param localName
 * @param qName
 * @param atts
 * @throws SAXException
 */
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
        throws SAXException {
    try {
        log.info("writing element {" + namespaceURI + '}' + localName + " directly to stream ");
        String prefix = getPrefix(qName);

        // it is only the prefix we want to learn from the QName! so we can get rid of the
        // spliting QName
        if (prefix == null) {
            writer.writeStartElement(namespaceURI, localName);
        } else {
            writer.writeStartElement(prefix, localName, namespaceURI);
        }
        if (atts != null) {
            int attCount = atts.getLength();
            for (int i = 0; i < attCount; i++) {
                writer.writeAttribute(atts.getURI(i), localName, atts.getValue(i));
            }
        }
    } catch (XMLStreamException e) {
        throw new SAXException(e);
    }
}

From source file:org.apache.axis.encoding.DeserializationContext.java

/**
 * Create a parser and parse the inputSource
 *///from w  ww.  j av a2  s  .  c  o m
public void parse() throws SAXException {
    if (inputSource != null) {
        SAXParser parser = XMLUtils.getSAXParser();
        try {
            parser.setProperty("http://xml.org/sax/properties/lexical-handler", this);
            parser.parse(inputSource, this);

            try {
                // cleanup - so that the parser can be reused.
                parser.setProperty("http://xml.org/sax/properties/lexical-handler", nullLexicalHandler);
            } catch (Exception e) {
                // Ignore.
            }

            // only release the parser for reuse if there wasn't an
            // error.  While parsers should be reusable, don't trust
            // parsers that died to clean up appropriately.
            XMLUtils.releaseSAXParser(parser);
        } catch (IOException e) {
            throw new SAXException(e);
        }
        inputSource = null;
    }
}

From source file:org.apache.axis.encoding.DeserializationContext.java

public void processingInstruction(String p1, String p2) throws SAXException {
    // must throw an error since SOAP 1.1 doesn't allow
    // processing instructions anywhere in the message
    throw new SAXException(Messages.getMessage("noInstructions00"));
}

From source file:org.apache.axis.encoding.DeserializationContext.java

/**
 * startElement is called when an element is read.  This is the big work-horse.
 *
 * This guy also handles monitoring the recording depth if we're recording
 * (so we know when to stop).//from  ww w. ja  v a  2 s.  c o m
 */
public void startElement(String namespace, String localName, String qName, Attributes attributes)
        throws SAXException {
    if (debugEnabled) {
        log.debug("Enter: DeserializationContext::startElement(" + namespace + ", " + localName + ")");
    }

    if (attributes == null || attributes.getLength() == 0) {
        attributes = NullAttributes.singleton;
    } else {
        attributes = new AttributesImpl(attributes);

        SOAPConstants soapConstants = getSOAPConstants();
        if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
            if (attributes.getValue(soapConstants.getAttrHref()) != null
                    && attributes.getValue(Constants.ATTR_ID) != null) {

                AxisFault fault = new AxisFault(Constants.FAULT_SOAP12_SENDER, null,
                        Messages.getMessage("noIDandHREFonSameElement"), null, null, null);

                throw new SAXException(fault);

            }
        }

    }

    SOAPHandler nextHandler = null;

    String prefix = "";
    int idx = qName.indexOf(':');
    if (idx > 0) {
        prefix = qName.substring(0, idx);
    }

    if (topHandler != null) {
        nextHandler = topHandler.onStartChild(namespace, localName, prefix, attributes, this);
    }

    if (nextHandler == null) {
        nextHandler = new SOAPHandler();
    }

    pushElementHandler(nextHandler);

    nextHandler.startElement(namespace, localName, prefix, attributes, this);

    if (!doneParsing && (recorder != null)) {
        recorder.startElement(namespace, localName, qName, attributes);
        if (!doneParsing) {
            curElement.setContentsIndex(recorder.getLength());
        }
    }

    if (startOfMappingsPos != -1) {
        startOfMappingsPos = -1;
    } else {
        // Push an empty frame if there are no mappings
        namespaces.push();
    }

    if (debugEnabled) {
        log.debug("Exit: DeserializationContext::startElement()");
    }
}

From source file:org.apache.axis.encoding.DeserializationContext.java

public void startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
        throws SAXException {
    /* It is possible for a malicious user to send us bad stuff in
       the <!DOCTYPE ../> tag that will cause a denial of service
       Example://from ww w.j  a v a2  s . c o m
       <?xml version="1.0" ?>
    <!DOCTYPE foobar [
        <!ENTITY x0 "hello">
        <!ENTITY x1 "&x0;&x0;">
        <!ENTITY x2 "&x1;&x1;">
          ...
        <!ENTITY x99 "&x98;&x98;">
        <!ENTITY x100 "&x99;&x99;">
    ]>
    */
    throw new SAXException(Messages.getMessage("noInstructions00"));
    /* if (recorder != null)
    recorder.startDTD(name, publicId, systemId);
    */
}

From source file:org.apache.axis.encoding.DeserializerImpl.java

/**
 * This method is invoked after startElement when the element requires
 * deserialization (i.e. the element is not an href and the value is not nil.)
 * DeserializerImpl provides default behavior, which simply
 * involves obtaining a correct Deserializer and plugging its handler.
 * @param namespace is the namespace of the element
 * @param localName is the name of the element
 * @param prefix is the prefix of the element
 * @param attributes are the attributes on the element...used to get the type
 * @param context is the DeserializationContext
 *//*from  ww w  .jav a  2s .  c  o m*/
public void onStartElement(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {
    // If I'm the base class, try replacing myself with an
    // appropriate deserializer gleaned from type info.
    if (this.getClass().equals(DeserializerImpl.class)) {
        QName type = context.getTypeFromAttributes(namespace, localName, attributes);

        // If no type is specified, use the defaultType if available.
        // xsd:string is used if no type is provided.
        if (type == null) {
            type = defaultType;
            if (type == null) {
                type = Constants.XSD_STRING;
            }
        }

        if (debugEnabled) {
            log.debug(Messages.getMessage("gotType00", "Deser", "" + type));
        }

        // We know we're deserializing, but we don't have
        // a specific deserializer.  So create one using the
        // attribute type qname.
        if (type != null) {
            Deserializer dser = context.getDeserializerForType(type);
            if (dser == null) {
                dser = context.getDeserializerForClass(null);
            }
            if (dser != null) {
                // Move the value targets to the new deserializer
                dser.moveValueTargets(this);
                context.replaceElementHandler((SOAPHandler) dser);
                // And don't forget to give it the start event...
                boolean isRef = context.isProcessingRef();
                context.setProcessingRef(true);
                dser.startElement(namespace, localName, prefix, attributes, context);
                context.setProcessingRef(isRef);
            } else {
                throw new SAXException(Messages.getMessage("noDeser00", "" + type));
            }
        }
    }
}

From source file:org.apache.axis.encoding.FieldTarget.java

public void set(Object value) throws SAXException {
    try {/*from   ww  w  .  j  a v  a  2  s .c  o m*/
        targetField.set(targetObject, value);
    } catch (IllegalAccessException accEx) {
        log.error(Messages.getMessage("illegalAccessException00"), accEx);
        throw new SAXException(accEx);
    } catch (IllegalArgumentException argEx) {
        log.error(Messages.getMessage("illegalArgumentException00"), argEx);
        throw new SAXException(argEx);
    }
}

From source file:org.apache.axis.encoding.MethodTarget.java

/**
 * Set the target's value by invoking the targetMethod.
 * @param value is the new Object value/*w w w.j  a  v  a 2 s.c  om*/
 */
public void set(Object value) throws SAXException {
    try {
        targetMethod.invoke(targetObject, new Object[] { value });
    } catch (IllegalAccessException accEx) {
        log.error(Messages.getMessage("illegalAccessException00"), accEx);
        throw new SAXException(accEx);
    } catch (IllegalArgumentException argEx) {
        log.error(Messages.getMessage("illegalArgumentException00"), argEx);
        throw new SAXException(argEx);
    } catch (InvocationTargetException targetEx) {
        log.error(Messages.getMessage("invocationTargetException00"), targetEx);
        throw new SAXException(targetEx);
    }
}

From source file:org.apache.axis.encoding.ser.ArrayDeserializer.java

/**
 * This method is invoked after startElement when the element requires
 * deserialization (i.e. the element is not an href & the value is not nil)
 * DeserializerImpl provides default behavior, which simply
 * involves obtaining a correct Deserializer and plugging its handler.
 * @param namespace is the namespace of the element
 * @param localName is the name of the element
 * @param prefix is the prefix of the element
 * @param attributes are the attrs on the element...used to get the type
 * @param context is the DeserializationContext
 *///from w w  w .  j  a va2 s  .com
public void onStartElement(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {
    // Deserializing the xml array requires processing the
    // xsi:type= attribute, the soapenc:arrayType attribute,
    // and the xsi:type attributes of the individual elements.
    //
    // The xsi:type=<qName> attribute is used to determine the java
    // type of the array to instantiate.  Axis expects it
    // to be set to the generic "soapenc:Array" or to
    // a specific qName.  If the generic "soapenc:Array"
    // specification is used, Axis determines the array
    // type by examining the soapenc:arrayType attribute.
    //
    // The soapenc:arrayType=<qname><dims> is used to determine
    // i) the number of dimensions, 
    // ii) the length of each dimension,
    // iii) the default xsi:type of each of the elements.
    //
    // If the arrayType attribute is missing, Axis assumes
    // a single dimension array with length equal to the number
    // of nested elements.  In such cases, the default xsi:type of 
    // the elements is determined using the array xsi:type.
    //
    // The xsi:type attributes of the individual elements of the
    // array are used to determine the java type of the element.
    // If the xsi:type attribute is missing for an element, the 
    // default xsi:type value is used.

    if (log.isDebugEnabled()) {
        log.debug("Enter: ArrayDeserializer::startElement()");
    }

    soapConstants = context.getSOAPConstants();

    // Get the qname for the array type=, set it to null if
    // the generic type is used.
    QName typeQName = context.getTypeFromAttributes(namespace, localName, attributes);
    if (typeQName == null) {
        typeQName = getDefaultType();
    }

    if (typeQName != null && Constants.equals(Constants.SOAP_ARRAY, typeQName)) {
        typeQName = null;
    }

    // Now get the arrayType value
    QName arrayTypeValue = context.getQNameFromString(
            Constants.getValue(attributes, Constants.URIS_SOAP_ENC, soapConstants.getAttrItemType()));

    // The first part of the arrayType expression is 
    // the default item type qname.
    // The second part is the dimension information
    String dimString = null;
    QName innerQName = null;
    String innerDimString = "";
    if (arrayTypeValue != null) {
        if (soapConstants != SOAPConstants.SOAP12_CONSTANTS) {
            // Doing SOAP 1.1
            // Array dimension noted like this : [][x]
            String arrayTypeValueNamespaceURI = arrayTypeValue.getNamespaceURI();
            String arrayTypeValueLocalPart = arrayTypeValue.getLocalPart();

            int leftBracketIndex = arrayTypeValueLocalPart.lastIndexOf('[');
            int rightBracketIndex = arrayTypeValueLocalPart.lastIndexOf(']');
            if (leftBracketIndex == -1 || rightBracketIndex == -1 || rightBracketIndex < leftBracketIndex) {
                throw new IllegalArgumentException(Messages.getMessage("badArrayType00", "" + arrayTypeValue));
            }

            dimString = arrayTypeValueLocalPart.substring(leftBracketIndex + 1, rightBracketIndex);
            arrayTypeValueLocalPart = arrayTypeValueLocalPart.substring(0, leftBracketIndex);

            // If multi-dim array set to soapenc:Array
            if (arrayTypeValueLocalPart.endsWith("]")) {
                defaultItemType = Constants.SOAP_ARRAY;
                int bracket = arrayTypeValueLocalPart.indexOf("[");
                innerQName = new QName(arrayTypeValueNamespaceURI,
                        arrayTypeValueLocalPart.substring(0, bracket));
                innerDimString = arrayTypeValueLocalPart.substring(bracket);
            } else {
                defaultItemType = new QName(arrayTypeValueNamespaceURI, arrayTypeValueLocalPart);
            }

        } else {
            String arraySizeValue = attributes.getValue(soapConstants.getEncodingURI(),
                    Constants.ATTR_ARRAY_SIZE);
            int leftStarIndex = arraySizeValue.lastIndexOf('*');

            // Skip to num if any
            if (leftStarIndex != -1) {
                // "*" => ""
                if (leftStarIndex == 0 && arraySizeValue.length() == 1) {
                    // "* *" => ""
                } else if (leftStarIndex == (arraySizeValue.length() - 1)) {
                    throw new IllegalArgumentException(
                            Messages.getMessage("badArraySize00", "" + arraySizeValue));
                    // "* N" => "N"
                } else {
                    dimString = arraySizeValue.substring(leftStarIndex + 2);
                    innerQName = arrayTypeValue;
                    innerDimString = arraySizeValue.substring(0, leftStarIndex + 1);
                }
            } else {
                dimString = arraySizeValue;
            }

            if (innerDimString == null || innerDimString.length() == 0) {
                defaultItemType = arrayTypeValue;
            } else {
                defaultItemType = Constants.SOAP_ARRAY12;
            }
        }
    }

    // If no type QName and no defaultItemType qname, use xsd:anyType
    if (defaultItemType == null && typeQName == null) {
        Class destClass = context.getDestinationClass();
        if (destClass != null && destClass.isArray()) {
            // This will get set OK down below...
        } else {
            defaultItemType = Constants.XSD_ANYTYPE;
        }
    }

    // Determine the class type for the array.
    arrayClass = null;
    if (typeQName != null) {
        arrayClass = context.getTypeMapping().getClassForQName(typeQName);
    }

    if (typeQName == null || arrayClass == null) {
        // type= information is not sufficient.
        // Get an array of the default item type.
        Class arrayItemClass = null;
        QName compQName = defaultItemType;

        // Nested array, use the innermost qname
        String dims = "[]";
        if (innerQName != null) {
            compQName = innerQName;

            if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
                // With SOAP 1.2 Array, we append [] for each * found
                int offset = 0;
                while ((offset = innerDimString.indexOf('*', offset)) != -1) {
                    dims += "[]";
                    offset++;
                }
            } else {
                // With SOAP 1.1 Array, we can append directly the complete innerDimString
                dims += innerDimString;
            }
        }

        // item Class
        arrayItemClass = context.getTypeMapping().getClassForQName(compQName);
        if (arrayItemClass != null) {
            try {
                // Append the dimension found to the classname computed from the itemClass
                // to form the array classname
                //
                String loadableArrayClassName = JavaUtils
                        .getLoadableClassName(JavaUtils.getTextClassName(arrayItemClass.getName()) + dims);
                arrayClass = ClassUtils.forName(loadableArrayClassName, true, arrayItemClass.getClassLoader());
            } catch (Exception e) {
                throw new SAXException(Messages.getMessage("noComponent00", "" + defaultItemType));
            }
        }
    }
    if (arrayClass == null) {
        arrayClass = context.getDestinationClass();
    }

    if (arrayClass == null) {
        throw new SAXException(Messages.getMessage("noComponent00", "" + defaultItemType));
    }

    if (dimString == null || dimString.length() == 0) {
        // Size determined using length of the members
        value = new ArrayListExtension(arrayClass);
    } else {
        try {
            StringTokenizer tokenizer;
            if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
                tokenizer = new StringTokenizer(dimString);
            } else {
                tokenizer = new StringTokenizer(dimString, "[],");
            }

            length = Integer.parseInt(tokenizer.nextToken());
            if (tokenizer.hasMoreTokens()) {
                // If the array is passed as a multi-dimensional array
                // (i.e. int[2][3]) then store all of the 
                // mult-dim lengths.
                // The valueReady method uses this array to set the
                // proper mult-dim element.
                mDimLength = new ArrayList();
                mDimLength.add(new Integer(length));

                while (tokenizer.hasMoreTokens()) {
                    mDimLength.add(new Integer(Integer.parseInt(tokenizer.nextToken())));
                }
            }

            // Create an ArrayListExtension class to store the ArrayList
            // plus converted objects.
            ArrayList list = new ArrayListExtension(arrayClass, length);

            // This is expensive as our array may not grown this big.
            // Prevents problems when XML claims a huge size
            // that it doesn't actually fill.
            //for (int i = 0; i < length; i++) {
            //    list.add(null);
            //}
            value = list;

        } catch (NumberFormatException e) {
            throw new IllegalArgumentException(Messages.getMessage("badInteger00", dimString));
        }
    }

    // If soapenc:offset specified, set the current index accordingly
    String offset = Constants.getValue(attributes, Constants.URIS_SOAP_ENC, Constants.ATTR_OFFSET);
    if (offset != null) {
        if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
            throw new SAXException(Messages.getMessage("noSparseArray"));
        }

        int leftBracketIndex = offset.lastIndexOf('[');
        int rightBracketIndex = offset.lastIndexOf(']');

        if (leftBracketIndex == -1 || rightBracketIndex == -1 || rightBracketIndex < leftBracketIndex) {
            throw new SAXException(Messages.getMessage("badOffset00", offset));
        }

        curIndex = convertToIndex(offset.substring(leftBracketIndex + 1, rightBracketIndex), "badOffset00");
    }

    if (log.isDebugEnabled()) {
        log.debug("Exit: ArrayDeserializer::startElement()");
    }
}

From source file:org.apache.axis.encoding.ser.ArrayDeserializer.java

/**
 * onStartChild is called on each child element.
 * @param namespace is the namespace of the child element
 * @param localName is the local name of the child element
 * @param prefix is the prefix used on the name of the child element
 * @param attributes are the attributes of the child element
 * @param context is the deserialization context.
 * @return is a Deserializer to use to deserialize a child (must be
 * a derived class of SOAPHandler) or null if no deserialization should
 * be performed.//  ww w  .j  a v  a  2s. c  o  m
 */
public SOAPHandler onStartChild(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {
    if (log.isDebugEnabled()) {
        log.debug("Enter: ArrayDeserializer.onStartChild()");
    }

    // If the position attribute is set, 
    // use it to update the current index
    if (attributes != null) {
        String pos = Constants.getValue(attributes, Constants.URIS_SOAP_ENC, Constants.ATTR_POSITION);
        if (pos != null) {
            if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
                throw new SAXException(Messages.getMessage("noSparseArray"));
            }

            int leftBracketIndex = pos.lastIndexOf('[');
            int rightBracketIndex = pos.lastIndexOf(']');

            if (leftBracketIndex == -1 || rightBracketIndex == -1 || rightBracketIndex < leftBracketIndex) {
                throw new SAXException(Messages.getMessage("badPosition00", pos));
            }

            curIndex = convertToIndex(pos.substring(leftBracketIndex + 1, rightBracketIndex), "badPosition00");
        }

        // If the xsi:nil attribute, set the value to null 
        // and return since there is nothing to deserialize.
        if (context.isNil(attributes)) {
            setChildValue(null, new Integer(curIndex++));
            return null;
        }
    }

    // Use the xsi:type setting on the attribute if it exists.
    QName itemType = context.getTypeFromAttributes(namespace, localName, attributes);

    // Get the deserializer for the type. 
    Deserializer dSer = null;
    if (itemType != null && (context.getCurElement().getHref() == null)) {
        dSer = context.getDeserializerForType(itemType);
    }

    if (dSer == null) {
        // No deserializer can be found directly.  Need to look harder
        QName defaultType = defaultItemType;
        Class javaType = null;
        if (arrayClass != null && arrayClass.isArray() && defaultType == null) {
            javaType = arrayClass.getComponentType();
            defaultType = context.getTypeMapping().getTypeQName(javaType);
        }

        // We don't have a deserializer, the safest thing to do
        // is to set up using the DeserializerImpl below.  
        // The DeserializerImpl will take care of href/id and
        // install the appropriate serializer, etc.  The problem 
        // is that takes a lot of time and will occur 
        // all the time if no xsi:types are sent.  Most of the
        // time an item is a simple schema type (i.e. String)
        // so the following shortcut is used to get a Deserializer
        // for these cases. 
        if (itemType == null && dSer == null) {
            if (defaultType != null && SchemaUtils.isSimpleSchemaType(defaultType)) {
                dSer = context.getDeserializer(javaType, defaultType);
            }
        }

        // If no deserializer is 
        // found, the deserializer is set to DeserializerImpl().
        // It is possible that the element has an href, thus we
        // won't know the type until the definitition is encountered.
        if (dSer == null) {
            dSer = new DeserializerImpl();
            // Determine a default type for the deserializer
            if (itemType == null) {
                dSer.setDefaultType(defaultType);
            }
        }
    }

    // Register the callback value target, and
    // keep track of this index so we know when it has been set.
    dSer.registerValueTarget(new DeserializerTarget(this, new Integer(curIndex)));

    // The framework handles knowing when the value is complete, as
    // long as we tell it about each child we're waiting on...
    addChildDeserializer(dSer);

    curIndex++;

    // In case of multi-array, we need to specify the destination class
    // of the children elements of this element array deserializer.
    context.setDestinationClass(arrayClass.getComponentType());

    if (log.isDebugEnabled()) {
        log.debug("Exit: ArrayDeserializer.onStartChild()");
    }

    return (SOAPHandler) dSer;
}