Пример #1
0
 private static void writeGameXml() throws JAXBException {
   JAXBContext jaxbContext = JAXBContext.newInstance(Game.class);
   Marshaller marshaller = jaxbContext.createMarshaller();
   marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
   File file = new File("Game.xml");
   marshaller.marshal(game, file);
 }
  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();
  }
  public void marshal(Object object, XMLStreamWriter streamWriter, TypeMappingInfo type)
      throws JAXBException {
    if (jaxbContext.getTypeMappingInfoToGeneratedType() == null) {
      marshal(object, streamWriter);
    } else {
      JAXBElement element = null;
      Object value = object;
      if (object instanceof JAXBElement) {
        // use the JAXBElement's properties to populate an XMLRoot
        element = (JAXBElement) object;
        value = element.getValue();
      }
      if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
        RootLevelXmlAdapter adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
        if (adapter != null) {
          try {
            value = adapter.getXmlAdapter().marshal(value);
          } catch (Exception ex) {
            throw new JAXBException(XMLMarshalException.marshalException(ex));
          }
        }
      }

      value = wrapObject(value, element, type);
      marshal(value, streamWriter);
    }
  }
Пример #5
0
  @Override
  public String prepareResponse(String className, Object o) throws ParseException {
    try {
      Class clazz = Class.forName(className);

      JAXBContext jaxbContext = JAXBContext.newInstance(clazz.getPackage().getName());
      Marshaller marshaller = jaxbContext.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      marshaller.marshal(clazz.cast(o), byteArrayOutputStream);

      return byteArrayOutputStream.toString();

    } 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);
    }
  }
Пример #6
0
  /**
   * Marshalls a scenario object and writes into output XML file
   *
   * @throws JAXBException, SiriusException
   */
  public void marshallIntoXML(Scenario scenarioToWrite)
      throws JAXBException, FileNotFoundException, BeatsException {

    JAXBContext jaxbContext = JAXBContext.newInstance("edu.berkeley.path.beats.jaxb");
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setSchema(this.getSchema());
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    jaxbMarshaller.marshal(scenarioToWrite, new File(this.outputFileName));
  }
Пример #7
0
  private static void createXml(TestObjects testObjects) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Characters.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    File file = new File("Characters.xml");
    marshaller.marshal(testObjects.getCharacters(), file);

    file = new File("LearningObjectiveOut.xml");
    jaxbContext = JAXBContext.newInstance(LearningAct.class);
    marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(testObjects.getLearningAct(), file);

    file = new File("ThemeOut.xml");
    jaxbContext = JAXBContext.newInstance(Theme.class);
    marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(testObjects.getTheme(), file);

    file = new File("LocaleOut.xml");
    jaxbContext = JAXBContext.newInstance(Locale.class);
    marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(testObjects.getLocale(), file);
  }
Пример #8
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;
  }
Пример #9
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();
      }
    }
Пример #10
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);
  }
