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.eclipse.wb.internal.core.databinding.wizards.autobindings.DescriptorContainer.java

/**
 * Parse descriptors XML file./*w w w.j  a  v  a  2 s .  co m*/
 * 
 * @return {@link Map} with all descriptors.
 */
public static Map<String, DescriptorContainer> parseDescriptors(InputStream stream,
        final ClassLoader classLoader, final IImageLoader imageLoader) throws Exception {
    final Map<String, DescriptorContainer> containers = Maps.newHashMap();
    //
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    parser.parse(stream, new DefaultHandler() {
        private DescriptorContainer m_container;
        private AbstractDescriptor m_descriptor;
        private Class<?> m_descriptorClass;

        //
        @Override
        public void startElement(String uri, String localName, String name, Attributes attributes)
                throws SAXException {
            try {
                if ("descriptors".equals(name)) {
                    // create container
                    m_container = new DescriptorContainer();
                    containers.put(attributes.getValue("name"), m_container);
                    m_descriptorClass = classLoader.loadClass(attributes.getValue("class"));
                } else if ("descriptor".equals(name)) {
                    // create descriptor
                    m_descriptor = (AbstractDescriptor) m_descriptorClass.newInstance();
                } else if (m_descriptor != null) {
                    // fill attributes
                    if (attributes.getLength() == 0) {
                        // method without parameters
                        ReflectionUtils.invokeMethod(m_descriptor, name + "()", ArrayUtils.EMPTY_OBJECT_ARRAY);
                    } else {
                        if (name.endsWith("Image")) {
                            // special support for images
                            try {
                                ReflectionUtils.invokeMethod(m_descriptor,
                                        name + "(org.eclipse.swt.graphics.Image)",
                                        new Object[] { imageLoader.getImage(attributes.getValue("value")) });
                            } catch (Throwable e) {
                                DesignerPlugin.log(
                                        "DescriptorContainer: error load image " + attributes.getValue("value"),
                                        e);
                            }
                        } else {
                            // method with single String parameter
                            ReflectionUtils.invokeMethod(m_descriptor, name + "(java.lang.String)",
                                    new Object[] { attributes.getValue("value") });
                        }
                    }
                }
            } catch (Exception e) {
                throw new SAXException("startElement", e);
            }
        }

        @Override
        public void endElement(String uri, String localName, String name) throws SAXException {
            // clear state
            if ("descriptors".equals(name)) {
                m_container = null;
            } else if ("descriptor".equals(name)) {
                m_container.addDescriptor(m_descriptor);
                m_descriptor = null;
            }
        }
    });
    //
    return containers;
}

From source file:org.exist.cocoon.XMLDBSource.java

/**
 * Stream SAX events to a given ContentHandler. If the requested
 * resource is a collection, build an XML view of it.
 *///from   w  w  w .j a  v  a  2 s .c o  m
public void toSAX(ContentHandler handler) throws SAXException {
    try {
        setup();
        if (status == ST_COLLECTION) {
            this.collectionToSAX(handler);
        } else if (status == ST_RESOURCE) {
            this.resourceToSAX(handler);
        } else {
            throw new SourceNotFoundException(getURI());
        }
    } catch (SAXException se) {
        throw se;
    } catch (Exception e) {
        throw new SAXException("Error processing " + getURI(), e);
    } finally {
        cleanup();
    }
}

From source file:org.exist.collections.Collection.java

/** 
 * Validates an XML document et prepares it for further storage. Launches prepare and postValidate triggers.
 * Since the process is dependant from the collection configuration, the collection acquires a write lock during the process.
 * //from   w ww . j av a 2s. c  om
 * @param transaction
 * @param broker
 * @param docUri
 * @param source
 * 
 * @return An {@link IndexInfo} with a write lock on the document. 
 * 
 * @throws EXistException
 * @throws PermissionDeniedException
 * @throws TriggerException
 * @throws SAXException
 * @throws LockException
 */
