Search in sources :

Example 41 with JAXBContext

use of javax.xml.bind.JAXBContext in project camel by apache.

the class RecordsUtil method createXMLFile.

public static void createXMLFile() {
    File in = new File("target/in/records.xml");
    if (in.exists()) {
        return;
    } else {
        if (!in.getParentFile().exists() && !in.getParentFile().mkdirs()) {
            throw new RuntimeException("can't create " + in.getParent());
        }
    }
    Records records = new Records();
    for (int i = 0; i < 10; i++) {
        Record record = new Record();
        record.setKey(Integer.toString(i));
        record.setValue("#" + i);
        records.getRecord().add(record);
    }
    Marshaller marshaller;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(Records.class.getPackage().getName());
        marshaller = jaxbCtx.createMarshaller();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    FileWriter writer = null;
    try {
        writer = new FileWriter(in);
        marshaller.marshal(records, writer);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    } finally {
        if (writer != null) {
            try {
                writer.flush();
                writer.close();
            } catch (IOException e) {
            // no-op
            }
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) FileWriter(java.io.FileWriter) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) File(java.io.File)

Example 42 with JAXBContext

use of javax.xml.bind.JAXBContext in project camel by apache.

the class RouteInfoCommand method executeOnRoute.

@Override
public void executeOnRoute(CamelController camelController, String contextName, String routeId, PrintStream out, PrintStream err) throws Exception {
    out.println(stringEscape.unescapeJava("Camel Route " + routeId + ""));
    out.println(stringEscape.unescapeJava("\tCamel Context: " + contextName));
    String xml = camelController.getRouteStatsAsXml(routeId, contextName, true, false);
    if (xml != null) {
        JAXBContext context = JAXBContext.newInstance(RouteStatDump.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        RouteStatDump route = (RouteStatDump) unmarshaller.unmarshal(new StringReader(xml));
        out.println(stringEscape.unescapeJava("\tState: " + route.getState()));
        out.println(stringEscape.unescapeJava("\tState: " + route.getState()));
        out.println("");
        out.println("");
        out.println(stringEscape.unescapeJava("Statistics"));
        long total = route.getExchangesCompleted() + route.getExchangesFailed();
        out.println(stringEscape.unescapeJava("\tExchanges Total: " + total));
        out.println(stringEscape.unescapeJava("\tExchanges Completed: " + route.getExchangesCompleted()));
        out.println(stringEscape.unescapeJava("\tExchanges Failed: " + route.getExchangesFailed()));
        out.println(stringEscape.unescapeJava("\tExchanges Inflight: " + route.getExchangesInflight()));
        out.println(stringEscape.unescapeJava("\tMin Processing Time: " + route.getMinProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tMax Processing Time: " + route.getMaxProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tMean Processing Time: " + route.getMeanProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tTotal Processing Time: " + route.getTotalProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tLast Processing Time: " + route.getLastProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tDelta Processing Time: " + route.getDeltaProcessingTime() + " ms"));
        if (isEmpty(route.getStartTimestamp())) {
            // Print an empty value for scripting
            out.println(stringEscape.unescapeJava("\tStart Statistics Date:"));
        } else {
            Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getStartTimestamp());
            String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
            out.println(stringEscape.unescapeJava("\tStart Statistics Date: " + text));
        }
        // Test for null to see if a any exchanges have been processed first to avoid NPE
        if (isEmpty(route.getResetTimestamp())) {
            // Print an empty value for scripting
            out.println(stringEscape.unescapeJava("\tReset Statistics Date:"));
        } else {
            Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getResetTimestamp());
            String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
            out.println(stringEscape.unescapeJava("\tReset Statistics Date: " + text));
        }
        // Test for null to see if a any exchanges have been processed first to avoid NPE
        if (isEmpty(route.getFirstExchangeCompletedTimestamp())) {
            // Print an empty value for scripting
            out.println(stringEscape.unescapeJava("\tFirst Exchange Date:"));
        } else {
            Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getFirstExchangeCompletedTimestamp());
            String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
            out.println(stringEscape.unescapeJava("\tFirst Exchange Date: " + text));
        }
        // Test for null to see if a any exchanges have been processed first to avoid NPE
        if (isEmpty(route.getLastExchangeCompletedTimestamp())) {
            // Print an empty value for scripting
            out.println(stringEscape.unescapeJava("\tLast Exchange Date:"));
        } else {
            Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getLastExchangeCompletedTimestamp());
            String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
            out.println(stringEscape.unescapeJava("\tLast Exchange Date: " + text));
        }
    }
}
Also used : RouteStatDump(org.apache.camel.util.RouteStatDump) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 43 with JAXBContext

