Search in sources :

Example 61 with Marshaller

use of javax.xml.bind.Marshaller in project ORCID-Source by ORCID.

the class MarshallingTest method testMarshallingV2_0.

@Test
public void testMarshallingV2_0() throws JAXBException, IOException, SAXException {
    JAXBContext context = JAXBContext.newInstance("org.orcid.jaxb.model.notification.permission_v2");
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream inputStream = MarshallingTest.class.getResourceAsStream(SAMPLE_PATH_V2);
    org.orcid.jaxb.model.notification.permission_v2.NotificationPermission notification = (org.orcid.jaxb.model.notification.permission_v2.NotificationPermission) unmarshaller.unmarshal(inputStream);
    assertNotNull(notification);
    assertEquals(org.orcid.jaxb.model.notification_v2.NotificationType.PERMISSION, notification.getNotificationType());
    assertEquals(2, notification.getItems().getItems().size());
    assertEquals("2014-01-01T14:45:32", notification.getSentDate().toXMLFormat());
    // Back the other way
    String expected = IOUtils.toString(getClass().getResourceAsStream(SAMPLE_PATH_V2), "UTF-8");
    Pattern pattern = Pattern.compile("<!--.*?-->\\s*", Pattern.DOTALL);
    expected = pattern.matcher(expected).replaceAll("");
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.orcid.org/ns/notification ../notification-permission-2.0.xsd");
    StringWriter writer = new StringWriter();
    marshaller.marshal(notification, writer);
    XMLUnit.setIgnoreWhitespace(true);
    Diff diff = new Diff(expected, writer.toString());
    assertTrue(diff.identical());
}
Also used : Pattern(java.util.regex.Pattern) Marshaller(javax.xml.bind.Marshaller) Diff(org.custommonkey.xmlunit.Diff) InputStream(java.io.InputStream) JAXBContext(javax.xml.bind.JAXBContext) StringWriter(java.io.StringWriter) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 62 with Marshaller

use of javax.xml.bind.Marshaller in project sukija by ahomansikka.

the class JAXBUtil method marshal.

/** Kirjoitetaan XML-dataa.
   *
   * @param value  Data, joka kirjoitetaan.
   * @param contextPath
   * @param out    Paikka, johon kirjoitetaan.
   */
public static final <T> void marshal(JAXBElement<T> value, String contextPath, OutputStream out, ClassLoader classLoader) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(contextPath, classLoader);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(value, out);
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBContext(javax.xml.bind.JAXBContext)

Example 63 with Marshaller

use of javax.xml.bind.Marshaller in project asterixdb by apache.

the class AsterixInstallerIntegrationUtil method initZookeeperTestConfiguration.

private static void initZookeeperTestConfiguration(int port) throws JAXBException, FileNotFoundException {
    String installerConfPath = InstallerDriver.getManagixHome() + File.separator + InstallerDriver.MANAGIX_CONF_XML;
    JAXBContext ctx = JAXBContext.newInstance(Configuration.class);
    Unmarshaller unmarshaller = ctx.createUnmarshaller();
    Configuration configuration = (Configuration) unmarshaller.unmarshal(new File(installerConfPath));
    configuration.getZookeeper().setClientPort(new BigInteger("" + port));
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(configuration, new FileOutputStream(installerConfPath));
}
Also used : Marshaller(javax.xml.bind.Marshaller) Configuration(org.apache.asterix.installer.schema.conf.Configuration) FileOutputStream(java.io.FileOutputStream) BigInteger(java.math.BigInteger) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 64 with Marshaller

use of javax.xml.bind.Marshaller in project sissi by KimShen.

the class JAXBWriter method marshallerPart.

/**
	 * 忽略指定部分
	 * 
	 * @param context
	 * @param part
	 * @param output
	 * @param element
	 * @return
	 * @throws Exception
	 */
private Element marshallerPart(JIDContext context, WriterPart part, OutputStream output, Element element) throws Exception {
    Marshaller marshaller = this.generateMarshaller(true, part == WriterPart.WITHOUT_FIRST ? true : false);
    String content = this.getPart(this.prepareToLines(marshaller, element), part);
    this.log.info("Write on " + context.jid().asString() + " " + content);
    output.write(content.getBytes("UTF-8"));
    return element;
}
Also used : Marshaller(javax.xml.bind.Marshaller)

Example 65 with Marshaller

use of javax.xml.bind.Marshaller in project phoenix by apache.

the class ConfigurationParserTest method writeXML.

/*
        Used for debugging to dump out a simple xml filed based on the bound objects.
     */
private String writeXML() {
    DataModel data = new DataModel();
    try {
        DataValue dataValue = new DataValue();
        dataValue.setDistribution(20);
        dataValue.setValue("jnhgGhHminwiajn");
        List<DataValue> dataValueList = new ArrayList<>();
        dataValueList.add(dataValue);
        Column column = new Column();
        column.setLength(15);
        column.setDataSequence(DataSequence.RANDOM);
        column.setName("TEST_COL");
        column.setUserDefined(true);
        column.setDataValues(dataValueList);
        List<Column> columnList = new ArrayList<>();
        columnList.add(column);
        data.setDataMappingColumns(columnList);
        Scenario scenario = new Scenario();
        scenario.setPhoenixProperties(new HashMap<String, String>());
        scenario.getPhoenixProperties().put("phoenix.query.threadPoolSize", "200");
        scenario.setDataOverride(new DataOverride());
        scenario.setTableName("tableName");
        scenario.setRowCount(10);
        QuerySet querySet = new QuerySet();
        querySet.setExecutionType(ExecutionType.PARALLEL);
        querySet.setExecutionDurationInMs(10000);
        scenario.getQuerySet().add(querySet);
        Query query = new Query();
        querySet.getQuery().add(query);
        querySet.setConcurrency("15");
        querySet.setNumberOfExecutions(20);
        query.setStatement("select * from FHA");
        Scenario scenario2 = new Scenario();
        scenario2.setPhoenixProperties(new HashMap<String, String>());
        scenario2.setDataOverride(new DataOverride());
        scenario2.setTableName("tableName2");
        scenario2.setRowCount(500);
        List<Scenario> scenarios = new ArrayList<Scenario>();
        scenarios.add(scenario);
        scenarios.add(scenario2);
        data.setScenarios(scenarios);
        // create JAXB context and initializing Marshaller
        JAXBContext jaxbContext = JAXBContext.newInstance(DataModel.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // for getting nice formatted output
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        // Writing to console
        jaxbMarshaller.marshal(data, System.out);
    } catch (JAXBException e) {
        // some exception occured
        e.printStackTrace();
    }
    return data.toString();
}
Also used : Marshaller(javax.xml.bind.Marshaller) DataValue(org.apache.phoenix.pherf.rules.DataValue) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext)

Aggregations

Marshaller (javax.xml.bind.Marshaller)280 JAXBContext (javax.xml.bind.JAXBContext)162 JAXBException (javax.xml.bind.JAXBException)100 StringWriter (java.io.StringWriter)82 Test (org.junit.Test)33 JAXBElement (javax.xml.bind.JAXBElement)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 File (java.io.File)29 Unmarshaller (javax.xml.bind.Unmarshaller)21 IOException (java.io.IOException)20 FileOutputStream (java.io.FileOutputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)15 QName (javax.xml.namespace.QName)14 Element (org.w3c.dom.Element)14 HashMap (java.util.HashMap)12 Writer (java.io.Writer)11 Document (org.w3c.dom.Document)11 InputStream (java.io.InputStream)7 OutputStream (java.io.OutputStream)7 ArrayList (java.util.ArrayList)7