public IndexInfo validateXMLResource(final Txn transaction, final DBBroker broker, final XmldbURI docUri,
        final InputSource source) throws EXistException, PermissionDeniedException, TriggerException,
        SAXException, LockException, IOException {
    final CollectionConfiguration colconf = getConfiguration(broker);

    return validateXMLResourceInternal(transaction, broker, docUri, colconf, new ValidateBlock() {
        @Override
        public void run(final IndexInfo info) throws SAXException, EXistException {
            final XMLReader reader = getReader(broker, true, colconf);
            info.setReader(reader, null);
            try {

                /*
                 * Note - we must close shield the input source,
                 * else it can be closed by the Reader, so subsequently
                 * when we try and read it in storeXmlInternal we will get
                 * an exception.
                 */
                final InputSource closeShieldedInputSource = closeShieldInputSource(source);

                reader.parse(closeShieldedInputSource);
            } catch (final SAXException e) {
                throw new SAXException("The XML parser reported a problem: " + e.getMessage(), e);
            } catch (final IOException e) {
                throw new EXistException(e);
            } finally {
                releaseReader(broker, info, reader);
            }
        }
    });
}

From source file:org.exist.collections.MutableCollection.java

@Override
public IndexInfo validateXMLResource(final Txn transaction, final DBBroker broker, final XmldbURI name,
        final InputSource source) throws EXistException, PermissionDeniedException, TriggerException,
        SAXException, LockException, IOException {
    final CollectionConfiguration colconf = getConfiguration(broker);

    return validateXMLResourceInternal(transaction, broker, name, colconf, (info) -> {
        final XMLReader reader = getReader(broker, true, colconf);
        info.setReader(reader, null);//from  www.ja  va  2  s.com
        try {

            /*
             * Note - we must close shield the input source,
             * else it can be closed by the Reader, so subsequently
             * when we try and read it in storeXmlInternal we will get
             * an exception.
             */
            final InputSource closeShieldedInputSource = closeShieldInputSource(source);

            reader.parse(closeShieldedInputSource);
        } catch (final SAXException e) {
            throw new SAXException("The XML parser reported a problem: " + e.getMessage(), e);
        } catch (final IOException e) {
            throw new EXistException(e);
        } finally {
            releaseReader(broker, info, reader);
        }
    });
}

From source file:org.fosstrak.epcis.repository.capture.CaptureOperationsModule.java

/**
 * Parses the xml tree for epc nodes and returns a List of BizTransaction
 * URIs with their corresponding type./*from www  . j  a  va  2  s  .  c  om*/
 * 
 * @param bizNode
 *            The parent Node from which BizTransaction URIs should be
 *            extracted.
 * @return A List of BizTransaction.
 * @throws SAXException
 *             If an unknown tag (no &lt;epc&gt;) is encountered.
 */
private List<BusinessTransaction> handleBizTransactions(Session session, Node bizNode) throws SAXException {
    List<BusinessTransaction> bizTransactionList = new ArrayList<BusinessTransaction>();

    for (int i = 0; i < bizNode.getChildNodes().getLength(); i++) {
        Node curNode = bizNode.getChildNodes().item(i);
        if (curNode.getNodeName().equals("bizTransaction")) {

            // Changed by nkef (use "getOrEditVocabularyElement" instead of
            // "getOrInsertVocabularyElement")
            String bizTransTypeUri = curNode.getAttributes().item(0).getTextContent();
            String bizTransUri = curNode.getTextContent();
            BusinessTransactionId bizTrans = (BusinessTransactionId) getOrEditVocabularyElement(session,
                    EpcisConstants.BUSINESS_TRANSACTION_ID, bizTransUri.toString(), "1");
            BusinessTransactionTypeId type = (BusinessTransactionTypeId) getOrEditVocabularyElement(session,
                    EpcisConstants.BUSINESS_TRANSACTION_TYPE_ID, bizTransTypeUri.toString(), "1");

            Criteria c0 = session.createCriteria(BusinessTransaction.class);
            c0.add(Restrictions.eq("bizTransaction", bizTrans));
            c0.add(Restrictions.eq("type", type));
            BusinessTransaction bizTransaction = (BusinessTransaction) c0.uniqueResult();

            if (bizTransaction == null) {
                bizTransaction = new BusinessTransaction();
                bizTransaction.setBizTransaction(bizTrans);
                bizTransaction.setType(type);
                session.save(bizTransaction);
            }

            bizTransactionList.add(bizTransaction);

        } else {
            if (!curNode.getNodeName().equals("#text") && !curNode.getNodeName().equals("#comment")) {
                throw new SAXException("Unknown XML tag: " + curNode.getNodeName(), null);
            }
        }
    }
    return bizTransactionList;
}

