Example usage for javax.xml.bind JAXBElement JAXBElement

List of usage examples for javax.xml.bind JAXBElement JAXBElement

Introduction

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

Prototype

public JAXBElement(QName name, Class<T> declaredType, T value) 

Source Link

Document

Construct an xml element instance.

Usage

From source file:com.tremolosecurity.openunison.util.OpenUnisonUtils.java

private static void storeMethod(String unisonXMLFile, TremoloType tt, String ksPath, KeyStore ks)
        throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException,
        FileNotFoundException, JAXBException, PropertyException {
    logger.info("Storing the keystore");
    ks.store(new FileOutputStream(ksPath), tt.getKeyStorePassword().toCharArray());

    logger.info("Saving the unison xml file");

    JAXBContext jc = JAXBContext.newInstance("com.tremolosecurity.config.xml");
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    OutputStream os = new FileOutputStream(unisonXMLFile);
    JAXBElement<TremoloType> root = new JAXBElement<TremoloType>(
            new QName("http://www.tremolosecurity.com/tremoloConfig", "tremoloConfig", "tns"),
            TremoloType.class, tt);
    marshaller.marshal(root, os);/*from   w  ww  .  ja  v a 2 s . c  om*/
    os.flush();
    os.close();
}

From source file:fr.fastconnect.factory.tibco.bw.maven.packaging.ApplicationManagement.java

/**
 * <p>/*  w w  w. j  a  va  2s. com*/
 * This method will create JAXB objects from Properties through recursive
 * calls.
 * Each property has a path. The last part of a path (after last /) is the
 * element.
 * </p> 
 */
