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.axis.encoding.ser.ArrayDeserializer.java

public void characters(char[] chars, int i, int i1) throws SAXException {
    for (int idx = i; i < i1; i++) {
        if (!Character.isWhitespace(chars[idx]))
            throw new SAXException(Messages.getMessage("charsInArray"));
    }//from  w  w  w.ja  va 2s .c  o m
}

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

/**
 * Converts the given string to an index.
 * Assumes the string consists of a brackets surrounding comma 
 * separated digits.  For example "[2]" or [2,3]".
 * The routine returns a single index./*  w  ww  . j a v  a 2s.c o m*/
 * For example "[2]" returns 2.
 * For example "[2,3]" depends on the size of the multiple dimensions.
 *   if the dimensions are "[3,5]" then 13 is returned (2*5) + 3.
 * @param text representing index text
 * @param exceptKey exception message key
 * @return index 
 */
private int convertToIndex(String text, String exceptKey) throws SAXException {
    StringTokenizer tokenizer = new StringTokenizer(text, "[],");
    int index = 0;
    try {
        if (mDimLength == null) {
            // Normal Case: Single dimension
            index = Integer.parseInt(tokenizer.nextToken());
            if (tokenizer.hasMoreTokens()) {
                throw new SAXException(Messages.getMessage(exceptKey, text));
            }
        } else {
            // Multiple Dimensions: 
            int dim = -1;
            ArrayList work = new ArrayList();
            while (tokenizer.hasMoreTokens()) {
                // Problem if the number of dimensions specified exceeds
                // the number of dimensions of arrayType
                dim++;
                if (dim >= mDimLength.size()) {
                    throw new SAXException(Messages.getMessage(exceptKey, text));
                }
                // Get the next token and convert to integer
                int workIndex = Integer.parseInt(tokenizer.nextToken());

                // Problem if the index is out of range.                     
                if (workIndex < 0 || workIndex >= ((Integer) mDimLength.get(dim)).intValue()) {
                    throw new SAXException(Messages.getMessage(exceptKey, text));
                }
                work.add(new Integer(workIndex));
            }
            index = toSingleIndex(work); // Convert to single index
        }
    } catch (SAXException e) {
        throw e;
    } catch (Exception e) {
        throw new SAXException(Messages.getMessage(exceptKey, text));
    }
    return index;
}

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

/**
 * Deserializer interface called on each child element encountered in
 * the XML stream./*from ww w. j  av a  2s.c  om*/
 * @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.
 */
