@Test public void oneMarshallerCanHaveMultipleMediaTypes() throws Exception { Unmarshaller<HttpEntity, String> xmlUnmarshaller = Unmarshaller.forMediaTypes( Arrays.asList(MediaTypes.APPLICATION_XML, MediaTypes.TEXT_XML), Unmarshaller.entityToString()) .thenApply((str) -> "xml"); { CompletionStage<String> resultStage = xmlUnmarshaller.unmarshall( HttpEntities.create(ContentTypes.TEXT_XML_UTF8, "<suchXml/>"), system().dispatcher(), materializer()); assertEquals("xml", resultStage.toCompletableFuture().get(3, TimeUnit.SECONDS)); } { CompletionStage<String> resultStage = xmlUnmarshaller.unmarshall( HttpEntities.create( ContentTypes.create(MediaTypes.APPLICATION_XML, HttpCharsets.UTF_8), "<suchXml/>"), system().dispatcher(), materializer()); assertEquals("xml", resultStage.toCompletableFuture().get(3, TimeUnit.SECONDS)); } }
public void resolve(Unmarshaller u) throws JAXBException { Object obj = null; try { obj = u.unmarshal(new URL(getSchemaLocation())); } catch (MalformedURLException e) { } File file = new File(getSchemaLocation()); if (!file.exists()) { if (!file.getName().startsWith("/")) { file = new File(_location.getSystemId()); file = file.getParentFile(); if (file == null) throw new JAXBException("File not found: " + getSchemaLocation()); file = new File(file, getSchemaLocation()); } } obj = u.unmarshal(file); if (obj instanceof Schema) _schema = (Schema) obj; }
private static void readXmlGenerated() throws JAXBException { // jaxbContext = JAXBContext.newInstance(Characters.class); // file = new // File("E:\\Development\\Java\\GameGenerator\\xml\\Characters.xml"); // unmarshaller = jaxbContext.createUnmarshaller(); // characters = (Characters) unmarshaller.unmarshal(file); // // jaxbContext = JAXBContext.newInstance(Subject.class); // file = new // File("E:\\Development\\Java\\GameGenerator\\xml\\Subject.xml"); // unmarshaller = jaxbContext.createUnmarshaller(); // subject = (Subject) unmarshaller.unmarshal(file); JAXBContext jaxbContext = JAXBContext.newInstance(Theme.class); File file = new File("ThemeOut.xml"); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); theme = (Theme) unmarshaller.unmarshal(file); // jaxbContext = JAXBContext.newInstance(Locale.class); // file = new // File("E:\\Development\\Java\\GameGenerator\\xml\\LocaleOut.xml"); // unmarshaller = jaxbContext.createUnmarshaller(); // locale = (Locale) unmarshaller.unmarshal(file); // // jaxbContext = JAXBContext.newInstance(LearningAct.class); // file = new // File("E:\\Development\\Java\\GameGenerator\\xml\\LearningObjectiveOut.xml"); // unmarshaller = jaxbContext.createUnmarshaller(); // learningAct = (LearningAct) unmarshaller.unmarshal(file); System.out.println(); }
@Test public void shouldParseAllNumbers() throws IOException { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeBits(5, 8); writer.writeBits(200, 8); writer.writeBits(1000, 16); writer.writeBits(50000, 16); writer.writeBits(5_000_000L, 24); writer.writeBits(10_000_000L, 24); writer.writeInt(-100000); writer.writeUnsignedInt(3_000_000_000L); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(20)); Unmarshaller parser = dadlContext.createUnmarshaller(); AllNumbers an = parser.unmarshal(bytes, AllNumbers.class); assertThat(an, is(notNullValue())); ShortNumbers sn = an.getShortNumbers(); assertThat(sn, is(notNullValue())); assertThat(sn.getI8(), is(5)); assertThat(sn.getU8(), is(200)); assertThat(sn.getI16(), is(1000)); assertThat(sn.getU16(), is(50000)); LongNumbers ln = an.getLongNumbers(); assertThat(ln, is(notNullValue())); assertThat(ln.getI24(), is(5_000_000)); assertThat(ln.getU24(), is(10_000_000)); assertThat(ln.getI32(), is(-100_000)); assertThat(ln.getU32(), is(3_000_000_000L)); }
@Test public void shouldUnmarshalDecimalNumbers() throws Exception { String marshalled = "005612"; Unmarshaller parser = dadlContext.createUnmarshaller(); DecimalNumbers numbers = parser.unmarshal(marshalled.getBytes(), DecimalNumbers.class); assertThat(numbers, is(notNullValue())); assertThat(numbers.getD1(), is(56)); assertThat(numbers.getD2(), is(12)); }
/** * Takes input XML file and unmarshalls it * * @throws JAXBException, FileNotFoundException, SiriusException * @returns Scenario */ public edu.berkeley.path.beats.jaxb.Scenario readAndUnmarshallXML() throws JAXBException, FileNotFoundException, BeatsException { JAXBContext jaxbContext = JAXBContext.newInstance("edu.berkeley.path.beats.jaxb"); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); javax.xml.validation.Schema schema = this.getSchema(); jaxbUnmarshaller.setSchema(schema); edu.berkeley.path.beats.simulator.ObjectFactory.setObjectFactory( jaxbUnmarshaller, new JaxbObjectFactory()); Scenario scenario = (Scenario) jaxbUnmarshaller.unmarshal(new FileInputStream(this.inputFileName)); return scenario; }
public void testJSONOutputStreamUTF8() throws Exception { marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); FlushRoot control = getControlObject(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(control, baos); XMLInputFactory xif = XMLInputFactory.newFactory(); Object test = unmarshaller.unmarshal(new ByteArrayInputStream(baos.toByteArray())); assertEquals(control, test); }
@Test public void canChooseOneOfManyUnmarshallers() throws Exception { Unmarshaller<HttpEntity, String> jsonUnmarshaller = Unmarshaller.forMediaType(MediaTypes.APPLICATION_JSON, Unmarshaller.entityToString()) .thenApply((str) -> "json"); Unmarshaller<HttpEntity, String> xmlUnmarshaller = Unmarshaller.forMediaType(MediaTypes.TEXT_XML, Unmarshaller.entityToString()) .thenApply((str) -> "xml"); final Unmarshaller<HttpEntity, String> both = Unmarshaller.firstOf(jsonUnmarshaller, xmlUnmarshaller); { CompletionStage<String> resultStage = both.unmarshall( HttpEntities.create(ContentTypes.TEXT_XML_UTF8, "<suchXml/>"), system().dispatcher(), materializer()); assertEquals("xml", resultStage.toCompletableFuture().get(3, TimeUnit.SECONDS)); } { CompletionStage<String> resultStage = both.unmarshall( HttpEntities.create(ContentTypes.APPLICATION_JSON, "{}"), system().dispatcher(), materializer()); assertEquals("json", resultStage.toCompletableFuture().get(3, TimeUnit.SECONDS)); } }
@Test public void shouldUnmarshalNumberWithColour() throws Exception { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeByte(22); writer.writeByte(14); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(2)); Unmarshaller parser = dadlContext.createUnmarshaller(); NumberWithColour nwc = parser.unmarshal(bytes, NumberWithColour.class); assertThat(nwc, is(notNullValue())); assertThat(nwc.getI1(), is(22)); assertThat(nwc.getC(), is(Colour.YELLOW)); }
/** @param reader */ public static com.cisco.eManager.common.process.GetProcessStatusForResp unmarshal( java.io.Reader reader) throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException { return (com.cisco.eManager.common.process.GetProcessStatusForResp) Unmarshaller.unmarshal( com.cisco.eManager.common.process.GetProcessStatusForResp.class, reader); } // -- com.cisco.eManager.common.process.GetProcessStatusForResp unmarshal(java.io.Reader)
@Test public void shouldUnmarshalParsedArray() throws Exception { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeInt(16); writer.writeInt(25); writer.writeInt(36); writer.writeInt(49); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(16)); Unmarshaller unmarshaller = dadlContext.createUnmarshaller(); ParsedNumberList numberList = unmarshaller.unmarshal(writer.toByteArray(), ParsedNumberList.class); assertThat(numberList.getItems(), contains(16, 25, 36, 49)); }
/** @param reader */ public static org.cocons.uml.ccl.ccldata.CoConSetConditionQuerySingleValue unmarshal( java.io.Reader reader) throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException { return (org.cocons.uml.ccl.ccldata.CoConSetConditionQuerySingleValue) Unmarshaller.unmarshal( org.cocons.uml.ccl.ccldata.CoConSetConditionQuerySingleValue.class, reader); } // -- org.cocons.uml.ccl.ccldata.CoConSetConditionQuerySingleValue unmarshal(java.io.Reader)
/** @param reader */ public static com.cisco.eManager.common.event2.EventUnacknowledgementNotificationMsg unmarshal( java.io.Reader reader) throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException { return (com.cisco.eManager.common.event2.EventUnacknowledgementNotificationMsg) Unmarshaller.unmarshal( com.cisco.eManager.common.event2.EventUnacknowledgementNotificationMsg.class, reader); } // -- com.cisco.eManager.common.event2.EventUnacknowledgementNotificationMsg
/** Used to retrieve the root document bound through JAXB */ private static void getRootDocument() { if (rDocument == null) { try { JAXBContext jaxbCtxt = JAXBContext.newInstance("odin.odin.xml"); Unmarshaller jaxbU = jaxbCtxt.createUnmarshaller(); jaxbU.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler()); File fRoot = new File("home.root.xml"); JAXBElement<Root> rElement = jaxbU.unmarshal(new StreamSource(fRoot), Root.class); rDocument = rElement.getValue(); } catch (JAXBException jaxbX) { System.err.println( "An error was caught while trying to read information from the server's root information document."); jaxbX.printStackTrace(); } } }
private static void test() throws JAXBException { Scene scene = new Scene(); List<Screen> screens = new ArrayList<Screen>(); // scene.setScreen(screens); JAXBContext jaxbContext = JAXBContext.newInstance(Scene.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); File file = new File("Scene.xml"); marshaller.marshal(scene, file); file = new File("Scene.xml"); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Scene scene2 = (Scene) unmarshaller.unmarshal(file); System.out.println(scene2); }
@Test public void shouldUnmarshalBcdSequence() throws Exception { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeShort(4096); writer.writeBits(1, 4); writer.writeBits(2, 4); writer.writeBits(3, 4); writer.writeBits(4, 4); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(4)); Unmarshaller unmarshaller = dadlContext.createUnmarshaller(); BcdSequence bcdSequence = unmarshaller.unmarshal(bytes, BcdSequence.class); assertThat(bcdSequence.getI16(), is(4096)); assertThat(bcdSequence.getBcd(), is(1234)); }
@Test public void shouldUnmarshalTaggedString() throws Exception { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); String text = "Hello DADL!"; writer.writeByte(0x0A); writer.writeByte(2 + text.length()); writer.writeByte(22); writer.writeByte(14); writer.writeBytes(text); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(15)); Unmarshaller unmarshaller = dadlContext.createUnmarshaller(); TaggedString taggedString = unmarshaller.unmarshal(bytes, TaggedString.class); assertThat(taggedString.getText(), is(text)); }
@Test public void shouldUnmarshalChoiceWithDiscriminator() throws Exception { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeBits(40, 8); writer.writeBits(42, 24); writer.writeBits(12345678, 32); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(8)); Unmarshaller parser = dadlContext.createUnmarshaller(); ChoiceWithDiscriminator choice = parser.unmarshal(bytes, ChoiceWithDiscriminator.class); assertThat(choice, is(notNullValue())); assertThat(choice.getOpt3(), is(nullValue())); assertThat(choice.getOpt4(), is(notNullValue())); assertThat(choice.getOpt4().getI41(), is(42)); assertThat(choice.getOpt4().getI42(), is(12345678)); }
@Test public void shouldUnmarshalBitField() throws Exception { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeBits(3, 2); writer.writeBits(6, 3); writer.writeBits(11, 4); writer.writeBits(125, 7); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(2)); Unmarshaller unmarshaller = dadlContext.createUnmarshaller(); BitField bitField = unmarshaller.unmarshal(bytes, BitField.class); assertThat(bitField.getB2(), is(3)); assertThat(bitField.getB3(), is(6)); assertThat(bitField.getB4(), is(11)); assertThat(bitField.getB7(), is(125)); }
/** * TDTEngine - constructor for a new Tag Data Translation engine * * @param confdir the string value of the path to a configuration directory consisting of two * subdirectories, <code>schemes</code> and <code>auxiliary</code>. * <p>When the class TDTEngine is constructed, the path to a local directory must be * specified, by passing it as a single string parameter to the constructor method (without * any trailing slash or file separator). e.g. <code> * <pre>TDTEngine engine = new TDTEngine("/opt/TDT");</pre></code> * <p>The specified directory must contain two subdirectories named auxiliary and schemes. The * Tag Data Translation definition files for the various coding schemes should be located * inside the subdirectory called <code>schemes</code>. Any auxiliary lookup files (such as * <code>ManagerTranslation.xml</code>) should be located inside the subdirectory called * <code>auxiliary</code>. * <p>Files within the schemes directory ending in <code>.xml</code> are read in and * unmarshalled using <a href = "http://www.castor.org">Castor</a>. */ public TDTEngine(String confdir) throws FileNotFoundException, MarshalException, ValidationException, TDTException { xmldir = confdir; long t = System.currentTimeMillis(); File[] dir = new java.io.File(confdir + File.separator + "schemes").listFiles(new XMLFilenameFilter()); // java.util.Arrays.sort(dir); // Sort it if (dir == null) throw new TDTException("Cannot find schemes in " + confdir); Unmarshaller unmar = new Unmarshaller(); for (File f : dir) { EpcTagDataTranslation tdt = (EpcTagDataTranslation) unmar.unmarshal(EpcTagDataTranslation.class, new FileReader(f)); initFromTDT(tdt); } // need to populate the hashmap gs1cpi from the ManagerTranslation.xml table in auxiliary. // Unmarshaller unmar = new Unmarshaller(); GEPC64Table cpilookup = (GEPC64Table) unmar.unmarshal( GEPC64Table.class, new FileReader( confdir + File.separator + "auxiliary" + File.separator + "ManagerTranslation.xml")); for (Enumeration e = cpilookup.enumerateEntry(); e.hasMoreElements(); ) { Entry entry = (Entry) e.nextElement(); String comp = entry.getCompanyPrefix(); String indx = Integer.toString(entry.getIndex()); gs1cpi.put(indx, comp); gs1cpi.put(comp, indx); } // System.out.println("Loaded schemas in " + // (System.currentTimeMillis() - t) // + " millisecs"); }
@Test public void shouldUnmarshalSequenceWithOptionalElements() throws IOException { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeBits(0x0B, 8); writer.writeBits(7, 8); writer.writeBits(42, 24); writer.writeBits(12345678, 32); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(9)); Unmarshaller parser = dadlContext.createUnmarshaller(); SequenceWithOptional choice = parser.unmarshal(bytes, SequenceWithOptional.class); assertThat(choice, is(notNullValue())); assertThat(choice.getOpt1(), is(nullValue())); assertThat(choice.getOpt2(), is(notNullValue())); assertThat(choice.getOpt2().getI21(), is(42)); assertThat(choice.getOpt2().getI22(), is(12345678)); }
public void testOutputStreamUTF8() throws Exception { FlushRoot control = getControlObject(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(control, baos); XMLInputFactory xif = XMLInputFactory.newFactory(); Object test = unmarshaller.unmarshal(new ByteArrayInputStream(baos.toByteArray())); assertEquals(control, test); }
private static void readXml() throws JAXBException { JAXBContext jaxbContext = null; File file = null; Unmarshaller unmarshaller = null; jaxbContext = JAXBContext.newInstance(Characters.class); file = new File("Characters.xml"); unmarshaller = jaxbContext.createUnmarshaller(); characters = (Characters) unmarshaller.unmarshal(file); jaxbContext = JAXBContext.newInstance(Subject.class); file = new File("Subject.xml"); unmarshaller = jaxbContext.createUnmarshaller(); subject = (Subject) unmarshaller.unmarshal(file); jaxbContext = JAXBContext.newInstance(Theme.class); file = new File("Theme.xml"); unmarshaller = jaxbContext.createUnmarshaller(); theme = (Theme) unmarshaller.unmarshal(file); jaxbContext = JAXBContext.newInstance(Locale.class); file = new File("Locale.xml"); unmarshaller = jaxbContext.createUnmarshaller(); locale = (Locale) unmarshaller.unmarshal(file); jaxbContext = JAXBContext.newInstance(LearningAct.class); file = new File("LearningAct.xml"); unmarshaller = jaxbContext.createUnmarshaller(); learningAct = (LearningAct) unmarshaller.unmarshal(file); System.out.println(); }
@Test public void shouldUnmarshalOpaqueContainer() throws Exception { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeInt(4); writer.writeByte(20); writer.writeByte(22); writer.writeByte(24); writer.writeByte(26); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(8)); Unmarshaller parser = dadlContext.createUnmarshaller(); OpaqueContainer oc = parser.unmarshal(bytes, OpaqueContainer.class); assertThat(oc, is(notNullValue())); assertThat(oc.getLength(), is(4)); assertThat(oc.getContent().length, is(oc.getLength())); assertThat(oc.getContent()[0], is((byte) 20)); assertThat(oc.getContent()[1], is((byte) 22)); assertThat(oc.getContent()[2], is((byte) 24)); assertThat(oc.getContent()[3], is((byte) 26)); }
public static Object unmarshal(String msg, String userPackages[]) throws Exception { TreeSet<String> contextPackages = new TreeSet<String>(); for (int i = 0; i < userPackages.length; i++) { String userPackage = userPackages[i]; contextPackages.add(userPackage); } JAXBContext jaxbContext = JAXBUtils.getJAXBContext( contextPackages, constructionType, contextPackages.toString(), XmlUtils.class.getClassLoader(), new HashMap<String, Object>()); Unmarshaller unmarshaller = JAXBUtils.getJAXBUnmarshaller(jaxbContext); Object o = unmarshaller.unmarshal(staxIF.createXMLStreamReader(new StringSource(msg))); JAXBUtils.releaseJAXBUnmarshaller(jaxbContext, unmarshaller); if (o instanceof JAXBElement) return ((JAXBElement) o).getValue(); return o; }
public void testXMLStreamWriter() throws Exception { FlushRoot control = getControlObject(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLOutputFactory xof = XMLOutputFactory.newFactory(); XMLStreamWriter xsw = xof.createXMLStreamWriter(baos); marshaller.marshal(control, xsw); XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new ByteArrayInputStream(baos.toByteArray())); Object test = unmarshaller.unmarshal(xsr); assertEquals(control, test); }
@Test public void shouldUnmarshalSeqMinLengthSuffix() throws Exception { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeByte(2); writer.writeInt(17); writer.writeInt(39); for (int i = 0; i < 11; i++) { writer.write(0); } writer.writeByte(99); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(21)); Unmarshaller unmarshaller = dadlContext.createUnmarshaller(); SeqMinLengthSuffix seqMinLength = unmarshaller.unmarshal(bytes, SeqMinLengthSuffix.class); NumberList list = seqMinLength.getSml().getNumberList(); assertThat(list.getNumItems(), is(2)); assertThat(list.getItems().get(0), is(17)); assertThat(list.getItems().get(1), is(39)); assertThat(seqMinLength.getSuffix(), is(99)); }
Context(Class clazz) { try { jaxbContext = JAXBContext.newInstance(clazz); marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setSchema(null); unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setSchema(null); } catch (JAXBException e) { e.printStackTrace(); } }
@Test public void shouldUnmarshalTaggedListWithSuffix() throws IOException { ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter(); writer.writeByte(0x8C); writer.writeByte(8); writer.writeShort(500); writer.writeShort(600); writer.writeShort(700); writer.writeShort(800); writer.writeInt(999); writer.close(); byte[] bytes = writer.toByteArray(); assertThat(bytes.length, is(14)); Unmarshaller unmarshaller = dadlContext.createUnmarshaller(); TaggedListWithSuffix tlws = unmarshaller.unmarshal(bytes, TaggedListWithSuffix.class); assertThat(tlws, is(notNullValue())); TaggedList tl = tlws.getTaggedList(); assertThat(tl.getIndexes().size(), is(4)); assertThat(tl.getIndexes(), contains(500, 600, 700, 800)); assertThat(tlws.getSuffix(), is(999)); }
@Override public Object parseRequest(String className) throws ParseException { try { Class clazz = Class.forName(className); JAXBContext jaxbContext = JAXBContext.newInstance(clazz.getPackage().getName()); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Source source = new StreamSource(new ByteArrayInputStream(body.getBytes())); JAXBElement<Object> root = jaxbUnmarshaller.unmarshal(source, clazz); return clazz.cast(root.getValue()); } catch (JAXBException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } catch (ClassNotFoundException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } }