Example usage for javax.xml.bind JAXBElement getValue

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

Introduction

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

Prototype

public T getValue() 

Source Link

Document

Return the content model and attribute values for this element.

See #isNil() for a description of a property constraint when this value is null

Usage

From source file:be.fedict.eid.dss.spi.utils.XAdESUtils.java

@SuppressWarnings("unchecked")
public static <T> T unmarshall(Element xadesElement, Class<T> xadesType) throws XAdESValidationException {
    JAXBElement<T> jaxbElement;
    try {/*from  w  w  w .  j a va 2s .com*/
        jaxbElement = (JAXBElement<T>) xadesUnmarshaller.unmarshal(xadesElement);
    } catch (JAXBException e) {
        throw new XAdESValidationException(e);
    }
    T value = jaxbElement.getValue();
    return value;
}

From source file:com.evolveum.midpoint.schema.util.PolicyRuleTypeUtil.java

@SuppressWarnings("unchecked")
private static void resolveLocalReferences(PolicyConstraintsType pc, ConstraintResolver resolver)
        throws ObjectNotFoundException, SchemaException {
    for (PolicyConstraintReferenceType ref : pc.getRef()) {
        String refName = ref != null ? ref.getName() : null;
        if (StringUtils.isEmpty(refName)) {
            throw new SchemaException("Illegal empty reference: " + ref);
        }/* w w w .  j  a va 2  s. c om*/
        List<String> pathToRoot = getPathToRoot(pc);
        if (pathToRoot.contains(refName)) {
            throw new SchemaException("Trying to resolve cyclic reference to constraint '" + refName
                    + "'. Contained in: " + pathToRoot);
        }
        JAXBElement<? extends AbstractPolicyConstraintType> resolved = resolver.resolve(refName);
        QName constraintName = resolved.getName();
        AbstractPolicyConstraintType constraintValue = resolved.getValue();
        PrismContainer<? extends AbstractPolicyConstraintType> container = pc.asPrismContainerValue()
                .findOrCreateContainer(constraintName);
        container.add(constraintValue.asPrismContainerValue().clone());
    }
    pc.getRef().clear();
}

From source file:com.evolveum.midpoint.pwdfilter.opendj.PasswordPusher.java