public Object getElement(String path, String element, String value, Object parent) {
    if (map.containsKey(path)) {
        return map.get(path);
    } else {
        Matcher matcherElement = patternElement.matcher(element);
        if (matcherElement.matches()) {
            String elementName = matcherElement.group(1);
            String nameAttribute = matcherElement.group(3);

            if (nameAttribute != null) {

                if (elementName.equals("variables")) {
                    NVPairs gvs = null;
                    if (parent.getClass().equals(Bw.class) || parent.getClass().equals(Adapter.class)) {
                        ServiceType service = (ServiceType) parent;
                        for (NVPairs nvPairs : service.getNVPairs()) {
                            if (nvPairs.getName().equals(nameAttribute)) {
                                gvs = nvPairs;
                                break;
                            }
                        }
                        if (gvs == null) {
                            gvs = new NVPairs();
                            service.getNVPairs().add(gvs);
                        }
                    } else if (parent.getClass().equals(Binding.class)) {
                        gvs = new NVPairs();
                        gvs.setName(nameAttribute);
                        ((Binding) parent).setNVPairs(gvs);
                    }
                    map.put(path, gvs);
                    return gvs;
                } else if (elementName.equals("variable")) {
                    NameValuePair simpleGV = new NameValuePair();
                    simpleGV.setName(nameAttribute);
                    simpleGV.setValue(value);

                    QName name = new QName(APPLICATION_MANAGEMENT_NAMESPACE, "NameValuePair");
                    JAXBElement<NameValuePair> _simpleGV = new JAXBElement<NameValuePair>(name,
                            NameValuePair.class, simpleGV);
                    boolean found = false;
                    for (JAXBElement<? extends NVPairType> nvPair : ((NVPairs) parent).getNVPair()) {
                        if (nameAttribute.equals(nvPair.getValue().getName())) {
                            nvPair.getValue().setValue(value);
                            found = true;
                        }
                    }
                    if (!found) {
                        ((NVPairs) parent).getNVPair().add(_simpleGV);
                    }

                    return simpleGV;
                } else if (elementName.equals("bw")) {
                    Bw service = this.getBw(nameAttribute);
                    map.put(path, service);
                    return service;
                } else if (elementName.equals("adapter")) {
                    Adapter service = this.getAdapter(nameAttribute);
                    map.put(path, service);
                    return service;
                } else if (elementName.equals("binding")) {
                    Binding binding = null;
                    if (parent.getClass().equals(Bw.class)) {
                        binding = this.getBinding(nameAttribute, (Bw) parent);
                    } else if (parent.getClass().equals(Adapter.class)) {
                        binding = this.getBinding(nameAttribute, (Adapter) parent);
                    } else {
                        // throw ?
                    }
                    map.put(path, binding);
                    return binding;
                } else if (elementName.equals("bwprocess")) {
                    Bwprocess bwProcess = this.getBWProcess(nameAttribute, (Bw) parent);
                    map.put(path, bwProcess);
                    return bwProcess;
                } else if (elementName.equals("checkpoint")) {
                    Checkpoints checkpoints = (Checkpoints) parent;
                    if (!checkpoints.getCheckpoint().contains(nameAttribute)) {
                        checkpoints.getCheckpoint().add(nameAttribute);
                    }
                    if ("true".equals(value)) {
                        checkpoints.setSelected(nameAttribute);
                    }
                }
            } else {
                if (elementName.equals("variables")) {
                    NVPairs gvs = null;
                    if (parent.getClass().equals(Bw.class) || parent.getClass().equals(Adapter.class)) {
                        ServiceType service = (ServiceType) parent;
                        for (NVPairs nvPairs : service.getNVPairs()) {
                            if (nvPairs.getName().equals("Runtime Variables")) {
                                gvs = nvPairs;
                                break;
                            }
                        }
                        if (gvs == null) {
                            gvs = new NVPairs();
                            service.getNVPairs().add(gvs);
                        }
                    } else if (parent.getClass().equals(Binding.class)) {
                        gvs = new NVPairs();
                        gvs.setName("Runtime Variables");
                        ((Binding) parent).setNVPairs(gvs);
                    }
                    map.put(path, gvs);
                    return gvs;
                } else if (elementName.equals("checkpoints")) {
                    Checkpoints checkpoints = this.getBWCheckpoints((Bw) parent);
                    map.put(path, checkpoints);
                    return checkpoints;
                } else if (elementName.equals("tablePrefix")) {
                    Checkpoints checkpoints = (Checkpoints) parent;
                    checkpoints.setTablePrefix(value);
                    // Binding children
                } else if (parent.getClass().equals(Binding.class)) {
                    return addBindingParameter((Binding) parent, elementName, value);
                } else if (parent.getClass().equals(Product.class)) {
                    return addProductParameter((Product) parent, elementName, value);
                } else if (parent.getClass().equals(Setting.class)) {
                    return addSettingParameter((Setting) parent, elementName, value);
                } else if (parent.getClass().equals(NTService.class)) {
                    return addNTServiceParameter((NTService) parent, elementName, value);
                } else if (parent.getClass().equals(Java.class)) {
                    return addJavaParameter((Java) parent, elementName, value);
                } else if (parent.getClass().equals(Shutdown.class)) {
                    return addShutdownParameter((Shutdown) parent, elementName, value);
                    //
                    // Bwprocess children
                } else if (parent.getClass().equals(Bwprocess.class)) {
                    addBWProcessParameter((Bwprocess) parent, elementName, value);
                } else if (parent.getClass().equals(FaultTolerant.class)) {
                    addFaultTolerantParameter((FaultTolerant) parent, elementName, value);
                } else if (parent.getClass().equals(Checkpoints.class)) {
                    addCheckpointsParameter((Checkpoints) parent, elementName, value);
                    // Bw chidren (direct children)
                } else if (parent.getClass().equals(Bw.class)) {
                    return addBwParameter((Bw) parent, elementName, value);
                } else if (parent.getClass().equals(Adapter.class)) {
                    return addAdapterParameter((Adapter) parent, elementName, value);
                }
            }

        }
        return parent;
    }
}

From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java

@Test
public void testGetJAXBElementXmlRootBookCollection() throws Exception {
    BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
    Book b1 = new Book("CXF in Action", 123L);
    Book b2 = new Book("CXF Rocks", 124L);
    List<JAXBElement<Book>> books = new ArrayList<JAXBElement<Book>>();
    books.add(new JAXBElement<Book>(new QName("bookRootElement"), Book.class, b1));
    books.add(new JAXBElement<Book>(new QName("bookRootElement"), Book.class, b2));
    List<JAXBElement<Book>> books2 = store.getJAXBElementBookXmlRootCollection(books);
    assertNotNull(books2);
    assertNotSame(books, books2);//from   w  ww .  j ava2 s.com
    assertEquals(2, books2.size());
    Book b11 = books2.get(0).getValue();
    assertEquals(123L, b11.getId());
    assertEquals("CXF in Action", b11.getName());
    Book b22 = books2.get(1).getValue();
    assertEquals(124L, b22.getId());
    assertEquals("CXF Rocks", b22.getName());
}