From source file:org.h2gis.drivers.osm.OSMParser.java

@Override
public void endDocument() throws SAXException {
    // Execute remaining batch
    try {//from  ww  w .  j  av a 2s . c o  m
        nodePreparedStmtBatchSize = insertBatch(nodePreparedStmt, nodePreparedStmtBatchSize, 1);
        nodeTagPreparedStmtBatchSize = insertBatch(nodeTagPreparedStmt, nodeTagPreparedStmtBatchSize, 1);
        wayPreparedStmtBatchSize = insertBatch(wayPreparedStmt, wayPreparedStmtBatchSize, 1);
        wayTagPreparedStmtBatchSize = insertBatch(wayTagPreparedStmt, wayTagPreparedStmtBatchSize, 1);
        relationPreparedStmtBatchSize = insertBatch(relationPreparedStmt, relationPreparedStmtBatchSize, 1);
        relationTagPreparedStmtBatchSize = insertBatch(relationTagPreparedStmt,
                relationTagPreparedStmtBatchSize, 1);
        nodeMemberPreparedStmtBatchSize = insertBatch(nodeMemberPreparedStmt, nodeMemberPreparedStmtBatchSize,
                1);
        wayMemberPreparedStmtBatchSize = insertBatch(wayMemberPreparedStmt, wayMemberPreparedStmtBatchSize, 1);
        relationMemberPreparedStmtBatchSize = insertBatch(relationMemberPreparedStmt,
                relationMemberPreparedStmtBatchSize, 1);
        wayNodePreparedStmtBatchSize = insertBatch(wayNodePreparedStmt, wayNodePreparedStmtBatchSize, 1);
    } catch (SQLException ex) {
        throw new SAXException("Could not insert sql batch", ex);
    }
}