@SuppressWarnings("unchecked")
private static <T> T unmarshallResouce(String path) throws JAXBException, FileNotFoundException {
    JAXBContext jc = ModelClientUtil.instantiateJaxbContext();
    Unmarshaller unmarshaller = jc.createUnmarshaller();

    InputStream is = null;/*ww w. ja v  a 2s .  com*/
    JAXBElement<T> element = null;
    try {
        is = PasswordPusher.class.getClassLoader().getResourceAsStream(path);
        if (is == null) {
            throw new FileNotFoundException("System resource " + path + " was not found");
        }
        element = (JAXBElement<T>) unmarshaller.unmarshal(is);
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
    if (element == null) {
        return null;
    }
    return element.getValue();
}

From source file:fr.cls.atoll.motu.processor.wps.TestServiceMetadata.java

public static void testLoadOGCServiceMetadata() {
    // String xmlFile =
    // "J:/dev/atoll-v2/atoll-motu/atoll-motu-processor/src/test/resources/xml/TestServiceMetadata.xml";
    String xmlFile = "C:/Documents and Settings/dearith/Mes documents/Atoll/SchemaIso/TestServiceMetadataOK.xml";

    String schemaPath = "schema/iso19139";

    try {/*  ww  w .  j  a  v a2 s .  c  o m*/
        List<String> errors = validateServiceMetadataFromString(xmlFile, schemaPath);
        if (errors.size() > 0) {
            StringBuffer stringBuffer = new StringBuffer();
            for (String str : errors) {
                stringBuffer.append(str);
                stringBuffer.append("\n");
            }
            throw new MotuException(String.format("ERROR - XML file '%s' is not valid - See errors below:\n%s",
                    xmlFile, stringBuffer.toString()));
        } else {
            System.out.println(String.format("XML file '%s' is valid", xmlFile));
        }

    } catch (MotuException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    InputStream in = null;
    try {
        in = Organizer.getUriAsInputStream(xmlFile);
    } catch (MotuException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    JAXBContext jc = null;
    try {
        // jc = JAXBContext.newInstance("org.isotc211.iso19139.d_2006_05_04.srv");
        // jc = JAXBContext.newInstance("org.isotc211.iso19139.d_2006_05_04.srv");
        jc = JAXBContext
                .newInstance(new Class[] { org.isotc211.iso19139.d_2006_05_04.srv.ObjectFactory.class });
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Source srcFile = new StreamSource(xmlFile);
        JAXBElement<?> element = (JAXBElement<?>) unmarshaller.unmarshal(srcFile);
        // JAXBElement<?> element = (JAXBElement<?>) unmarshaller.unmarshal(in);
        SVServiceIdentificationType serviceIdentificationType = (SVServiceIdentificationType) element
                .getValue();
        // serviceIdentificationType = (SVServiceIdentificationType) unmarshaller.unmarshal(in);
        System.out.println(serviceIdentificationType.toString());

        List<SVOperationMetadataPropertyType> operationMetadataPropertyTypeList = serviceIdentificationType
                .getContainsOperations();

        for (SVOperationMetadataPropertyType operationMetadataPropertyType : operationMetadataPropertyTypeList) {

            SVOperationMetadataType operationMetadataType = operationMetadataPropertyType
                    .getSVOperationMetadata();
            System.out.println("---------------------------------------------");
            if (operationMetadataType == null) {
                continue;
            }
            System.out.println(operationMetadataType.getOperationName().getCharacterString().getValue());
            System.out.println(operationMetadataType.getInvocationName().getCharacterString().getValue());
            System.out.println(operationMetadataType.getOperationDescription().getCharacterString().getValue());

            CIOnlineResourcePropertyType onlineResourcePropertyType = operationMetadataType.getConnectPoint()
                    .get(0);
            if (onlineResourcePropertyType != null) {
                System.out.println(operationMetadataType.getConnectPoint().get(0).getCIOnlineResource()
                        .getLinkage().getURL());
            }

            List<SVParameterPropertyType> parameterPropertyTypeList = operationMetadataType.getParameters();

            for (SVParameterPropertyType parameterPropertyType : parameterPropertyTypeList) {
                SVParameterType parameterType = parameterPropertyType.getSVParameter();

                if (parameterType.getName().getAName().getCharacterString() != null) {
                    System.out.println(parameterType.getName().getAName().getCharacterString().getValue());
                } else {
                    System.out.println("WARNING - A parameter has no name");

                }
                if (parameterType.getDescription() != null) {
                    if (parameterType.getDescription().getCharacterString() != null) {
                        System.out.println(parameterType.getDescription().getCharacterString().getValue());
                    } else {
                        System.out.println("WARNING - A parameter has no description");

                    }
                } else {
                    System.out.println("WARNING - A parameter has no description");

                }
            }

        }
        FileWriter writer = new FileWriter("c:/tempVFS/test.xml");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(element, writer);

        writer.flush();
        writer.close();

        System.out.println("End testLoadOGCServiceMetadata");
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.vmware.vchs.api.samples.services.Compute.java

/**
 * This method uses the vCloud Query API to retrieve vCloud VDC Templates. It will return a
 * collection of VAppTemplateType instances for each template retrieved.
 * /*from w  w  w.  j ava  2  s.  c om*/
 * @param vdc
 *            the VDC to find and return all templates from
 */
public static final Collection<VAppTemplateType> getTemplatesForVdc(String computeUrl, VdcType vdc,
        String version, String token) {
    // Query the vCloud Query API to search for a vAppTemplate matching the
    // options.templateName (command line option --templatename)
    QueryResultRecordsType queryResults = HttpUtils.getQueryResults(getBaseUrl(computeUrl), "type=vAppTemplate",
            version, token);

    List<JAXBElement<? extends QueryResultRecordType>> rslt = queryResults.getRecord();

    // We should have only one record with the name matching templateName
    if (null != rslt && rslt.size() > 0) {
        Collection<VAppTemplateType> templates = new ArrayList<VAppTemplateType>();

        for (JAXBElement<? extends QueryResultRecordType> record : rslt) {
            if (record.getDeclaredType().equals(QueryResultVAppTemplateRecordType.class)) {
                QueryResultVAppTemplateRecordType template = (QueryResultVAppTemplateRecordType) record
                        .getValue();

                // invoke the GET request to the template href to get the VAppTemplateType
                HttpGet get = new HttpGet(template.getHref());
                get.setHeader(HttpHeaders.ACCEPT,
                        SampleConstants.APPLICATION_PLUS_XML_VERSION + version + ";charset=utf-8");
                get.setHeader(SampleConstants.VCD_AUTHORIZATION_HEADER, token);
                HttpResponse response = HttpUtils.httpInvoke(get);

                // make sure the status is 200 OK
                if (null != response && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    // unmarshal the response entity into a VAppTemplateType
                    VAppTemplateType vat = HttpUtils.unmarshal(response.getEntity(), VAppTemplateType.class);
                    templates.add(vat);
                }
            }
        }

        return templates;
    }

    return null;
}

From source file:com.evolveum.midpoint.model.client.ModelClientUtil.java

public static <O> O unmarshallResource(String path) throws JAXBException, FileNotFoundException {
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    InputStream is = null;/*from w  w  w  . j av  a 2  s.  c o m*/
    JAXBElement<O> element = null;
    try {
        is = ModelClientUtil.class.getClassLoader().getResourceAsStream(path);
        if (is == null) {
            throw new FileNotFoundException("System resource " + path + " was not found");
        }
        element = (JAXBElement<O>) unmarshaller.unmarshal(is);
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
    if (element == null) {
        return null;
    }
    return element.getValue();
}

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

private static TremoloType loadTremoloType(String unisonXMLFile) throws Exception {
    JAXBContext jc = JAXBContext.newInstance("com.tremolosecurity.config.xml");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    FileInputStream in = new FileInputStream(unisonXMLFile);

    Object obj = unmarshaller.unmarshal(in);

    JAXBElement<TremoloType> cfg = (JAXBElement<TremoloType>) obj;
    return cfg.getValue();
}

From source file:Main.java

public static <T> T unmarshal(Reader r, Class<T> clazz)
        throws JAXBException, XMLStreamException, FactoryConfigurationError {
    JAXBContext context = s_contexts.get(clazz);
    if (context == null) {
        context = JAXBContext.newInstance(clazz);
        s_contexts.put(clazz, context);/*from  w  w w.  j  av  a  2s  . c  om*/
    }

    ValidationEventCollector valEventHndlr = new ValidationEventCollector();
    XMLStreamReader xmlsr = XMLInputFactory.newFactory().createXMLStreamReader(r);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setSchema(null);
    unmarshaller.setEventHandler(valEventHndlr);

    JAXBElement<T> elem = null;
    try {
        elem = unmarshaller.unmarshal(xmlsr, clazz);
    } catch (Exception e) {
        if (e instanceof JAXBException) {
            throw (JAXBException) e;
        } else {
            throw new UnmarshalException(e.getMessage(), e);
        }
    }

    if (valEventHndlr.hasEvents()) {
        for (ValidationEvent valEvent : valEventHndlr.getEvents()) {
            if (valEvent.getSeverity() != ValidationEvent.WARNING) {
                // throw a new Unmarshall Exception if there is a parsing error
                String msg = MessageFormat.format("Line {0}, Col: {1}: {2}",
                        valEvent.getLocator().getLineNumber(), valEvent.getLocator().getColumnNumber(),
                        valEvent.getLinkedException().getMessage());
                throw new UnmarshalException(msg, valEvent.getLinkedException());
            }
        }
    }
    return elem.getValue();
}

From source file:com.qpark.eip.core.spring.JAXBElementValueGetterTransformer.java

/**
 * @param message//from   w ww  . j  av a  2  s. co m
 * @return The value out of the {@link JAXBElement}
 */
@SuppressWarnings("unchecked")
public Message<? extends T> transform(final Message<JAXBElement<?>> message) {
    JAXBElement<?> payload = message.getPayload();
    return MessageBuilder.withPayload((T) payload.getValue()).copyHeaders(message.getHeaders()).build();
}

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

private static TremoloType loadTremoloType(String unisonXMLFile, CommandLine cmd, Options options)
        throws Exception {
    JAXBContext jc = JAXBContext.newInstance("com.tremolosecurity.config.xml");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    InputStream in = null;/*  w w w .j  av  a 2 s.c om*/

    String envFile = cmd.getOptionValue("envFile");
    if (envFile != null) {
        BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(envFile)));
        String line = null;
        while ((line = fin.readLine()) != null) {
            String name = line.substring(0, line.indexOf('='));
            String val = line.substring(line.indexOf('=') + 1);
            System.setProperty(name, val);
        }

        String withProps = OpenUnisonConfigLoader.generateOpenUnisonConfig(unisonXMLFile);
        in = new ByteArrayInputStream(withProps.getBytes("UTF-8"));

    } else {
        in = new FileInputStream(unisonXMLFile);
    }

    Object obj = unmarshaller.unmarshal(in);

    JAXBElement<TremoloType> cfg = (JAXBElement<TremoloType>) obj;
    return cfg.getValue();
}