Search in sources :

Example 51 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project OpenRefine by OpenRefine.

the class XmlImporter method createParserUIInitializationData.

@Override
public JSONObject createParserUIInitializationData(ImportingJob job, List<JSONObject> fileRecords, String format) {
    JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
    try {
        if (fileRecords.size() > 0) {
            JSONObject firstFileRecord = fileRecords.get(0);
            File file = ImportingUtilities.getFile(job, firstFileRecord);
            InputStream is = new FileInputStream(file);
            try {
                XMLStreamReader parser = createXMLStreamReader(is);
                PreviewParsingState state = new PreviewParsingState();
                while (parser.hasNext() && state.tokenCount < PREVIEW_PARSING_LIMIT) {
                    int tokenType = parser.next();
                    state.tokenCount++;
                    if (tokenType == XMLStreamConstants.START_ELEMENT) {
                        JSONObject rootElement = descendElement(parser, state);
                        if (rootElement != null) {
                            JSONUtilities.safePut(options, "dom", rootElement);
                            break;
                        }
                    } else {
                    // ignore everything else
                    }
                }
            } catch (XMLStreamException e) {
                logger.warn("Error generating parser UI initialization data for XML file", e);
            } finally {
                is.close();
            }
        }
    } catch (IOException e) {
        logger.error("Error generating parser UI initialization data for XML file", e);
    }
    return options;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JSONObject(org.json.JSONObject) XMLStreamException(javax.xml.stream.XMLStreamException) PushbackInputStream(java.io.PushbackInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 52 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project wildfly by wildfly.

the class EjbJarParsingDeploymentUnitProcessor method getXMLStreamReader.

/**
     * Creates and returns a {@link XMLStreamReader} for the passed {@link VirtualFile ejb-jar.xml}
     *
     * @param stream    The input stream
     * @param ejbJarXml
     * @return
     * @throws DeploymentUnitProcessingException
     *
     */
private static XMLStreamReader getXMLStreamReader(InputStream stream, VirtualFile ejbJarXml, XMLResolver resolver) throws DeploymentUnitProcessingException {
    try {
        final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setXMLResolver(resolver);
        XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(stream);
        return xmlReader;
    } catch (XMLStreamException xmlse) {
        throw EjbLogger.ROOT_LOGGER.failedToParse(xmlse, "ejb-jar.xml: " + ejbJarXml.getPathName());
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 53 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project wildfly by wildfly.

the class EjbTimerXmlPersister method writeCalendarTimer.

private void writeCalendarTimer(XMLExtendedStreamWriter writer, CalendarTimer timer) throws XMLStreamException {
    String info = null;
    String primaryKey = null;
    if (timer.getInfo() != null) {
        try {
            Marshaller marshaller = factory.createMarshaller(configuration);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            marshaller.start(new OutputStreamByteOutput(out));
            marshaller.writeObject(timer.getInfo());
            marshaller.finish();
            marshaller.flush();
            info = Base64.getEncoder().encodeToString(out.toByteArray());
        } catch (Exception e) {
            EjbLogger.EJB3_TIMER_LOGGER.failedToPersistTimer(timer, e);
            return;
        }
    }
    if (timer.getPrimaryKey() != null) {
        try {
            Marshaller marshaller = factory.createMarshaller(configuration);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            marshaller.start(new OutputStreamByteOutput(out));
            marshaller.writeObject(timer.getPrimaryKey());
            marshaller.finish();
            marshaller.flush();
            primaryKey = Base64.getEncoder().encodeToString(out.toByteArray());
        } catch (Exception e) {
            EjbLogger.EJB3_TIMER_LOGGER.failedToPersistTimer(timer, e);
            return;
        }
    }
    writer.writeStartElement(CALENDAR_TIMER);
    writer.writeAttribute(TIMED_OBJECT_ID, timer.getTimedObjectId());
    writer.writeAttribute(TIMER_ID, timer.getId());
    if (timer.getInitialExpiration() != null) {
        writer.writeAttribute(INITIAL_DATE, Long.toString(timer.getInitialExpiration().getTime()));
    }
    if (timer.getNextExpiration() != null) {
        writer.writeAttribute(NEXT_DATE, Long.toString(timer.getNextExpiration().getTime()));
    }
    writer.writeAttribute(TIMER_STATE, timer.getState().name());
    writer.writeAttribute(SCHEDULE_EXPR_SECOND, timer.getScheduleExpression().getSecond());
    writer.writeAttribute(SCHEDULE_EXPR_MINUTE, timer.getScheduleExpression().getMinute());
    writer.writeAttribute(SCHEDULE_EXPR_HOUR, timer.getScheduleExpression().getHour());
    writer.writeAttribute(SCHEDULE_EXPR_DAY_OF_WEEK, timer.getScheduleExpression().getDayOfWeek());
    writer.writeAttribute(SCHEDULE_EXPR_DAY_OF_MONTH, timer.getScheduleExpression().getDayOfMonth());
    writer.writeAttribute(SCHEDULE_EXPR_MONTH, timer.getScheduleExpression().getMonth());
    writer.writeAttribute(SCHEDULE_EXPR_YEAR, timer.getScheduleExpression().getYear());
    if (timer.getScheduleExpression().getStart() != null) {
        writer.writeAttribute(SCHEDULE_EXPR_START_DATE, Long.toString(timer.getScheduleExpression().getStart().getTime()));
    }
    if (timer.getScheduleExpression().getEnd() != null) {
        writer.writeAttribute(SCHEDULE_EXPR_END_DATE, Long.toString(timer.getScheduleExpression().getEnd().getTime()));
    }
    if (timer.getScheduleExpression().getTimezone() != null) {
        writer.writeAttribute(SCHEDULE_EXPR_TIMEZONE, timer.getScheduleExpression().getTimezone());
    }
    if (info != null) {
        writer.writeStartElement(INFO);
        writer.writeCharacters(info);
        writer.writeEndElement();
    }
    if (primaryKey != null) {
        writer.writeStartElement(PRIMARY_KEY);
        writer.writeCharacters(primaryKey);
        writer.writeEndElement();
    }
    if (timer.isAutoTimer()) {
        writer.writeStartElement(TIMEOUT_METHOD);
        writer.writeAttribute(DECLARING_CLASS, timer.getTimeoutMethod().getDeclaringClass().getName());
        writer.writeAttribute(NAME, timer.getTimeoutMethod().getName());
        for (Class<?> param : timer.getTimeoutMethod().getParameterTypes()) {
            writer.writeStartElement(PARAMETER);
            writer.writeAttribute(TYPE, param.getName());
            writer.writeEndElement();
        }
        writer.writeEndElement();
    }
    writer.writeEndElement();
}
Also used : Marshaller(org.jboss.marshalling.Marshaller) OutputStreamByteOutput(org.jboss.marshalling.OutputStreamByteOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 54 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project wildfly by wildfly.

the class SubsystemOperationsTestCase method testRemoveThreadPool.

@Test
public void testRemoveThreadPool() throws Exception {
    final KernelServices kernelServices = boot(getSubsystemXml("/minimal-subsystem.xml"));
    final ModelNode address = createAddress(BatchSubsystemDefinition.THREAD_POOL_PATH);
    // Remove the thread pool
    final ModelNode removeOp = SubsystemOperations.createRemoveOperation(address);
    executeOperation(kernelServices, removeOp);
    // Reboot with a default thread pool
    String marshalledXml = kernelServices.getPersistedSubsystemXml();
    try {
        boot(marshalledXml);
        Assert.fail("Should be missing <thread-pool/>");
    } catch (XMLStreamException ignore) {
    }
    // Add back a thread-pool, must be named batch
    final ModelNode addOp = SubsystemOperations.createAddOperation(address);
    addOp.get("max-threads").set(10);
    final ModelNode keepAlive = addOp.get("keepalive-time");
    keepAlive.get("time").set(100L);
    keepAlive.get("unit").set(TimeUnit.MILLISECONDS.toString());
    executeOperation(kernelServices, addOp);
    // Get the serialized output and boot
    marshalledXml = kernelServices.getPersistedSubsystemXml();
    try {
        final KernelServices k = boot(marshalledXml);
        Assert.assertTrue(k.isSuccessfulBoot());
    } catch (XMLStreamException e) {
        final StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        Assert.fail("Failed to parse XML; " + writer.toString());
    }
    // Remove and add in a composite operation
    final Operation compositeOp = CompositeOperationBuilder.create().addStep(removeOp).addStep(addOp).build();
    executeOperation(kernelServices, compositeOp);
    // Get the serialized output and boot
    marshalledXml = kernelServices.getPersistedSubsystemXml();
    try {
        final KernelServices k = boot(marshalledXml);
        Assert.assertTrue(k.isSuccessfulBoot());
    } catch (XMLStreamException e) {
        final StringWriter writer = new StringWriter();
        e.printStackTrace(new PrintWriter(writer));
        Assert.fail("Failed to parse XML; " + writer.toString());
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) StringWriter(java.io.StringWriter) KernelServices(org.jboss.as.subsystem.test.KernelServices) Operation(org.jboss.as.controller.client.Operation) ModelNode(org.jboss.dmr.ModelNode) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 55 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project wildfly by wildfly.

the class JBossWebParsingDeploymentProcessor method deploy.

@Override
public void deploy(DeploymentPhaseContext phaseContext) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = phaseContext.getDeploymentUnit();
    if (!DeploymentTypeMarker.isType(DeploymentType.WAR, deploymentUnit)) {
        // Skip non web deployments
        return;
    }
    final VirtualFile deploymentRoot = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT).getRoot();
    final VirtualFile jbossWebXml = deploymentRoot.getChild(JBOSS_WEB_XML);
    WarMetaData warMetaData = deploymentUnit.getAttachment(WarMetaData.ATTACHMENT_KEY);
    assert warMetaData != null;
    if (jbossWebXml.exists()) {
        InputStream is = null;
        try {
            is = jbossWebXml.openStream();
            final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setXMLResolver(NoopXMLResolver.create());
            XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
            final JBossWebMetaData jBossWebMetaData = JBossWebMetaDataParser.parse(xmlReader, JBossDescriptorPropertyReplacement.propertyReplacer(deploymentUnit));
            warMetaData.setJBossWebMetaData(jBossWebMetaData);
            // deployment unit
            if (jBossWebMetaData.getValves() != null) {
                for (ValveMetaData valve : jBossWebMetaData.getValves()) {
                    UndertowLogger.ROOT_LOGGER.unsupportedValveFeature(valve.getValveClass());
                }
            }
            if (jBossWebMetaData.getDistinctName() != null) {
                deploymentUnit.putAttachment(org.jboss.as.ee.structure.Attachments.DISTINCT_NAME, jBossWebMetaData.getDistinctName());
            }
        } catch (XMLStreamException e) {
            throw new DeploymentUnitProcessingException(UndertowLogger.ROOT_LOGGER.failToParseXMLDescriptor(jbossWebXml.toString(), e.getLocation().getLineNumber(), e.getLocation().getColumnNumber()), e);
        } catch (IOException e) {
            throw new DeploymentUnitProcessingException(UndertowLogger.ROOT_LOGGER.failToParseXMLDescriptor(jbossWebXml.toString()), e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    } else {
        //jboss web embedded inside jboss-all.xml
        final JBossWebMetaData jbMeta = deploymentUnit.getAttachment(WebJBossAllParser.ATTACHMENT_KEY);
        if (jbMeta != null) {
            warMetaData.setJBossWebMetaData(jbMeta);
        }
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) JBossWebMetaData(org.jboss.metadata.web.jboss.JBossWebMetaData) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) WarMetaData(org.jboss.as.web.common.WarMetaData) IOException(java.io.IOException) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) ValveMetaData(org.jboss.metadata.web.jboss.ValveMetaData) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)442 XMLStreamReader (javax.xml.stream.XMLStreamReader)137 IOException (java.io.IOException)126 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)75 XMLInputFactory (javax.xml.stream.XMLInputFactory)69 InputStream (java.io.InputStream)66 Document (org.w3c.dom.Document)36 JAXBException (javax.xml.bind.JAXBException)35 Fault (org.apache.cxf.interceptor.Fault)34 Element (org.w3c.dom.Element)32 StringReader (java.io.StringReader)30 XMLEvent (javax.xml.stream.events.XMLEvent)28 DOMSource (javax.xml.transform.dom.DOMSource)27 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ArrayList (java.util.ArrayList)23 QName (javax.xml.namespace.QName)23 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)23 Node (org.w3c.dom.Node)22 StringWriter (java.io.StringWriter)21 XMLEventReader (javax.xml.stream.XMLEventReader)20