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:TestModelBuilder.java

public void endElement(String namespace, String localname, String qname) throws SAXException {
    element = stack.pop();//from w  w  w . j ava  2s  . c o  m
    if (!stack.empty())
        try {
            setProperty(qname, stack.peek(), element);
        } catch (Exception e) {
            throw new SAXException("Error: " + e);
        }
}

From source file:cl.vmetrix.operation.persistence.XmlValidator.java

/**
 * The method validatorXMLstg determinate if the String XML is correct. 
 * @return a boolean (true or false) to indicate if the XML is correct.
 * @param xml is the String XML to validate against XSD Schema
 * @throws SAXException//from   w ww .  ja va  2  s . c  om
 * @throws IOException
 */

public boolean validateXMLstg(String xml) throws SAXException, IOException {
    String rpta = "";

    boolean validation = true;
    try {

        xml = new String(xml.getBytes("UTF-8"));

        //         System.out.println("---> XML in UTF-8: "+ xml);
        // convert String into InputStream
        InputStream is = new ByteArrayInputStream(xml.getBytes());

        Source xmlFile = new StreamSource(is);//new File("C:/XML/424437.xml"));

        // XSD schema
        String schemea = getFileSchema("operationSchema.xsd");

        InputStream sch = new ByteArrayInputStream(schemea.getBytes());

        Source schemaFile = new StreamSource(sch);//new File("main/resources/Operation.xsd"));

        // Preparing the schema
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaFile);

        // Validator creation
        Validator validator = schema.newValidator();

        // DException handle of validator
        final List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
        validator.setErrorHandler(new ErrorHandler() {
            @Override
            public void warning(SAXParseException exception) throws SAXException {
                exceptions.add(exception);
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                exceptions.add(exception);
            }

            @Override
            public void error(SAXParseException exception) throws SAXException {
                exceptions.add(exception);
            }
        });

        // XML validation
        validator.validate(xmlFile);

        // Validation result. If there are errors, detailed the exact position in the XML  and the error
        if (exceptions.size() == 0) {
            rpta = "XML IS VALID";
        } else {
            validation = false;
            StringBuffer sb = new StringBuffer();
            sb.append("XML IS INVALID");
            sb.append("\n");
            sb.append("NUMBER OF ERRORS: " + exceptions.size());
            sb.append("\n");
            for (int i = 0; i < exceptions.size(); i++) {
                i = i + 1;
                sb.append("Error # " + i + ":");
                sb.append("\n");
                i = i - 1;
                sb.append("    - Line: " + ((SAXParseException) exceptions.get(i)).getLineNumber());
                sb.append("\n");
                sb.append("    - Column: " + ((SAXParseException) exceptions.get(i)).getColumnNumber());
                sb.append("\n");
                sb.append("    - Error message: " + ((Throwable) exceptions.get(i)).getLocalizedMessage());
                sb.append("\n");
                sb.append("------------------------------");
            }
            rpta = sb.toString();
            logger.debug(rpta);

        }
    } catch (SAXException e) {
        logger.error("SAXException in XML validator: ", e);
        logger.debug(rpta);
        throw new SAXException(e);
    } catch (IOException e) {
        logger.error("IOException in XML validator: ", e);
        logger.debug(rpta);
        throw new IOException(e);
    }

    return validation;
}