From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java

@Test
public void testGetJAXBElementBookCollection() throws Exception {
    JAXBElementProvider<?> provider = new JAXBElementProvider<Object>();
    provider.setMarshallAsJaxbElement(true);
    provider.setUnmarshallAsJaxbElement(true);
    BookStore store = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class,
            Collections.singletonList(provider));
    BookNoXmlRootElement b1 = new BookNoXmlRootElement("CXF in Action", 123L);
    BookNoXmlRootElement b2 = new BookNoXmlRootElement("CXF Rocks", 124L);
    List<JAXBElement<BookNoXmlRootElement>> books = new ArrayList<JAXBElement<BookNoXmlRootElement>>();
    books.add(new JAXBElement<BookNoXmlRootElement>(new QName("bookNoXmlRootElement"),
            BookNoXmlRootElement.class, b1));
    books.add(new JAXBElement<BookNoXmlRootElement>(new QName("bookNoXmlRootElement"),
            BookNoXmlRootElement.class, b2));
    List<JAXBElement<BookNoXmlRootElement>> books2 = store.getJAXBElementBookCollection(books);
    assertNotNull(books2);/*from  w w  w .  j  a  va  2  s.  co m*/
    assertNotSame(books, books2);
    assertEquals(2, books2.size());
    BookNoXmlRootElement b11 = books.get(0).getValue();
    assertEquals(123L, b11.getId());
    assertEquals("CXF in Action", b11.getName());
    BookNoXmlRootElement b22 = books.get(1).getValue();
    assertEquals(124L, b22.getId());
    assertEquals("CXF Rocks", b22.getName());
}

From source file:fr.fastconnect.factory.tibco.bw.maven.packaging.ApplicationManagement.java

protected static <E extends EventType> void addEvent(Events events, E event) {
    if (events == null || event == null)
        return;/*from  ww  w. j a v  a 2s .  co  m*/

    @SuppressWarnings("unchecked")
    Class<E> c = (Class<E>) event.getClass();
    JAXBElement<E> e = new JAXBElement<E>(new QName("name"), c, event);
    events.getEvent().add(e);
}

From source file:fr.fastconnect.factory.tibco.bw.maven.packaging.ApplicationManagement.java

public static <A extends ActionType> void addAction(Actions actions, A action) {
    if (actions == null || action == null)
        return;//from w  ww. j a v  a2  s .c o m

    @SuppressWarnings("unchecked")
    Class<A> c = (Class<A>) action.getClass();
    JAXBElement<A> a = new JAXBElement<A>(new QName("name"), c, action);
    actions.getAction().add(a);
}

From source file:be.fedict.eid.idp.common.saml2.Saml2Util.java

/**
 * Convert specified JAXB object to an opensaml XML object
 * //from   w ww  . j av a 2  s  .  c o m
 * @param wsObject
 *            JAXB object
 * @param wsType
 *            JAXB class
 * @param samlElementName
 *            opensaml QName
 * @param <F>
 *            JAXB type
 * @param <T>
 *            opensaml type
 * @return opensaml {@link XMLObject}
 */
@SuppressWarnings({ "unchecked" })
public static <F, T extends XMLObject> T toSAML(final F wsObject, Class<F> wsType, QName samlElementName) {

    try {
        Document root = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        JAXBContext.newInstance(wsType).createMarshaller()
                .marshal(new JAXBElement<F>(samlElementName, wsType, wsObject), root);

        return (T) unmarshall(root.getDocumentElement());
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Default parser " + "configuration failed.", e);
    } catch (JAXBException e) {
        throw new RuntimeException("Marshaling to OpenSAML " + "object failed.", e);
    }
}

From source file:com.evolveum.midpoint.prism.parser.XNodeProcessor.java

