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(String message, Exception e) 

Source Link

Document

Create a new SAXException from an existing exception.

Usage

From source file:org.apache.tomcat.util.digester.Digester.java

/**
 * Create a SAX exception which also understands about the location in
 * the digester file where the exception occurs
 *
 * @return the new exception//from  www . ja v  a2s.  c  o  m
 */
public SAXException createSAXException(String message, Exception e) {
    if ((e != null) && (e instanceof InvocationTargetException)) {
        Throwable t = ((InvocationTargetException) e).getTargetException();
        if ((t != null) && (t instanceof Exception)) {
            e = (Exception) t;
        }
    }
    if (locator != null) {
        String error = "Error at (" + locator.getLineNumber() + ", " + locator.getColumnNumber() + ": "
                + message;
        if (e != null) {
            return new SAXParseException(error, locator, e);
        } else {
            return new SAXParseException(error, locator);
        }
    }
    log.error("No Locator!");
    if (e != null) {
        return new SAXException(message, e);
    } else {
        return new SAXException(message);
    }
}

From source file:org.apache.torque.engine.database.transform.DTDResolver.java

/**
 * Reads the resource with the given name from the classpath.
 *
 * @param resourceName the name of the resource to read
 * @return an Inputsource witht the content of the resource.
 *
 * @throws SAXException if the resource cannot be read.
 *//* w  w  w. ja va 2 s  .c om*/
private InputSource readFromClasspath(String resourceName) throws SAXException {
    try {
        InputStream dtdStream = getClass().getResourceAsStream(resourceName);

        // getResource was buggy on many systems including Linux,
        // OSX, and some versions of windows in jdk1.3.
        // getResourceAsStream works on linux, maybe others?
        if (dtdStream != null) {
            String pkg = getClass().getName().substring(0, getClass().getName().lastIndexOf('.'));
            log.debug("Resolver: used " + resourceName + " from '" + pkg + "' package");
            return new InputSource(dtdStream);
        } else {
            log.warn("Could not locate database.dtd");
            return null;
        }
    } catch (Exception ex) {
        throw new SAXException("Could not get stream for " + resourceName, ex);
    }
}

From source file:org.apache.torque.generator.configuration.ConfigurationEntityResolver.java

/**
 * Reads the resource with the given name from the classpath.
 *
 * @param resourceName the name of the resource to read
 * @return an <code>InputSource</code> with the content of the resource.
 *
 * @throws SAXException if the resource cannot be read.
 *//* www  .  j  a  v a  2 s . c  o  m*/
private InputSource readFromClasspath(String resourceName) throws SAXException {
    try {
        InputStream inputStream = CLAZZ.getResourceAsStream(resourceName);

        // getResource was buggy on many systems including Linux,
        // OSX, and some versions of windows in jdk1.3.
        // getResourceAsStream works on linux, maybe others?
        if (inputStream != null) {
            String pkg = CLAZZ.getName().substring(0, CLAZZ.getName().lastIndexOf('.'));
            log.debug("Resolver: used " + resourceName + " from '" + pkg + "' package");
            return new InputSource(inputStream);
        } else {
            log.warn("Could not locate resource");
            return null;
        }
    } catch (Exception ex) {
        throw new SAXException("Could not get stream for " + resourceName, ex);
    }
}

From source file:org.apache.torque.generator.configuration.outlet.OutletSaxHandler.java

/**
 * {@inheritDoc}// ww w  .  j a  v  a  2s  .  c  o  m
 */
@Override
public void endElement(String uri, String localName, String rawName) throws SAXException {
    if (mergepointSaxHandler != null) {
        mergepointSaxHandler.endElement(uri, localName, rawName);
        if (mergepointSaxHandler.isFinished()) {
            try {
                outlet.addMergepointMapping(mergepointSaxHandler.getMergepointMapping());
            } catch (ConfigurationException e) {
                throw new SAXException("Could not get mergepoint mapping from the " + "mergepoint Sax handler",
                        e);
            }
            mergepointSaxHandler = null;
        }
    } else if (startTagRawName.equals(rawName)) {
        finished = true;
    }
}

From source file:org.apache.torque.generator.configuration.source.SourceTransformerSaxHandler.java