use of javax.xml.bind.JAXBContext in project springside4 by springside.

the class XmlMapper method createMarshaller.

/**
	 * 创建Marshaller并设定encoding(可为null).
	 * 线程不安全,需要每次创建或pooling。
	 */
public static Marshaller createMarshaller(Class clazz, String encoding) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        if (StringUtils.isNotBlank(encoding)) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        }
        return marshaller;
    } catch (JAXBException e) {
        throw ExceptionUtil.unchecked(e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 44 with JAXBContext

use of javax.xml.bind.JAXBContext in project springside4 by springside.

the class XmlMapper method getJaxbContext.

protected static JAXBContext getJaxbContext(Class clazz) {
    Validate.notNull(clazz, "'clazz' must not be null");
    JAXBContext jaxbContext = jaxbContexts.get(clazz);
    if (jaxbContext == null) {
        try {
            jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class);
            jaxbContexts.putIfAbsent(clazz, jaxbContext);
        } catch (JAXBException ex) {
            throw new RuntimeException("Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
        }
    }
    return jaxbContext;
}
Also used : JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 45 with JAXBContext

use of javax.xml.bind.JAXBContext in project midpoint by Evolveum.

the class DescriptorLoader method loadData.

public void loadData(MidPointApplication application) {
    LOGGER.debug("Loading data from descriptor files.");
    try (InputStream baseInput = application.getServletContext().getResourceAsStream(baseFileName);
        InputStream customInput = application.getServletContext().getResourceAsStream(customFileName)) {
        if (baseInput == null) {
            LOGGER.error("Couldn't find " + baseFileName + " file, can't load application menu and other stuff.");
        }
        JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        JAXBElement<DescriptorType> element = (JAXBElement) unmarshaller.unmarshal(baseInput);
        DescriptorType descriptor = element.getValue();
        LOGGER.debug("Loading menu bar from " + baseFileName + " .");
        DescriptorType customDescriptor = null;
        if (customInput != null) {
            element = (JAXBElement) unmarshaller.unmarshal(customInput);
            customDescriptor = element.getValue();
        }
        scanPackagesForPages(descriptor.getPackagesToScan(), application);
        if (customDescriptor != null) {
            scanPackagesForPages(customDescriptor.getPackagesToScan(), application);
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("loaded:\n{}", debugDump(1));
        }
    } catch (Exception ex) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't process application descriptor", ex);
    }
}
Also used : InputStream(java.io.InputStream) DescriptorType(com.evolveum.midpoint.xml.ns._public.gui.admin_1.DescriptorType) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)442 Unmarshaller (javax.xml.bind.Unmarshaller)240 Marshaller (javax.xml.bind.Marshaller)129 JAXBException (javax.xml.bind.JAXBException)128 Test (org.junit.Test)92 InputStream (java.io.InputStream)84 File (java.io.File)50 StringWriter (java.io.StringWriter)47 BaseTest (org.orcid.core.BaseTest)39 V2Convertible (org.orcid.core.version.V2Convertible)39 StringReader (java.io.StringReader)27 IOException (java.io.IOException)26 InputSource (org.xml.sax.InputSource)21 ArrayList (java.util.ArrayList)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)19 JAXBElement (javax.xml.bind.JAXBElement)19 FileOutputStream (java.io.FileOutputStream)18 Schema (javax.xml.validation.Schema)18 SchemaFactory (javax.xml.validation.SchemaFactory)17 SAXSource (javax.xml.transform.sax.SAXSource)14