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

/**
 * Returns the inner value of the named node in the given document.  The node must only exist once
 *
 * @param doc the XML document// ww w  . j  av  a2 s .  co  m
 * @param nodeName the name of the node
 * @return the value of the named node
 */
public static String getNodeInnerValue(final Document doc, final String nodeName) throws SAXException {
    final NodeList nodes = doc.getElementsByTagName(nodeName);
    if (nodes.getLength() != 1) {
        throw new SAXException("Could not get single node for " + nodeName);
    }
    final NodeList childNodes = nodes.item(0).getChildNodes();
    if (childNodes.getLength() != 1) {
        throw new SAXException("Could not get single child node for " + nodeName);
    }
    return childNodes.item(0).getNodeValue();
}

From source file:Main.java

public static XMLReader createXmlReader() throws SAXException {
    try {/*from  w w w  . j  a  v a  2  s.co  m*/
        // use Xerces to ensure XML 1.1 is handled correctly
        Class<?> clazz = Class.forName("org.apache.xerces.parsers.SAXParser"); //$NON-NLS-1$
        return (XMLReader) clazz.newInstance();
    } catch (Throwable e) {
        SAXParser saxParser;
        try {
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            saxParserFactory.setNamespaceAware(true);
            saxParser = saxParserFactory.newSAXParser();
        } catch (ParserConfigurationException e2) {
            throw new SAXException(e2);
        }
        return saxParser.getXMLReader();
    }
}

From source file:Main.java

/**
 * Returns an attribute value as a int value.
 * @param attributes the Attributes object
 * @param name the name of the attribute
 * @return the attribute value as an int
 * @throws SAXException if the attribute is missing
 *//*from www . j av  a 2s  .c o  m*/
public static int getAttributeAsInt(Attributes attributes, String name) throws SAXException {
    String s = attributes.getValue(name);
    if (s == null) {
        throw new SAXException("Attribute '" + name + "' is missing");
    } else {
        return Integer.parseInt(s);
    }
}

From source file:Main.java

static String getChildNodeValue(Element root, String childNodeName, String defaultValue) throws SAXException {
    NodeList nl = root.getElementsByTagName(childNodeName);
    if (nl.getLength() == 1) {
        return getTextValue(nl.item(0)).trim();
    } else if (defaultValue == null) {
        throw new SAXException("Node <" + root.getNodeName() + "> has no child named <" + childNodeName + ">");
    } else {//  ww  w  . j  a  v a  2 s.co  m
        return defaultValue;
    }
}

From source file:Main.java

public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
    if (qName.equals("order")) {
        System.out.println("here process element start");
    } else {/*from  w w  w  .j a  v  a  2  s.co  m*/
        String location = "";
        if (locator != null) {
            location = locator.getSystemId(); // XML-document name;
            location += " line " + locator.getLineNumber();
            location += ", column " + locator.getColumnNumber();
            location += ": ";
        }
        throw new SAXException(location + "Illegal element");
    }
}

From source file:Main.java

static String getTextValue(Node node) throws SAXException {
    Node textNode = node.getFirstChild();
    if (textNode == null)
        return "";
    if (textNode.getNodeType() != Node.TEXT_NODE)
        throw new SAXException("No text value found for <" + node.getNodeName() + "> node");
    return textNode.getNodeValue();
}

From source file:com.limegroup.gnutella.xml.LimeXMLDocumentFactoryImpl.java

public LimeXMLDocument createLimeXMLDocument(String xml)
        throws SAXException, SchemaNotFoundException, IOException {
    if (xml == null || xml.equals("")) {
        throw new SAXException("null or empty string");
    }/*from   www  .j  a va2  s  .com*/

    InputSource doc = new InputSource(new StringReader(xml));
    XMLParsingUtils.ParseResult result = XMLParsingUtils.parse(doc);
    if (result.isEmpty()) {
        throw new IOException("No element present");
    }
    if (result.schemaURI == null) {
        throw new SchemaNotFoundException("no schema");
    }

    Map<String, String> map = result.get(0);
    DocInfo docInfo = setFields(result.canonicalKeyPrefix, map);
    LimeXMLSchema schema = limeXMLSchemaRepository.get().getSchema(result.schemaURI);
    return createDocument(schema, map, docInfo);
}

From source file:net.ontopia.xml.ContentWriter.java

@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
        throws SAXException {
    try {//from w w  w  . j a v a 2s  .c  o  m
        out.write("<" + localName);
        if (atts != null) {
            for (int i = 0; i < atts.getLength(); i++)
                out.write(" " + atts.getQName(i) + "=\"" + escape(atts.getValue(i)) + "\"");
        }
        out.write('>');
    } catch (IOException e) {
        throw new SAXException(e);
    }

    content = false;
}

From source file:com.xhsoft.framework.common.init.ConfigLoad.java

/**
 * <p>Description:</p>//from  ww w  . j ava  2s . c om
 * @param namespaceURI   
 * @param localName  
 * @param rawName  
 * @param attrs  
 * @return String
 * @author wenzhi
 * @version 1.0
 * @exception SAXException
 */
public void startElement(String namespaceURI, String localName, String rawName, Attributes attrs)
        throws SAXException {
    /** ?config*/
    if (rawName.equals("config")) {
        String name = attrs.getValue("name");
        String value = attrs.getValue("value");
        if (name == null || value == null) {
            throw new SAXException("xml format error");
        }
        configMap.put(name, value);
    }
}

From source file:net.sf.joost.emitter.TextEmitter.java

/**
 * Flushes the output writer /*ww  w .jav  a 2  s  .  c o  m*/
 */
public void endDocument() throws SAXException {
    try {
        writer.flush();
    } catch (IOException ex) {
        if (log != null)
            log.error(ex);
        throw new SAXException(ex);
    }
}