Example usage for javax.xml.bind Marshaller JAXB_FRAGMENT

List of usage examples for javax.xml.bind Marshaller JAXB_FRAGMENT

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller JAXB_FRAGMENT.

Prototype

String JAXB_FRAGMENT

To view the source code for javax.xml.bind Marshaller JAXB_FRAGMENT.

Click Source Link

Document

The name of the property used to specify whether or not the marshaller will generate document level events (ie calling startDocument or endDocument).

Usage

From source file:de.tudarmstadt.ukp.integration.alignment.xml.AlignmentXmlWriter.java

public AlignmentXmlWriter(OutputStream out) throws IOException {

    try {/*from  w w  w  . j  av a2s  .  c  o m*/
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        xmlEventWriter = xmlOutputFactory.createXMLEventWriter(out);

        JAXBContext context = JAXBContext.newInstance(XmlMeta.class, Alignments.class); //Source.class

        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); // no document level events
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        xmlef = XMLEventFactory.newInstance();
        xmlEventWriter.add(xmlef.createStartDocument());
        xmlEventWriter.add(xmlef.createStartElement("", "", RESOURCE_ALIGNMENT));

    } catch (XMLStreamException e) {
        throw new IOException(e);
    } catch (JAXBException e) {
        throw new IOException(e);
    }

}

From source file:org.mashupmedia.service.MapperManagerImpl.java

protected Marshaller getMarshaller() throws JAXBException {
    if (marshaller != null) {
        return marshaller;
    }/*from w w  w  .  j a va  2  s  . c o m*/

    JAXBContext jaxbContext = JAXBContext.newInstance(Song.class);
    marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT, true);
    return marshaller;
}

From source file:com.px100systems.util.XmlParser.java

/**
 * Serialize the bean to XML//from  ww w .j a  v a  2 s .c om
 * @param bean teh bean to serialize
 * @return XML
 */