From source file:cz.muni.fi.japanesejmdictsaxparser.saxholder.SaxKanjidic2Holder.java

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    if (mCanceled) {
        throw new SAXException("SAX terminated due to ParserService end.");
    }/*w  w  w .j a v  a 2s  .co m*/
    switch (qName) {

    case "character":
        mDoc = new Document();
        mValueDicRef = new JSONObject();
        mValueRmGroupJaOn = new JSONArray();
        mValueRmGroupJaKun = new JSONArray();
        mValueMeaningEnglish = new JSONArray();
        mValueMeaningFrench = new JSONArray();
        /*
         *  dutch and german aren't in current kanjidict 2
         */
        mValueMeaningDutch = new JSONArray();
        mValueMeaningGerman = new JSONArray();

        mValueNanori = new JSONArray();
        break;
    case "literal":
        mLiteral = true;
        break;
    case "rad_value":
        if ("classical".equals(attributes.getValue("rad_type"))) {
            mRadicalClassic = true;
        }
        break;
    case "grade":
        mGrade = true;
        break;
    case "stroke_count":
        mStrokeCount = true;
        break;
    case "dic_ref":
        mDicRef = true;
        mDicRefKey = attributes.getValue("dr_type");
    case "q_code":
        if ("skip".equals(attributes.getValue("qc_type"))) {
            mQueryCodeSkip = true;
        }
        break;
    case "reading":
        switch (attributes.getValue("r_type")) {
        case "ja_on":
            mRMGroupJaOn = true;
            break;
        case "ja_kun":
            mRMGroupJaKun = true;
            break;
        }
        break;
    case "meaning":
        if (attributes.getValue("m_lang") != null) {
            switch (attributes.getValue("m_lang")) {
            case "fr":
                mMeaningFrench = true;
                break;
            case "du":
                mMeaningDutch = true;
                break;
            case "ge":
                mMeaningGerman = true;
                break;
            default:
                mMeaningEnglish = true;
            }
        } else {
            mMeaningEnglish = true;
        }
        break;
    case "nanori":
        mNanori = true;
        break;
    }
    super.startElement(uri, localName, qName, attributes);
}

From source file:net.i2cat.netconf.transport.TransportContentParser.java

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException {
    super.startElement(uri, localName, qName, attributes);

    // if (insideDataTag && !localName.equalsIgnoreCase("data"))
    // return;/*from w  ww.  ja v a  2  s .c o  m*/

    if (insideDataTag) {
        dataTagContent.append("<" + localName + ">");
    }

    else if (insideSoftwareInfoTag) {
        softwareInfoTagContent.append("<" + localName + ">");
    }

    // log.debug("startElement <" + uri + "::" + localName + ">");

    if (localName.equalsIgnoreCase("hello")) {
        hello = new Hello();
        capabilities = new ArrayList<Capability>();
    }
    // if (localName.equalsIgnoreCase("capabilities")) {
    // insideCapabilityTag = false;
    // }
    else if (localName.equalsIgnoreCase("capability")) {
        insideCapabilityTag = true;
    } else if (localName.equalsIgnoreCase("session-id")) {
        insideSessionIdTag = true;
    } else if (localName.equalsIgnoreCase("rpc-reply")) {
        reply = new Reply();

        messageId = attributes.getValue("message-id");
        if (messageId == null)
            throw new SAXException(
                    new NetconfProtocolException("Received <rpc-reply> message without a messageId"));

        reply.setMessageId(messageId);
        reply.setOk(false); // defaults to false

        insideRPCReplyTag = true;
    } else if (localName.equalsIgnoreCase("data")) {
        insideDataTag = true;
    } else if (localName.equalsIgnoreCase("ok")) {
        reply.setOk(true);
    } else if (localName.equalsIgnoreCase("rpc-error")) {
        error = new Error();
    } else if (localName.equalsIgnoreCase("error-type")) {
        insideErrorTypeTag = true;
    } else if (localName.equalsIgnoreCase("error-tag")) {
        insideErrorTagTag = true;
    } else if (localName.equalsIgnoreCase("error-severity")) {
        insideErrorSeverityTag = true;
    } else if (localName.equalsIgnoreCase("error-app-tag")) {
        insideErrorAppTagTag = true;
    } else if (localName.equalsIgnoreCase("error-path")) {
        insideErrorPathTag = true;
    } else if (localName.equalsIgnoreCase("error-message")) {
        insideErrorMessageTag = true;
    } else if (localName.equalsIgnoreCase("error-info")) {
        insideErrorInfoTag = true;
    }

    /* extra features from JUNOS (out RFC) */
    else if (localName.equalsIgnoreCase("interface-information")) {
        insideInterfaceInfoTag = true;
    } else if (localName.equalsIgnoreCase("software-information")) {
        // software-information is the root node and leaving it in place
        // makes gives us a well-formed XML document rather than multiple
        // top-level nodes.
        softwareInfoTagContent.append("<" + localName + ">");
        insideSoftwareInfoTag = true;
    }
    /* any other under <rpc-reply> tag */
    else if (insideRPCReplyTag) {
        insideRPCReplyTag = false;
        underRPCReplyTagName = localName;
        underRPCReplyTagContent.append("<" + localName + ">");
        insideUnderRPCReplyTag = true;
    } else if (insideUnderRPCReplyTag) {
        underRPCReplyTagContent.append("<" + localName + ">");
    }

}