/**
 * Creates an instance of a transformer.
 *
 * @param className the name of the transformer to be created.
 *
 * @return the instance of the transformer, not null.
 *
 * @throws ExceptionInInitializerError if an error occurs in the
 *        initializer of the transformer.
 * @throws LinkageError if the linkage fails.
 * @throws SAXException if any other  error occurs during instantiation
 *         of the class.// w w  w.  j a  v a  2 s .  c om
 */
private static SourceTransformer createJavaSourceTransformer(String className) throws SAXException {
    Class<?> transformerClass;
    try {
        transformerClass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new SAXException(
                "Error  while initializing a source transformer :" + " Could not load class " + className, e);
    } catch (ExceptionInInitializerError e) {
        log.error("Error  while initializing a source transformer :" + " Could not initialize class "
                + className + " : " + e.getMessage());
        throw e;
    } catch (LinkageError e) {
        log.error("Error  while initializing a source transformer :" + " Could not link class " + className
                + " : " + e.getMessage());
        throw e;
    }

    SourceTransformer result;
    try {
        Constructor<?> constructor = transformerClass.getConstructor();
        result = (SourceTransformer) constructor.newInstance();
    } catch (NoSuchMethodException e) {
        throw new SAXException("Error  while instantiating a source transformer :" + " The class " + className
                + " has no default constructor", e);
    } catch (ClassCastException e) {
        throw new SAXException("Error  while instantiating a source transformer :" + " The class " + className
                + " is not an instance of " + SourceTransformer.class.getName(), e);
    } catch (IllegalAccessException e) {
        throw new SAXException("Error  while instantiating a source transformer :"
                + " The constructor of class " + className + " could not be accessed", e);
    } catch (InvocationTargetException e) {
        throw new SAXException("Error  while instantiating a source transformer :"
                + " The constructor of class " + className + " could not be called", e);
    } catch (InstantiationException e) {
        throw new SAXException("Error  while instantiating a source transformer :" + " The class " + className
                + " represents an abstract class, " + "an interface, an array class, a primitive type, "
                + "or void, or the class has no parameterless constructor, "
                + "or the instantiation fails for some other reason.", e);
    } catch (SecurityException e) {
        throw new SAXException("Error  while instantiating a source transformer :"
                + " The security manager denies instantiation of the class " + className, e);
    }

    return result;
}

From source file:org.dhatim.cdr.XMLConfigDigester.java

private void digestConfigRecursively(Reader stream, String baseURI)
        throws IOException, SAXException, URISyntaxException, SmooksConfigurationException {
    Document configDoc;// w  w w.j  av  a2 s. c  o m
    String streamData = StreamUtils.readStream(stream);

    try {
        configDoc = XmlUtil.parseStream(new StringReader(streamData), getDTDEntityResolver(),
                XmlUtil.VALIDATION_TYPE.DTD, true);
        logger.debug("Using a deprecated Smooks configuration DTD '" + DTD_V10
                + "'.  Update configuration to use XSD '" + XSD_V10 + "'.");
        digestV10DTDValidatedConfig(configDoc);
        logger.debug("Using a deprecated Smooks configuration DTD '" + DTD_V10
                + "'.  Update configuration to use XSD '" + XSD_V10 + "'.");
    } catch (Exception e) {
        // Must be an XSD based config...
        try {
            configDoc = XmlUtil.parseStream(new StringReader(streamData));
        } catch (ParserConfigurationException ee) {
            throw new SAXException("Unable to parse Smooks configuration.", ee);
        }

        XsdDOMValidator validator = new XsdDOMValidator(configDoc);
        String defaultNS = validator.getDefaultNamespace().toString();

        validator.validate();

        configStack.peek().defaultNS = defaultNS;
        if (XSD_V10.equals(defaultNS)) {
            if (validator.getNamespaces().size() > 1) {
                throw new SmooksConfigurationException(
                        "Unsupported use of multiple configuration namespaces from inside a v1.0 Smooks configuration. Configuration extension not supported from a v1.0 configuration.  Use the v1.1 configuration namespace.");
            }
            digestV10XSDValidatedConfig(baseURI, configDoc);
        } else if (XSD_V11.equals(defaultNS)) {
            digestV11XSDValidatedConfig(baseURI, configDoc);
        } else {
            throw new SAXException(
                    "Cannot parse Smooks configuration.  Unsupported default Namespace '" + defaultNS + "'.");
        }
    }

    if (resourcelist.isEmpty()) {
        throw new SAXException(
                "Invalid Content Delivery Resource archive definition file: 0 Content Delivery Resource definitions.");
    }
}