Пример #11
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();
     }
   }
 }
  private Object wrapObject(
      Object object, JAXBElement wrapperElement, TypeMappingInfo typeMappingInfo) {
    if (jaxbContext.getTypeMappingInfoToGeneratedType().size() > 0) {
      Class generatedClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(typeMappingInfo);
      if (generatedClass != null && object == null && wrapperElement != null) {
        return wrapObjectInXMLRoot(wrapperElement, object, typeMappingInfo);
      }

      if (generatedClass != null && WrappedValue.class.isAssignableFrom(generatedClass)) {
        ClassDescriptor desc =
            xmlMarshaller.getXMLContext().getSession(generatedClass).getDescriptor(generatedClass);
        Object newObject = desc.getInstantiationPolicy().buildNewInstance();
        ((WrappedValue) newObject).setValue(object);
        object = newObject;
      } else if (generatedClass != null) {
        // should be a many value
        ClassDescriptor desc =
            xmlMarshaller.getXMLContext().getSession(generatedClass).getDescriptor(generatedClass);
        Object newObject = desc.getInstantiationPolicy().buildNewInstance();
        ((ManyValue) newObject).setItem(object);
        object = newObject;
      }
    }

    if (null == wrapperElement) {
      Root xmlRoot = new Root();
      QName xmlTagName = typeMappingInfo.getXmlTagName();
      if (null == xmlTagName) {
        return object;
      }
      xmlRoot.setNamespaceURI(typeMappingInfo.getXmlTagName().getNamespaceURI());
      xmlRoot.setLocalName(typeMappingInfo.getXmlTagName().getLocalPart());
      xmlRoot.setObject(object);
      return xmlRoot;
    }
    return wrapObjectInXMLRoot(wrapperElement, object, typeMappingInfo);
  }
 @Override
 public void execute() {
   try {
     File file = new File(path);
     file.mkdir();
     file = new File(path + config.getName() + extention);
     file.createNewFile();
     JAXBContext jc = JAXBContext.newInstance(PrintConfiguration.class);
     JAXBElement<PrintConfiguration> je =
         new JAXBElement<PrintConfiguration>(
             new QName(xmlHeadName), PrintConfiguration.class, config);
     Marshaller marshaller = jc.createMarshaller();
     OutputStream os = new FileOutputStream(path + config.getName() + extention);
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
     marshaller.marshal(je, os);
     file = null;
     os = null;
     System.gc();
     result = Boolean.TRUE;
   } catch (Exception ex) {
     Logger.getLogger(SavePrintConfigurationCommand.class.getName()).log(Level.SEVERE, null, ex);
     result = Boolean.FALSE;
   }
 }
Пример #14
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);
    }
  }
 private HashMap<String, Class> getClassToGeneratedClasses() {
   return jaxbContext.getClassToGeneratedClasses();
 }
  /**
   * Create an instance of XMLRoot populated from the contents of the provided JAXBElement. XMLRoot
   * will be used to hold the contents of the JAXBElement while the marshal operation is performed
   * by TopLink OXM. This will avoid adding any runtime dependencies to TopLink.
   *
   * @param elt
   * @return
   */
  private Root createXMLRootFromJAXBElement(JAXBElement elt) {
    // create an XMLRoot to hand into the marshaller
    Root xmlroot = new Root();
    Object objectValue = elt.getValue();
    xmlroot.setObject(objectValue);
    QName qname = elt.getName();
    xmlroot.setLocalName(qname.getLocalPart());
    xmlroot.setNamespaceURI(qname.getNamespaceURI());
    xmlroot.setDeclaredType(elt.getDeclaredType());
    xmlroot.setNil(elt.isNil());
    if (elt.getDeclaredType() == CoreClassConstants.ABYTE
        || elt.getDeclaredType() == CoreClassConstants.APBYTE
        || elt.getDeclaredType().getCanonicalName().equals("javax.activation.DataHandler")
        || elt.getDeclaredType().isEnum()) {
      // need a binary data mapping so need to wrap
      Class generatedClass =
          getClassToGeneratedClasses().get(elt.getDeclaredType().getCanonicalName());
      if (!elt.getDeclaredType().isEnum()) {
        xmlroot.setSchemaType(Constants.BASE_64_BINARY_QNAME);
      }
      if (generatedClass != null && WrappedValue.class.isAssignableFrom(generatedClass)) {
        ClassDescriptor desc =
            xmlMarshaller.getXMLContext().getSession(generatedClass).getDescriptor(generatedClass);
        Object newObject = desc.getInstantiationPolicy().buildNewInstance();
        ((WrappedValue) newObject).setValue(objectValue);
        xmlroot.setObject(newObject);
        return xmlroot;
      }
    } else {
      xmlroot.setSchemaType(
          (QName)
              org.eclipse.persistence.internal.oxm.XMLConversionManager.getDefaultJavaTypes()
                  .get(elt.getDeclaredType()));
    }

    if (elt instanceof WrappedValue) {
      xmlroot.setObject(elt);
      return xmlroot;
    }
    Map<QName, Class> qNameToGeneratedClasses = jaxbContext.getQNameToGeneratedClasses();
    if (qNameToGeneratedClasses != null) {
      Class theClass = qNameToGeneratedClasses.get(qname);
      if (theClass != null && WrappedValue.class.isAssignableFrom(theClass)) {
        ClassDescriptor desc =
            xmlMarshaller.getXMLContext().getSession(theClass).getDescriptor(theClass);
        Object newObject = desc.getInstantiationPolicy().buildNewInstance();
        ((WrappedValue) newObject).setValue(objectValue);
        xmlroot.setObject(newObject);
        return xmlroot;
      }
    }

    Class generatedClass = null;
    if (jaxbContext.getTypeMappingInfoToGeneratedType() != null) {
      if (jaxbContext.getTypeToTypeMappingInfo() != null) {
        if (elt.getDeclaredType() != null && elt.getDeclaredType().isArray()) {
          TypeMappingInfo tmi = jaxbContext.getTypeToTypeMappingInfo().get(elt.getDeclaredType());
          generatedClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(tmi);
        } else if (elt instanceof JAXBTypeElement) {
          Type objectType = ((JAXBTypeElement) elt).getType();
          TypeMappingInfo tmi = jaxbContext.getTypeToTypeMappingInfo().get(objectType);
          generatedClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(tmi);
        }
      }
    } else {
      if (elt.getDeclaredType() != null && elt.getDeclaredType().isArray()) {
        if (jaxbContext.getArrayClassesToGeneratedClasses() != null) {
          generatedClass =
              jaxbContext
                  .getArrayClassesToGeneratedClasses()
                  .get(elt.getDeclaredType().getCanonicalName());
        }
      } else if (elt instanceof JAXBTypeElement) {
        Type objectType = ((JAXBTypeElement) elt).getType();
        generatedClass = jaxbContext.getCollectionClassesToGeneratedClasses().get(objectType);
      }
    }

    if (generatedClass != null) {
      ClassDescriptor desc =
          xmlMarshaller.getXMLContext().getSession(generatedClass).getDescriptor(generatedClass);
      Object newObject = desc.getInstantiationPolicy().buildNewInstance();
      ((ManyValue) newObject).setItem(objectValue);
      xmlroot.setObject(newObject);
    }

    return xmlroot;
  }