public String write(T bean) {
    StringBuilder result = new StringBuilder();
    Writer writer = new StringBuilderWriter(result);
    try {
        Marshaller m = jaxb.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        m.marshal(bean, writer);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
    return result.toString();
}

From source file:eu.impress.repository.service.BedAvailabilityServiceImpl.java

@Override
public String createBedAvailabilityDE() throws DatatypeConfigurationException {

    String DEmessageenvelope = "";
    String DEmessage = "";

    EDXLDistribution ed = EDXLlib.createEDXLEnvelope();

    try {/*ww  w.j  a v  a 2  s.  c o  m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(EDXLDistribution.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        //jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter sw = new StringWriter();
        //marshal the envelope 
        jaxbMarshaller.marshal(ed, sw);
        DEmessageenvelope = sw.toString();

        //could not unescape characters no matter what!
        //encapsulate the edxl have message into DE by avoiding jaxb 
        //DEmessage = EDXLlib.DEEncapsulation(DEmessageenvelope, edxlhave);
        DEmessage = DEmessageenvelope;

    } catch (JAXBException e) {
        e.printStackTrace();
        return "Error Marshalling XML Object";
    }

    return DEmessage;

}

From source file:com.tangfan.test.UserServiceTest.java

/**
 * add jaxws-ri???/*from www  .  ja  v a  2 s.co m*/
 */
@Test
public void add() {
    try {
        //1.?xml
        JAXBContext jaxb = JAXBContext.newInstance(LicenseInfo.class);
        User lu = new User();
        lu.setId(0);
        lu.setNickname("");
        lu.setUsername("admin");
        lu.setPassword("123");
        LicenseInfo info = new LicenseInfo();
        info.setLoginUser(lu);

        QName qName = new QName(ns, "licenseInfo");
        JAXBElement<LicenseInfo> jele = new JAXBElement<LicenseInfo>(qName, LicenseInfo.class, info);

        Marshaller mars = jaxb.createMarshaller();
        mars.setProperty(Marshaller.JAXB_FRAGMENT, true);//xml
        mars.setProperty(Marshaller.JAXB_ENCODING, "utf-8");//?

        //2.?dom
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        mars.marshal(jele, doc);

        //3.Headers.create?header
        WSBindingProvider wsb = (WSBindingProvider) port;
        wsb.setOutboundHeaders(Headers.create(doc.getDocumentElement()));

        User u = new User();
        u.setId(2);
        u.setUsername("master");
        u.setNickname("jboss?");
        u.setPassword("123456");
        port.add(u);
    } catch (UserException_Exception e) {
        System.out.println(e.getMessage());
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:com.github.lindenb.jvarkit.tools.blast.BlastFilterJS.java

@Override
protected Collection<Throwable> call(String inputName) throws Exception {

    final CompiledScript compiledScript;
    Unmarshaller unmarshaller;/*from w w  w.  j  a v  a2  s.  c  o  m*/
    Marshaller marshaller;
    try {
        compiledScript = super.compileJavascript();

        JAXBContext jc = JAXBContext.newInstance("gov.nih.nlm.ncbi.blast");

        unmarshaller = jc.createUnmarshaller();
        marshaller = jc.createMarshaller();

        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        PrintWriter pw = openFileOrStdoutAsPrintWriter();
        XMLOutputFactory xof = XMLOutputFactory.newFactory();
        xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE);
        XMLEventWriter w = xof.createXMLEventWriter(pw);

        StreamSource src = null;
        if (inputName == null) {
            LOG.info("Reading stdin");
            src = new StreamSource(stdin());
        } else {
            LOG.info("Reading file " + inputName);
            src = new StreamSource(new File(inputName));
        }

        XMLEventReader r = xmlInputFactory.createXMLEventReader(src);

        XMLEventFactory eventFactory = XMLEventFactory.newFactory();

        SimpleBindings bindings = new SimpleBindings();
        while (r.hasNext()) {
            XMLEvent evt = r.peek();
            switch (evt.getEventType()) {
            case XMLEvent.START_ELEMENT: {
                StartElement sE = evt.asStartElement();
                Hit hit = null;
                JAXBElement<Hit> jaxbElement = null;
                if (sE.getName().getLocalPart().equals("Hit")) {
                    jaxbElement = unmarshaller.unmarshal(r, Hit.class);
                    hit = jaxbElement.getValue();
                } else {
                    w.add(r.nextEvent());
                    break;
                }

                if (hit != null) {

                    bindings.put("hit", hit);

                    boolean accept = super.evalJavaScriptBoolean(compiledScript, bindings);

                    if (accept) {
                        marshaller.marshal(jaxbElement, w);
                        w.add(eventFactory.createCharacters("\n"));
                    }
                }

                break;
            }
            case XMLEvent.SPACE:
                break;
            default: {
                w.add(r.nextEvent());
                break;
            }
            }
            r.close();
        }
        w.flush();
        w.close();
        pw.flush();
        pw.close();
        return RETURN_OK;
    } catch (Exception err) {
        return wrapException(err);
    } finally {

    }
}

From source file:eu.impress.impressplatform.IntegrationLayer.ResourcesMgmt.BedAvailabilityServiceBean.java

@Override
public String createBedAvailabilityDE() throws DatatypeConfigurationException {

    String DEmessageenvelope = "";
    String DEmessage = "";

    EDXLDistribution ed = EDXLlib.createEDXLEnvelope();

    try {//from  w w  w  .  java 2 s . c o  m
        JAXBContext jaxbContext = JAXBContext.newInstance(EDXLDistribution.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        //jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter sw = new StringWriter();
        //marshal the envelope 
        jaxbMarshaller.marshal(ed, sw);
        DEmessageenvelope = sw.toString();

        //could not unescape characters no matter what!
        //encapsulate the edxl have message into DE by avoiding jaxb 
        //DEmessage = EDXLlib.DEEncapsulation(DEmessageenvelope, edxlhave);
        DEmessage = DEmessageenvelope;

    } catch (JAXBException e) {
        e.printStackTrace();
        return "Error Marshalling XML Object" + HttpStatus.INTERNAL_SERVER_ERROR;
    }

    return DEmessage;

}

From source file:com.floreantpos.bo.actions.DataExportAction.java

@Override
public void actionPerformed(ActionEvent e) {
    Session session = null;//from   w  ww  .  j a  v  a  2 s  .  co  m
    Transaction transaction = null;
    FileWriter fileWriter = null;
    GenericDAO dao = new GenericDAO();

    try {
        JFileChooser fileChooser = getFileChooser();
        int option = fileChooser.showSaveDialog(com.floreantpos.util.POSUtil.getBackOfficeWindow());
        if (option != JFileChooser.APPROVE_OPTION) {
            return;
        }

        File file = fileChooser.getSelectedFile();
        if (file.exists()) {
            option = JOptionPane.showConfirmDialog(com.floreantpos.util.POSUtil.getFocusedWindow(),
                    Messages.getString("DataExportAction.1") + file.getName() + "?", //$NON-NLS-1$//$NON-NLS-2$
                    Messages.getString("DataExportAction.3"), //$NON-NLS-1$
                    JOptionPane.YES_NO_OPTION);
            if (option != JOptionPane.YES_OPTION) {
                return;
            }
        }

        // fixMenuItemModifierGroups();

        JAXBContext jaxbContext = JAXBContext.newInstance(Elements.class);
        Marshaller m = jaxbContext.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        StringWriter writer = new StringWriter();

        session = dao.createNewSession();
        transaction = session.beginTransaction();

        Elements elements = new Elements();

        //          * 2. USERS
        //          * 3. TAX
        //          * 4. MENU_CATEGORY
        //          * 5. MENU_GROUP
        //          * 6. MENU_MODIFIER
        //          * 7. MENU_MODIFIER_GROUP
        //          * 8. MENU_ITEM
        //          * 9. MENU_ITEM_SHIFT
        //          * 10. RESTAURANT
        //          * 11. USER_TYPE
        //          * 12. USER_PERMISSION
        //          * 13. SHIFT

        elements.setTaxes(TaxDAO.getInstance().findAll(session));
        elements.setMenuCategories(MenuCategoryDAO.getInstance().findAll(session));
        elements.setMenuGroups(MenuGroupDAO.getInstance().findAll(session));
        elements.setMenuModifiers(MenuModifierDAO.getInstance().findAll(session));
        elements.setMenuModifierGroups(MenuModifierGroupDAO.getInstance().findAll(session));
        elements.setMenuItems(MenuItemDAO.getInstance().findAll(session));
        elements.setMenuItemModifierGroups(MenuItemModifierGroupDAO.getInstance().findAll(session));

        //           elements.setUsers(UserDAO.getInstance().findAll(session));
        //           
        //           elements.setMenuItemShifts(MenuItemShiftDAO.getInstance().findAll(session));
        //           elements.setRestaurants(RestaurantDAO.getInstance().findAll(session));
        //           elements.setUserTypes(UserTypeDAO.getInstance().findAll(session));
        //           elements.setUserPermissions(UserPermissionDAO.getInstance().findAll(session));
        //           elements.setShifts(ShiftDAO.getInstance().findAll(session));

        m.marshal(elements, writer);

        transaction.commit();

        fileWriter = new FileWriter(file);
        fileWriter.write(writer.toString());
        fileWriter.close();

        POSMessageDialog.showMessage(com.floreantpos.util.POSUtil.getFocusedWindow(),
                Messages.getString("DataExportAction.4")); //$NON-NLS-1$

    } catch (Exception e1) {
        transaction.rollback();
        PosLog.error(getClass(), e1);
        POSMessageDialog.showMessage(com.floreantpos.util.POSUtil.getFocusedWindow(), e1.getMessage());
    } finally {
        IOUtils.closeQuietly(fileWriter);
        dao.closeSession(session);
    }
}

From source file:org.axiom_tools.codecs.ModelCodec.java

/**
 * Converts a model to XML./*from   w  ww  .jav  a  2  s .  c  o  m*/
 * @return model XML, or empty
 */
public String toXML() {
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Marshaller m = buildJAXBContext().createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        m.marshal(this.entity, stream);
        return stream.toString(XML_ENCODING).trim();
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
        return Empty;
    }
}

From source file:com.redhat.akashche.wixgen.cli.Launcher.java

private static void writeXml(Marshaller marshaller, Object jaxbElement, String path, boolean fragment)
        throws Exception {
    Writer writer = null;/*from   w  w w.j a  va 2s . c o m*/
    try {
        OutputStream os = new FileOutputStream(new File(path));
        writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, fragment);
        marshaller.marshal(jaxbElement, writer);
    } finally {
        closeQuietly(writer);
    }
}