public SOAPHandler onStartChild(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {
    handleMixedContent();

    BeanPropertyDescriptor propDesc = null;
    FieldDesc fieldDesc = null;

    SOAPConstants soapConstants = context.getSOAPConstants();
    String encodingStyle = context.getEncodingStyle();
    boolean isEncoded = Constants.isSOAP_ENC(encodingStyle);

    QName elemQName = new QName(namespace, localName);
    // The collectionIndex needs to be reset for Beans with multiple arrays
    if ((prevQName == null) || (!prevQName.equals(elemQName))) {
        collectionIndex = -1;
    }

    boolean isArray = false;
    QName itemQName = null;
    if (typeDesc != null) {
        // Lookup the name appropriately (assuming an unqualified
        // name for SOAP encoding, using the namespace otherwise)
        String fieldName = typeDesc.getFieldNameForElement(elemQName, isEncoded);
        propDesc = (BeanPropertyDescriptor) propertyMap.get(fieldName);
        fieldDesc = typeDesc.getFieldByName(fieldName);

        if (fieldDesc != null) {
            ElementDesc element = (ElementDesc) fieldDesc;
            isArray = element.isMaxOccursUnbounded();
            itemQName = element.getItemQName();
        }
    }

    if (propDesc == null) {
        // look for a field by this name.
        propDesc = (BeanPropertyDescriptor) propertyMap.get(localName);
    }

    // Workaround
    if (propDesc == null) {
        StringBuffer sb = new StringBuffer();
        sb.append(Character.toLowerCase(localName.charAt(0)));
        if (localName.length() > 1)
            sb.append(localName.substring(1));
        // look for a field by this name.
        propDesc = (BeanPropertyDescriptor) propertyMap.get(sb.toString());

    }

    // try and see if this is an xsd:any namespace="##any" element before
    // reporting a problem
    if (propDesc == null || (((prevQName != null) && prevQName.equals(elemQName)
            && !(propDesc.isIndexed() || isArray) && getAnyPropertyDesc() != null))) {
        // try to put unknown elements into a SOAPElement property, if
        // appropriate
        prevQName = elemQName;
        propDesc = getAnyPropertyDesc();
        if (propDesc != null) {
            try {
                MessageElement[] curElements = (MessageElement[]) propDesc.get(value);
                int length = 0;
                if (curElements != null) {
                    length = curElements.length;
                }
                MessageElement[] newElements = new MessageElement[length + 1];
                if (curElements != null) {
                    System.arraycopy(curElements, 0, newElements, 0, length);
                }
                MessageElement thisEl = context.getCurElement();

                newElements[length] = thisEl;
                propDesc.set(value, newElements);
                // if this is the first pass through the MessageContexts
                // make sure that the correct any element is set,
                // that is the child of the current MessageElement, however
                // on the first pass this child has not been set yet, so
                // defer it to the child SOAPHandler
                if (!localName.equals(thisEl.getName())) {
                    return new SOAPHandler(newElements, length);
                }
                return new SOAPHandler();
            } catch (Exception e) {
                throw new SAXException(e);
            }
        }
    }

    if (propDesc == null) {
        // No such field
        throw new SAXException(Messages.getMessage("badElem00", javaType.getName(), localName));
    }

    prevQName = elemQName;
    // Get the child's xsi:type if available
    QName childXMLType = context.getTypeFromAttributes(namespace, localName, attributes);
    String href = attributes.getValue(soapConstants.getAttrHref());
    Class fieldType = propDesc.getType();

    // If no xsi:type or href, check the meta-data for the field
    if (childXMLType == null && fieldDesc != null && href == null) {
        childXMLType = fieldDesc.getXmlType();
        if (itemQName != null) {
            // This is actually a wrapped literal array and should be
            // deserialized with the ArrayDeserializer
            childXMLType = Constants.SOAP_ARRAY;
            fieldType = propDesc.getActualType();
        } else {
            childXMLType = fieldDesc.getXmlType();
        }
    }

    // Get Deserializer for child, default to using DeserializerImpl
    Deserializer dSer = getDeserializer(childXMLType, fieldType, href, context);

    // It is an error if the dSer is not found - the only case where we
    // wouldn't have a deserializer at this point is when we're trying
    // to deserialize something we have no clue about (no good xsi:type,
    // no good metadata).
    if (dSer == null) {
        dSer = context.getDeserializerForClass(propDesc.getType());
    }

    // Fastpath nil checks...
    if (context.isNil(attributes)) {
        if ((propDesc.isIndexed() || isArray)) {
            if (!((dSer != null) && (dSer instanceof ArrayDeserializer))) {
                collectionIndex++;
                dSer.registerValueTarget(new BeanPropertyTarget(value, propDesc, collectionIndex));
                addChildDeserializer(dSer);
                return (SOAPHandler) dSer;
            }
        }
        return null;
    }

    if (dSer == null) {
        throw new SAXException(Messages.getMessage("noDeser00", childXMLType.toString()));
    }

    if (constructorToUse != null) {
        if (constructorTarget == null) {
            constructorTarget = new ConstructorTarget(constructorToUse, this);
        }
        dSer.registerValueTarget(constructorTarget);
    } else if (propDesc.isWriteable()) {
        // If this is an indexed property, and the deserializer we found
        // was NOT the ArrayDeserializer, this is a non-SOAP array:
        // <bean>
        //   <field>value1</field>
        //   <field>value2</field>
        // ...
        // In this case, we want to use the collectionIndex and make sure
        // the deserialized value for the child element goes into the
        // right place in the collection.

        // Register value target
        if ((itemQName != null || propDesc.isIndexed() || isArray) && !(dSer instanceof ArrayDeserializer)) {
            collectionIndex++;
            dSer.registerValueTarget(new BeanPropertyTarget(value, propDesc, collectionIndex));
        } else {
            // If we're here, the element maps to a single field value,
            // whether that be a "basic" type or an array, so use the
            // normal (non-indexed) BeanPropertyTarget form.
            collectionIndex = -1;
            dSer.registerValueTarget(new BeanPropertyTarget(value, propDesc));
        }
    }

    // Let the framework know that we need this deserializer to complete
    // for the bean to complete.
    addChildDeserializer(dSer);

    return (SOAPHandler) dSer;
}

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

/**
 * Set the bean properties that correspond to element attributes.
 * //w w  w  .  j  a v  a 2  s.  co m
 * 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.)
 * @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
 */
public void onStartElement(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {

    // The value should have been created or assigned already.
    // This code may no longer be needed.
    if (value == null && constructorToUse == null) {
        // create a value
        try {
            value = javaType.newInstance();
        } catch (Exception e) {
            throw new SAXException(Messages.getMessage("cantCreateBean00", javaType.getName(), e.toString()));
        }
    }

    // If no type description meta data, there are no attributes,
    // so we are done.
    if (typeDesc == null)
        return;

    // loop through the attributes and set bean properties that 
    // correspond to attributes
    for (int i = 0; i < attributes.getLength(); i++) {
        QName attrQName = new QName(attributes.getURI(i), attributes.getLocalName(i));
        String fieldName = typeDesc.getFieldNameForAttribute(attrQName);
        if (fieldName == null)
            continue;

        FieldDesc fieldDesc = typeDesc.getFieldByName(fieldName);

        // look for the attribute property
        BeanPropertyDescriptor bpd = (BeanPropertyDescriptor) propertyMap.get(fieldName);
        if (bpd != null) {
            if (constructorToUse == null) {
                // check only if default constructor
                if (!bpd.isWriteable() || bpd.isIndexed())
                    continue;
            }

            // Get the Deserializer for the attribute
            Deserializer dSer = getDeserializer(fieldDesc.getXmlType(), bpd.getType(), null, context);
            if (dSer == null) {
                dSer = context.getDeserializerForClass(bpd.getType());

                // The java type is an array, but the context didn't
                // know that we are an attribute.  Better stick with
                // simple types..
                if (dSer instanceof ArrayDeserializer) {
                    SimpleListDeserializerFactory factory = new SimpleListDeserializerFactory(bpd.getType(),
                            fieldDesc.getXmlType());
                    dSer = (Deserializer) factory.getDeserializerAs(dSer.getMechanismType());
                }
            }

            if (dSer == null)
                throw new SAXException(Messages.getMessage("unregistered00", bpd.getType().toString()));

            if (!(dSer instanceof SimpleDeserializer))
                throw new SAXException(
                        Messages.getMessage("AttrNotSimpleType00", bpd.getName(), bpd.getType().toString()));

            // Success!  Create an object from the string and set
            // it in the bean
            try {
                dSer.onStartElement(namespace, localName, prefix, attributes, context);
                Object val = ((SimpleDeserializer) dSer).makeValue(attributes.getValue(i));
                if (constructorToUse == null) {
                    bpd.set(value, val);
                } else {
                    // add value for our constructor
                    if (constructorTarget == null) {
                        constructorTarget = new ConstructorTarget(constructorToUse, this);
                    }
                    constructorTarget.set(val);
                }
            } catch (Exception e) {
                throw new SAXException(e);
            }

        } // if
    } // attribute loop
}

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

/**
 * set the bean property with specified value
 * @param value is the value.//  www  .  j  a  v  a2  s . c o m
 */
public void set(Object value) throws SAXException {

    try {
        // Set the value on the bean property. 
        // Use the indexed property method if the 
        // index is set.
        if (index < 0) {
            pd.set(object, value);
        } else {
            pd.set(object, index, value);
        }
    } catch (Exception e) {

        try {
            // If an exception occurred, 
            // see it the value can be converted into
            // the expected type.
            Class type = pd.getType();

            if (value.getClass().isArray() && value.getClass().getComponentType().isPrimitive()
                    && type.isArray() && type.getComponentType().equals(Object.class)) {
                //we make our own array type here.
                type = Array.newInstance(JavaUtils.getWrapperClass(value.getClass().getComponentType()), 0)
                        .getClass();
            }

            if (JavaUtils.isConvertable(value, type)) {
                value = JavaUtils.convert(value, type);
                if (index < 0)
                    pd.set(object, value);
                else
                    pd.set(object, index, value);
            } else {
                // It is possible that an indexed
                // format was expected, but the
                // entire array was sent.  In such 
                // cases traverse the array and 
                // call the setter for each item.
                if (index == 0 && value.getClass().isArray() && !type.getClass().isArray()) {
                    for (int i = 0; i < Array.getLength(value); i++) {
                        Object item = JavaUtils.convert(Array.get(value, i), type);
                        pd.set(object, i, item);
                    }
                } else {
                    // Can't proceed.  Throw an exception that
                    // will be caught in the catch block below.
                    throw e;
                }
            }
        } catch (Exception ex) {
            // Throw a SAX exception with an informative
            // message.
            String field = pd.getName();
            if (index >= 0) {
                field += "[" + index + "]";
            }
            if (log.isErrorEnabled()) {
                //TODO: why is this just logged on the server-side and not thrown back to the client???
                String valueType = "null";
                if (value != null)
                    valueType = value.getClass().getName();
                log.error(Messages.getMessage("cantConvert02", new String[] { valueType, field,
                        (index >= 0) ? pd.getType().getComponentType().getName() : pd.getType().getName() }));
            }
            if (ex instanceof InvocationTargetException) {
                Throwable t = ((InvocationTargetException) ex).getTargetException();
                if (t != null) {
                    String classname = this.object.getClass().getName();
                    //show the context where this exception occured.
                    throw new SAXException(Messages.getMessage("cantConvert04", new String[] { classname, field,
                            (value == null) ? null : value.toString(), t.getMessage() }));
                }
            }
            throw new SAXException(ex);
        }
    }
}

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

public final void onEndElement(String namespace, String localName, DeserializationContext context)
        throws SAXException {
    try {//from   w  ww  . java  2s  . c  o m
        MessageElement msgElem = context.getCurElement();
        if (msgElem != null) {
            MessageContext messageContext = context.getMessageContext();
            Boolean currentElement = (Boolean) messageContext.getProperty(DESERIALIZE_CURRENT_ELEMENT);
            if (currentElement != null && currentElement.booleanValue()) {
                value = msgElem.getAsDocument();
                messageContext.setProperty(DESERIALIZE_CURRENT_ELEMENT, Boolean.FALSE);
                return;
            }
            List children = msgElem.getChildren();
            if (children != null) {
                msgElem = (MessageElement) children.get(0);
                if (msgElem != null)
                    value = msgElem.getAsDocument();
            }
        }
    } catch (Exception exp) {
        log.error(Messages.getMessage("exception00"), exp);
        throw new SAXException(exp);
    }
}

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

public final void onEndElement(String namespace, String localName, DeserializationContext context)
        throws SAXException {
    try {//from ww  w . j  a  v  a2 s  . c o  m
        MessageElement msgElem = context.getCurElement();
        if (msgElem != null) {
            MessageContext messageContext = context.getMessageContext();
            Boolean currentElement = (Boolean) messageContext.getProperty(DESERIALIZE_CURRENT_ELEMENT);
            if (currentElement != null && currentElement.booleanValue()) {
                value = msgElem.getAsDOM();
                messageContext.setProperty(DESERIALIZE_CURRENT_ELEMENT, Boolean.FALSE);
                return;
            }
            List children = msgElem.getChildren();
            if (children != null) {
                msgElem = (MessageElement) children.get(0);
                if (msgElem != null)
                    value = msgElem.getAsDOM();
            }
        }
    } catch (Exception exp) {
        log.error(Messages.getMessage("exception00"), exp);
        throw new SAXException(exp);
    }
}

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

public void startElement(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {

    if (!context.isDoneParsing()) {
        if (myElement == null) {
            try {
                myElement = makeNewElement(namespace, localName, prefix, attributes, context);
            } catch (AxisFault axisFault) {
                throw new SAXException(axisFault);
            }// ww  w .j  a va2 s .  c  o  m
            context.pushNewElement(myElement);
        }
    }
    populateDataHandler(context, namespace, localName, attributes);
}

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

/**
 * Deserializer interface called on each child element encountered in
 * the XML stream.//from   www  . ja v  a  2  s.c o m
 */
public SOAPHandler onStartChild(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {
    if (namespace.equals(Constants.URI_XOP_INCLUDE) && localName.equals(Constants.ELEM_XOP_INCLUDE)) {
        populateDataHandler(context, namespace, localName, attributes);
        return null;
    } else {
        throw new SAXException(Messages.getMessage("noSubElements", namespace + ":" + localName));
    }
}

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

public void startElement(String namespace, String localName, String prefix, Attributes attributes,
        DeserializationContext context) throws SAXException {

    super.startElement(namespace, localName, prefix, attributes, context);

    if (getValue() instanceof DataHandler) {
        try {//from  w w  w.j  a  v a 2 s.co  m
            DataHandler dh = (DataHandler) getValue();
            MimeMultipart mmp = new MimeMultipart(dh.getDataSource());
            if (mmp.getCount() == 0) {
                mmp = null;
            }
            setValue(mmp);
        } catch (Exception e) {
            throw new SAXException(e);
        }
    }
}