public <T> JAXBElement<T> parseAnyValueAsJAXBElement(XNode node) throws SchemaException {
    // is the type name explicitly specified? (if not, guess that we have a string)
    QName typeName = getExplicitType(node);
    if (typeName == null && node instanceof PrimitiveXNode) {
        typeName = DOMUtil.XSD_STRING;//from w ww.j  a  va2s .c  o m
    }
    if (typeName != null) {
        ItemDefinition itemDefinition = getSchemaRegistry().findItemDefinitionByType(typeName);
        if (itemDefinition != null) {
            Item item = parseItem(node, getElementName(node, itemDefinition), itemDefinition);
            return getItemValueAsJAXBElement(item);
        } else {
            Object o = parseAtomicValue(node, typeName);
            if (o instanceof JAXBElement) {
                return (JAXBElement) o;
            } else {
                if (!(node instanceof RootXNode)) {
                    throw new SchemaException(
                            "Couldn't represent an object with no root element name and no item definition as JAXBElement: "
                                    + node);
                }
                QName elementName = ((RootXNode) node).getRootElementName();
                return new JAXBElement(elementName, Object.class, o); // or o.getClass?
            }
        }
    } else {
        // if type name is not known, we have to derive it from the element name
        if (!(node instanceof RootXNode)) {
            throw new SchemaException(
                    "Couldn't parse general object with no type name and no root element name: " + node);
        }
        QName elementName = ((RootXNode) node).getRootElementName();
        ItemDefinition itemDefinition = getSchemaRegistry().findItemDefinitionByElementName(elementName);
        if (itemDefinition == null) {
            throw new SchemaException(
                    "Couldn't parse general object with no type name and unknown element name: " + elementName);
        }
        Item item = parseItem(((RootXNode) node).getSubnode(), elementName, itemDefinition);
        return getItemValueAsJAXBElement(item);
    }
}

From source file:com.evolveum.midpoint.prism.parser.XNodeProcessor.java

private <T> JAXBElement<T> getItemValueAsJAXBElement(Item item) throws SchemaException {
    Object v = getItemRealValue(item);
    if (v == null) {
        return null;
    } else {/*ww  w .java  2 s  .com*/
        return (JAXBElement) new JAXBElement<>(item.getElementName(), Object.class, v); // or v.getClass?
    }
}

From source file:com.evolveum.midpoint.model.impl.lens.ChangeExecutor.java

private void evaluateScriptArgument(ProvisioningScriptArgumentType argument, ExpressionVariables variables,
        Task task, OperationResult result)
        throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {

    QName FAKE_SCRIPT_ARGUMENT_NAME = new QName(SchemaConstants.NS_C, "arg");

    PrismPropertyDefinition<String> scriptArgumentDefinition = new PrismPropertyDefinition<>(
            FAKE_SCRIPT_ARGUMENT_NAME, DOMUtil.XSD_STRING, prismContext);

    String shortDesc = "Provisioning script argument expression";
    Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory
            .makeExpression(argument, scriptArgumentDefinition, shortDesc, task, result);

    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, variables, shortDesc, task,
            result);/*  w  w  w  . java  2 s . c o  m*/
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> outputTriple = expression.evaluate(params);

    Collection<PrismPropertyValue<String>> nonNegativeValues = null;
    if (outputTriple != null) {
        nonNegativeValues = outputTriple.getNonNegativeValues();
    }

    //replace dynamic script with static value..
    argument.getExpressionEvaluator().clear();
    if (nonNegativeValues == null || nonNegativeValues.isEmpty()) {
        // We need to create at least one evaluator. Otherwise the expression code will complain
        //         Element value = DOMUtil.createElement(SchemaConstants.C_VALUE);
        //         DOMUtil.setNill(value);
        JAXBElement<RawType> el = new JAXBElement(SchemaConstants.C_VALUE, RawType.class,
                new RawType(prismContext));
        argument.getExpressionEvaluator().add(el);

    } else {
        for (PrismPropertyValue<String> val : nonNegativeValues) {
            //            Element value = DOMUtil.createElement(SchemaConstants.C_VALUE);
            //            value.setTextContent(val.getValue());
            PrimitiveXNode<String> prim = new PrimitiveXNode<>();
            prim.setValue(val.getValue(), DOMUtil.XSD_STRING);
            JAXBElement<RawType> el = new JAXBElement(SchemaConstants.C_VALUE, RawType.class,
                    new RawType(prim, prismContext));
            argument.getExpressionEvaluator().add(el);
        }
    }
}