From source file:org.dhatim.cdr.XMLConfigDigester.java

private void digestV10DTDValidatedConfig(Document configDoc) throws SAXException {
    int cdrIndex = 1;
    Element currentElement;//w w w . j  av a2 s.co  m
    String resourceSelector;

    currentElement = (Element) XmlUtil.getNode(configDoc, "/smooks-resource-list");
    String defaultSelector = DomUtils.getAttributeValue(currentElement, "default-selector");
    String defaultNamespace = DomUtils.getAttributeValue(currentElement, "default-namespace");
    String defaultUseragent = DomUtils.getAttributeValue(currentElement, "default-useragent");
    String defaultPath = DomUtils.getAttributeValue(currentElement, "default-path");

    resourceSelector = "/smooks-resource-list/smooks-resource[" + cdrIndex + "]";
    while ((currentElement = (Element) XmlUtil.getNode(configDoc, resourceSelector)) != null) {
        String selector = DomUtils.getAttributeValue(currentElement, "selector");
        String namespace = DomUtils.getAttributeValue(currentElement, "namespace");
        String useragents = DomUtils.getAttributeValue(currentElement, "useragent");
        String path = DomUtils.getAttributeValue(currentElement, "path");
        SmooksResourceConfiguration resourceConfig;

        try {
            resourceConfig = new SmooksResourceConfiguration((selector != null ? selector : defaultSelector),
                    (namespace != null ? namespace : defaultNamespace),
                    (useragents != null ? useragents : defaultUseragent), (path != null ? path : defaultPath));
        } catch (IllegalArgumentException e) {
            throw new SAXException("Invalid unit definition.", e);
        }

        // Add the parameters...
        digestParameters(currentElement, resourceConfig);

        resourcelist.add(resourceConfig);
        if (logger.isDebugEnabled()) {
            logger.debug(
                    "Adding smooks-resource config from [" + resourcelist.getName() + "]: " + resourceConfig);
        }

        cdrIndex++;
        resourceSelector = "/smooks-resource-list/smooks-resource[" + cdrIndex + "]";
    }
}

From source file:org.dhatim.cdr.XMLConfigDigester.java