From source file:org.h2gis.drivers.osm.OSMParser.java

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (localName.compareToIgnoreCase("node") == 0) {
        tagLocation = TAG_LOCATION.OTHER;
        try {/*from  w  ww  .  ja  v a  2 s . c  o  m*/
            nodePreparedStmt.setObject(1, nodeOSMElement.getID());
            nodePreparedStmt.setObject(2, nodeOSMElement.getPoint(gf));
            nodePreparedStmt.setObject(3, nodeOSMElement.getElevation());
            nodePreparedStmt.setObject(4, nodeOSMElement.getUser());
            nodePreparedStmt.setObject(5, nodeOSMElement.getUID());
            nodePreparedStmt.setObject(6, nodeOSMElement.getVisible());
            nodePreparedStmt.setObject(7, nodeOSMElement.getVersion());
            nodePreparedStmt.setObject(8, nodeOSMElement.getChangeSet());
            nodePreparedStmt.setObject(9, nodeOSMElement.getTimeStamp(), Types.DATE);
            nodePreparedStmt.setString(10, nodeOSMElement.getName());
            nodePreparedStmt.addBatch();
            nodePreparedStmtBatchSize++;
            HashMap<String, String> tags = nodeOSMElement.getTags();
            for (Map.Entry<String, String> entry : tags.entrySet()) {
                nodeTagPreparedStmt.setObject(1, nodeOSMElement.getID());
                nodeTagPreparedStmt.setObject(2, entry.getKey());
                nodeTagPreparedStmt.setObject(3, entry.getValue());
                nodeTagPreparedStmt.addBatch();
                nodeTagPreparedStmtBatchSize++;
            }
        } catch (SQLException ex) {
            throw new SAXException("Cannot insert the node  :  " + nodeOSMElement.getID(), ex);
        }
    } else if (localName.compareToIgnoreCase("way") == 0) {
        tagLocation = TAG_LOCATION.OTHER;
        try {
            wayPreparedStmt.setObject(1, wayOSMElement.getID());
            wayPreparedStmt.setObject(2, wayOSMElement.getUser());
            wayPreparedStmt.setObject(3, wayOSMElement.getUID());
            wayPreparedStmt.setObject(4, wayOSMElement.getVisible());
            wayPreparedStmt.setObject(5, wayOSMElement.getVersion());
            wayPreparedStmt.setObject(6, wayOSMElement.getChangeSet());
            wayPreparedStmt.setTimestamp(7, wayOSMElement.getTimeStamp());
            wayPreparedStmt.setString(8, wayOSMElement.getName());
            wayPreparedStmt.addBatch();
            wayPreparedStmtBatchSize++;
            HashMap<String, String> tags = wayOSMElement.getTags();
            for (Map.Entry<String, String> entry : tags.entrySet()) {
                wayTagPreparedStmt.setObject(1, wayOSMElement.getID());
                wayTagPreparedStmt.setObject(2, entry.getKey());
                wayTagPreparedStmt.setObject(3, entry.getValue());
                wayTagPreparedStmt.addBatch();
                wayTagPreparedStmtBatchSize++;
            }
            int order = 1;
            for (long ref : wayOSMElement.getNodesRef()) {
                wayNodePreparedStmt.setObject(1, wayOSMElement.getID());
                wayNodePreparedStmt.setObject(2, ref);
                wayNodePreparedStmt.setObject(3, order++);
                wayNodePreparedStmt.addBatch();
                wayNodePreparedStmtBatchSize++;
            }
        } catch (SQLException ex) {
            throw new SAXException("Cannot insert the way  :  " + wayOSMElement.getID(), ex);
        }
    } else if (localName.compareToIgnoreCase("relation") == 0) {
        tagLocation = TAG_LOCATION.OTHER;
        try {
            relationPreparedStmt.setObject(1, relationOSMElement.getID());
            relationPreparedStmt.setObject(2, relationOSMElement.getUser());
            relationPreparedStmt.setObject(3, relationOSMElement.getUID());
            relationPreparedStmt.setObject(4, relationOSMElement.getVisible());
            relationPreparedStmt.setObject(5, relationOSMElement.getVersion());
            relationPreparedStmt.setObject(6, relationOSMElement.getChangeSet());
            relationPreparedStmt.setTimestamp(7, relationOSMElement.getTimeStamp());
            relationPreparedStmt.addBatch();
            relationPreparedStmtBatchSize++;
            HashMap<String, String> tags = relationOSMElement.getTags();
            for (Map.Entry<String, String> entry : tags.entrySet()) {
                relationTagPreparedStmt.setObject(1, relationOSMElement.getID());
                relationTagPreparedStmt.setObject(2, entry.getKey());
                relationTagPreparedStmt.setObject(3, entry.getValue());
                relationTagPreparedStmt.addBatch();
                relationTagPreparedStmtBatchSize++;
            }
            idMemberOrder = 0;
        } catch (SQLException ex) {
            throw new SAXException("Cannot insert the relation  :  " + relationOSMElement.getID(), ex);
        }
    } else if (localName.compareToIgnoreCase("member") == 0) {
        idMemberOrder++;
    }
    try {
        insertBatch();
    } catch (SQLException ex) {
        throw new SAXException("Could not insert sql batch", ex);
    }
    if (nodeCountProgress++ % readFileSizeEachNode == 0) {
        // Update Progress
        try {
            progress.setStep((int) (((double) fc.position() / fileSize) * 100));
        } catch (IOException ex) {
            // Ignore
        }
    }
}