Search in sources :

Example 1 with TypeDictionary

use of org.opcfoundation.opcua.binaryschema.TypeDictionary in project milo by eclipse.

the class DataTypeDictionaryGenerator method writeToOutputStream.

public void writeToOutputStream(OutputStream outputStream) throws IOException {
    TypeDictionary typeDictionary = new TypeDictionary();
    typeDictionary.setDefaultByteOrder(ByteOrder.LITTLE_ENDIAN);
    typeDictionary.setTargetNamespace(namespaceUri);
    enumeratedTypes.forEach(t -> typeDictionary.getOpaqueTypeOrEnumeratedTypeOrStructuredType().add(t));
    structuredTypes.forEach(t -> typeDictionary.getOpaqueTypeOrEnumeratedTypeOrStructuredType().add(t));
    namespaces.forEach(namespace -> {
        ImportDirective importDirective = new ImportDirective();
        importDirective.setNamespace(namespace);
        typeDictionary.getImport().add(importDirective);
    });
    try {
        JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        try {
            marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new OpcUaNamespacePrefixMapper());
        } catch (PropertyException e) {
            logger.debug("NamespacePrefixMapper not supported", e);
        }
        marshaller.marshal(typeDictionary, outputStream);
    } catch (Throwable t) {
        throw new IOException("failed to write dictionary to OutputStream", t);
    }
}
Also used : ImportDirective(org.opcfoundation.opcua.binaryschema.ImportDirective) Marshaller(javax.xml.bind.Marshaller) PropertyException(javax.xml.bind.PropertyException) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) TypeDictionary(org.opcfoundation.opcua.binaryschema.TypeDictionary)

Example 2 with TypeDictionary

use of org.opcfoundation.opcua.binaryschema.TypeDictionary in project milo by eclipse.

the class BsdParser method parse.

/**
 * Parse an XML document containing a type dictionary conforming to the OPC Binary XML Schema.
 *
 * @param inputStream the {@link InputStream} to read the XML document from.
 * @return a {@link DictionaryDescription}.
 * @throws JAXBException if parsing fails.
 */
public DictionaryDescription parse(InputStream inputStream) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
    TypeDictionary typeDictionary = (TypeDictionary) context.createUnmarshaller().unmarshal(inputStream);
    List<CodecDescription> enumCodecs = typeDictionary.getOpaqueTypeOrEnumeratedTypeOrStructuredType().stream().filter(typeDescription -> typeDescription instanceof EnumeratedType).map(typeDescription -> {
        EnumeratedType enumeratedType = (EnumeratedType) typeDescription;
        logger.debug("EnumeratedType: {}", typeDescription.getName());
        return new CodecDescription(getEnumCodec(enumeratedType), enumeratedType.getName());
    }).collect(toList());
    List<CodecDescription> structCodecs = typeDictionary.getOpaqueTypeOrEnumeratedTypeOrStructuredType().stream().filter(typeDescription -> typeDescription instanceof StructuredType).map(typeDescription -> {
        StructuredType structuredType = (StructuredType) typeDescription;
        logger.debug("StructuredType: {}", typeDescription.getName());
        return new CodecDescription(getStructCodec(structuredType), structuredType.getName());
    }).collect(toList());
    return new DictionaryDescription(typeDictionary.getTargetNamespace(), enumCodecs, structCodecs);
}
Also used : StructuredType(org.opcfoundation.opcua.binaryschema.StructuredType) List(java.util.List) OpcUaBinaryDataTypeCodec(org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec) Collectors.toList(java.util.stream.Collectors.toList) Logger(org.slf4j.Logger) ObjectFactory(org.opcfoundation.opcua.binaryschema.ObjectFactory) TypeDictionary(org.opcfoundation.opcua.binaryschema.TypeDictionary) LoggerFactory(org.slf4j.LoggerFactory) EnumeratedType(org.opcfoundation.opcua.binaryschema.EnumeratedType) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) InputStream(java.io.InputStream) EnumeratedType(org.opcfoundation.opcua.binaryschema.EnumeratedType) JAXBContext(javax.xml.bind.JAXBContext) TypeDictionary(org.opcfoundation.opcua.binaryschema.TypeDictionary) StructuredType(org.opcfoundation.opcua.binaryschema.StructuredType)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)2 TypeDictionary (org.opcfoundation.opcua.binaryschema.TypeDictionary)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1 PropertyException (javax.xml.bind.PropertyException)1 OpcUaBinaryDataTypeCodec (org.eclipse.milo.opcua.stack.core.serialization.codecs.OpcUaBinaryDataTypeCodec)1 EnumeratedType (org.opcfoundation.opcua.binaryschema.EnumeratedType)1 ImportDirective (org.opcfoundation.opcua.binaryschema.ImportDirective)1 ObjectFactory (org.opcfoundation.opcua.binaryschema.ObjectFactory)1 StructuredType (org.opcfoundation.opcua.binaryschema.StructuredType)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1