private void digestResourceConfig(Element configElement, String defaultSelector, String defaultNamespace,
        String defaultProfile, String defaultConditionRef) throws SAXException {
    String selector = DomUtils.getAttributeValue(configElement, "selector");
    String namespace = DomUtils.getAttributeValue(configElement, "selector-namespace");
    String profiles = DomUtils.getAttributeValue(configElement, "target-profile");
    Element resourceElement = getElementByTagName(configElement, "resource");
    Element conditionElement = getElementByTagName(configElement, "condition");
    String resource = null;//from  w w w  . j  a va 2s .  com
    SmooksResourceConfiguration resourceConfig;

    try {
        if (resourceElement != null) {
            resource = DomUtils.getAllText(resourceElement, true);
        }
        resourceConfig = new SmooksResourceConfiguration((selector != null ? selector : defaultSelector),
                (namespace != null ? namespace : defaultNamespace),
                (profiles != null ? profiles : defaultProfile), resource);
        if (resourceElement != null) {
            resourceConfig.setResourceType(DomUtils.getAttributeValue(resourceElement, "type"));
        }

        // And add the condition, if defined...
        if (conditionElement != null) {
            ExpressionEvaluator evaluator = digestCondition(conditionElement);
            resourceConfig.setConditionEvaluator(evaluator);
        } else if (defaultConditionRef != null) {
            ExpressionEvaluator evaluator = getConditionEvaluator(defaultConditionRef);
            resourceConfig.setConditionEvaluator(evaluator);
        }
    } catch (IllegalArgumentException e) {
        throw new SAXException("Invalid unit definition.", e);
    }

    // Add the parameters...
    digestParameters(configElement, resourceConfig);

    resourcelist.add(resourceConfig);
    if (resource == null) {
        if (resourceConfig.getParameters("restype") != null) {
            logger.debug("Resource 'null' for resource config: " + resourceConfig
                    + ".  This is probably an error because the configuration contains a 'resdata' param, which suggests it is following the old DTD based configuration model.  The new model requires the resource to be specified in the <resource> element.");
        } else {
            logger.debug("Resource 'null' for resource config: " + resourceConfig + ". This is not invalid!");
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Adding smooks-resource config from [" + resourcelist.getName() + "]: " + resourceConfig);
    }
}

From source file:org.dhatim.delivery.AbstractParser.java

private Object createHandler(String handlerName) throws SAXException {
    try {/*w ww.  ja  va 2  s .  com*/
        Class handlerClass = ClassUtil.forName(handlerName, getClass());
        return handlerClass.newInstance();
    } catch (ClassNotFoundException e) {
        throw new SAXException("Failed to create SAX Handler '" + handlerName + "'.", e);
    } catch (IllegalAccessException e) {
        throw new SAXException("Failed to create SAX Handler '" + handlerName + "'.", e);
    } catch (InstantiationException e) {
        throw new SAXException("Failed to create SAX Handler '" + handlerName + "'.", e);
    }
}

From source file:org.dhatim.smooks.edi.EDIReader.java

/**
* Get the mapping model associated with the supplied SmooksResourceConfiguration.
* <p/>//from   w  ww .  java  2s .  c  om
* The parsed and validated model are cached in the Smooks container context, keyed
* by the SmooksResourceConfiguration instance.
* @return The Mapping Model.
* @throws IOException Error reading resource configuration data (the mapping model).
* @throws SAXException Error parsing mapping model.
*/
private EdifactModel getMappingModel() throws IOException, SAXException {
    EdifactModel edifactModel;
    Hashtable mappings = getMappingTable(applicationContext);

    synchronized (configuration) {
        edifactModel = (EdifactModel) mappings.get(configuration);
        if (edifactModel == null) {
            try {
                ContainerResourceLocator resourceLocator = applicationContext.getResourceLocator();

                if (modelConfigData.startsWith("urn:") || modelConfigData.endsWith(".jar")
                        || modelConfigData.endsWith(".zip")) {
                    throw new IOException("Unsupported mapping model config URI for basic EDI Parser '"
                            + modelConfigData
                            + "'.  Check that you are using the correct EDI parser.  You may need to configure an Interchange Parser, such as the UN/EDIFACT parser.");
                }

                if (resourceLocator instanceof URIResourceLocator) {
                    // This will resolve config paths relative to the containing smooks config file....
                    edifactModel = EDIParser.parseMappingModel(modelConfigData, (resourceLocator).getBaseURI());
                } else {
                    edifactModel = EDIParser.parseMappingModel(modelConfigData,
                            URIResourceLocator.getSystemBaseURI());
                }
                if (edifactModel == null) {
                    logger.error("Invalid " + MODEL_CONFIG_KEY + " config value '" + modelConfigData
                            + "'. Failed to locate EDI Mapping Model resource!");
                }
            } catch (IOException e) {
                IOException newE = new IOException(
                        "Error parsing EDI mapping model [" + configuration.getStringParameter(MODEL_CONFIG_KEY)
                                + "].  Target Profile(s) " + getTargetProfiles() + ".");
                newE.initCause(e);
                throw newE;
            } catch (SAXException e) {
                throw new SAXException(
                        "Error parsing EDI mapping model [" + configuration.getStringParameter(MODEL_CONFIG_KEY)
                                + "].  Target Profile(s) " + getTargetProfiles() + ".",
                        e);
            } catch (EDIConfigurationException e) {
                throw new SAXException(
                        "Error parsing EDI mapping model [" + configuration.getStringParameter(MODEL_CONFIG_KEY)
                                + "].  Target Profile(s) " + getTargetProfiles() + ".",
                        e);
            }
            mappings.put(configuration, edifactModel);
            logger.debug("Parsed, validated and cached EDI mapping model ["
                    + edifactModel.getEdimap().getDescription().getName() + ", Version "
                    + edifactModel.getEdimap().getDescription().getVersion() + "].  Target Profile(s) "
                    + getTargetProfiles() + ".");
        } else if (logger.isInfoEnabled()) {
            logger.debug("Found EDI mapping model [" + edifactModel.getEdimap().getDescription().getName()
                    + ", Version " + edifactModel.getEdimap().getDescription().getVersion()
                    + "] in the model cache.  Target Profile(s) " + getTargetProfiles() + ".");
        }
    }

    return edifactModel;
}