From source file:org.callimachusproject.xml.XdmNodeFactory.java

private XdmNode parse(Source source) throws SAXException {
    try {//from   w w  w .j  a v  a2  s .  co  m
        DocumentBuilder builder = processor.newDocumentBuilder();
        builder.setLineNumbering(true);
        builder.setDTDValidation(false);
        if (source.getSystemId() != null) {
            builder.setBaseURI(URI.create(source.getSystemId()));
        }
        return builder.build(source);
    } catch (SaxonApiException e) {
        throw new SAXException(e);
    }
}

From source file:eu.annocultor.converter.ConverterHandler.java

public void multiFileEndDocument() throws SAXException {
    super.endDocument();
    if (!passedARecord) {
        throw new SAXException("No single record was passed: an error in the record tag");
    }//  w w  w. j  a v a 2 s  . c o  m
}

From source file:net.sf.joost.stx.TransformerHandlerResolverImpl.java

/**
 * Initialize the object/*from  ww  w  .j a v  a2 s .com*/
 * It scans plugins directories and create a hashtable of all implemented
 * filter-methods and their factories.
 * In case of duplicated method implementations its behaviour is
 * defined by {link @flgName} system property.
 * @throws SAXException when duplicated method implementation is found
 * and has been asked to raise an exception
 */
private static synchronized void init() throws SAXException {

    // check again, in case this method has been called twice
    if (!notInitializedYet)
        return;

    if (DEBUG)
        log.debug("init() : entering");

    // system property which says what to do in case of
    // duplicated method implementations
    String prop = System.getProperty(flgName);
    if (DEBUG)
        log.debug(flgName + "=" + prop);
    int flg;
    // fail with exception if duplicate is found
    if ("fail".equalsIgnoreCase(prop))
        flg = FLAG_FAIL;
    // ignore duplicate and print info message
    else if ("ignore".equalsIgnoreCase(prop))
        flg = FLAG_IGNORE;
    // accept duplicate and print warning message
    else
        // just a warning and replace
        flg = FLAG_REPLACE;

    // plugin classes
    Enumeration clss = Service.providers(TransformerHandlerResolver.class);

    // loop over founded classes
    while (clss.hasMoreElements()) {
        TransformerHandlerResolver plg = (TransformerHandlerResolver) clss.nextElement();
        if (DEBUG)
            log.debug("scanning implemented stx-filter-methods of class" + plg.getClass());

        // lookup over implemented methods
        String[] uriMethods = plg.resolves();
        for (int i = 0; i < uriMethods.length; i++) {

            // method name (url)
            String mt = uriMethods[i];

            if (DEBUG)
                log.debug("stx-filter-method found : " + mt);

            // see if method is already defined by some other plugin ?
            TransformerHandlerResolver firstPlg = (TransformerHandlerResolver) plugins.get(mt);

            if (null != firstPlg) {
                String msg = "Plugin '" + plg.getClass() + "' implements stx-filter-method '" + mt
                        + "' which already has been implemented by '" + firstPlg.getClass().toString() + "'!";
                if (flg == FLAG_FAIL) {
                    if (DEBUG)
                        log.debug("plugin already implemented!");
                    throw new SAXException(msg);
                } else if (flg == FLAG_IGNORE) {
                    if (log != null)
                        log.warn(msg + "\nImplementation ignored, " + "using first plugin!");
                } else { // replace + warning
                    if (log != null)
                        log.warn(msg + "\nUsing new implementation, " + "previous plugin ignored!");
                    plugins.put(mt, plg);
                }
            } else {
                // add method to the hashtable
                plugins.put(mt, plg);
            }
        }

    }

    // revert init() flag
    notInitializedYet = false;

    if (DEBUG)
        log.debug("init() : exiting");
}

From source file:com.amalto.workbench.editors.XSDDriver.java

private void error(String message) {
    eh.printException(new SAXException(message));
}

From source file:gov.nih.nci.cagrid.encoding.AxisContentHandler.java

/**
 * delegates to the serialization context
 *///from w w w  .  j a  v  a  2  s . c om
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException {
    try {
        context.writeChars(ch, start, length);
    } catch (IOException ioe) {
        throw new SAXException(ioe);
    }
}

From source file:com.amalto.core.load.payload.FlushXMLReader.java

public void parse(String systemId) throws IOException, SAXException {
    throw new SAXException("Not supported");
}