Пример #1
0
  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();
  }
  public void testCreateContextWithPathAndBindings() throws Exception {
    String oxmString = "org/eclipse/persistence/testing/jaxb/jaxbcontext/eclipselink-oxm.xml";
    InputStream oxm = ClassLoader.getSystemClassLoader().getResourceAsStream(oxmString);

    Map<String, Object> props = new HashMap<String, Object>();
    props.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm);

    // Specify some other, unrelated context path -- we want to ensure that we don't fail
    // due to lack of ObjectFactory/jaxb.index
    JAXBContext ctx =
        JAXBContext.newInstance(
            "org.eclipse.persistence.testing.oxm.jaxb", ClassLoader.getSystemClassLoader(), props);

    Employee e = new Employee();
    e.id = 6;
    e.name = "Jeeves Sobs";
    e.put("tag", "tag-value");

    Document marshalDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(e, marshalDoc);

    // Make sure OXM was picked up, "tag" property should have been added.
    Employee e2 = (Employee) ctx.createUnmarshaller().unmarshal(marshalDoc);
    assertEquals(
        "OXM file was not processed during context creation.", e.get("tag"), e2.get("tag"));
  }
Пример #3
0
  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();
  }
Пример #4
0
  /**
   * 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;
  }
Пример #5
0
    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();
      }
    }
Пример #6
0
 /** 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();
     }
   }
 }
Пример #7
0
  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);
  }
Пример #8
0
  @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);
    }
  }
Пример #9
0
 @Override
 protected void setUp() throws Exception {
   JAXBContext jc = JAXBContextFactory.createContext(new Class[] {FlushRoot.class}, null);
   marshaller = jc.createMarshaller();
   unmarshaller = jc.createUnmarshaller();
 }