Пример #17
0
 /**
  * Returns a new JAXB context.
  *
  * @return a JAXBContext
  * @throws JAXBException if raised during construction
  */
 private JAXBContext buildJAXBContext() throws JAXBException {
   return JAXBContext.newInstance(this.entityClass);
 }
 /**
  * Test creating a context with classes generated from the GML/XLink schemas. Tests that a class
  * with both ns0:title and @ns0:title mappings is interpreted properly.
  */
 public void testCreateContextXLink() throws Exception {
   JAXBContext context =
       JAXBContext.newInstance(this.getClass().getPackage().getName() + ".xlink");
   assertNotNull(context);
 }
Пример #19
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();
  }
Пример #20
0
 @Override
 protected void setUp() throws Exception {
   JAXBContext jc = JAXBContextFactory.createContext(new Class[] {FlushRoot.class}, null);
   marshaller = jc.createMarshaller();
   unmarshaller = jc.createUnmarshaller();
 }
Пример #21
0
  private static void createXsd() throws JAXBException, IOException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Characters.class);
    SchemaOutputResolver sor = new MySchemaOutputResolver("Characters.xsd");
    jaxbContext.generateSchema(sor);

    jaxbContext = JAXBContext.newInstance(Theme.class);
    sor = new MySchemaOutputResolver("Theme.xsd");
    jaxbContext.generateSchema(sor);

    // jaxbContext = JAXBContext.newInstance(LearningAct.class);
    // sor = new MySchemaOutputResolver("LearningAct.xsd");
    // jaxbContext.generateSchema(sor);

    jaxbContext = JAXBContext.newInstance(Lesson.class);
    sor = new MySchemaOutputResolver("Lesson.xsd");
    jaxbContext.generateSchema(sor);

    jaxbContext = JAXBContext.newInstance(Challenge.class);
    sor = new MySchemaOutputResolver("Challenge.xsd");
    jaxbContext.generateSchema(sor);

    jaxbContext = JAXBContext.newInstance(Locale.class);
    sor = new MySchemaOutputResolver("Locale.xsd");
    jaxbContext.generateSchema(sor);

    jaxbContext = JAXBContext.newInstance(Subject.class);
    sor = new MySchemaOutputResolver("Subject.xsd");
    jaxbContext.generateSchema(sor);
  }