Search in sources :

Example 1 with ExtensionType

use of com.helger.xsds.bdxr.smp1.ExtensionType in project peppol-commons by phax.

the class SMPExtensionConverterTest method testConvertFromExtensionType.

@Test
public void testConvertFromExtensionType() {
    // Try converting an empty extension
    assertNull(SMPExtensionConverter.convertToString((ExtensionType) null));
    assertNull(SMPExtensionConverter.convertToString(new ObjectFactory().createExtensionType()));
}
Also used : ObjectFactory(com.helger.xsds.peppol.smp1.ObjectFactory) ExtensionType(com.helger.xsds.peppol.smp1.ExtensionType) Test(org.junit.Test)

Example 2 with ExtensionType

use of com.helger.xsds.bdxr.smp1.ExtensionType in project peppol-commons by phax.

the class SMPExtensionConverterTest method testConvertFromString.

@Test
public void testConvertFromString() {
    // Use elements
    final String sXML = "<any xmlns=\"urn:foo\"><child>text1</child><child2 /></any>";
    final ExtensionType aExtension = SMPExtensionConverter.convert(sXML);
    assertNotNull(aExtension);
    assertNotNull(aExtension.getAny());
    assertNull(SMPExtensionConverter.convert((String) null));
    assertNull(SMPExtensionConverter.convert(""));
    // Convert back to String
    final String sXML2 = SMPExtensionConverter.convertToString(aExtension);
    assertEquals(sXML, sXML2);
    // Cannot convert non-element
    assertNull(SMPExtensionConverter.convert("Plain text"));
}
Also used : ExtensionType(com.helger.xsds.peppol.smp1.ExtensionType) Test(org.junit.Test)

Example 3 with ExtensionType

use of com.helger.xsds.bdxr.smp1.ExtensionType in project peppol-commons by phax.

the class BDXR1ExtensionConverter method convert.

/**
 * Convert the passed Json string to a list of SMP extensions.
 *
 * @param sJson
 *        the Json representation to be converted.
 * @return <code>null</code> if the passed string is empty or cannot be
 *         interpreted as JSON.
 */
@Nullable
public static ICommonsList<ExtensionType> convert(@Nullable final String sJson) {
    if (StringHelper.hasText(sJson)) {
        // Try to interpret as JSON
        final IJson aJson = JsonReader.readFromString(sJson);
        if (aJson == null || !aJson.isArray()) {
            LOGGER.warn("Error in parsing extension JSON '" + sJson + "'");
        } else {
            final ICommonsList<ExtensionType> ret = new CommonsArrayList<>();
            aJson.getAsArray().forEach(aChild -> {
                final IJsonObject aObject = aChild.getAsObject();
                final ExtensionType aExt = new ExtensionType();
                aExt.setExtensionID(aObject.getAsString(JSON_ID));
                aExt.setExtensionName(aObject.getAsString(JSON_NAME));
                aExt.setExtensionAgencyID(aObject.getAsString(JSON_AGENCY_ID));
                aExt.setExtensionAgencyName(aObject.getAsString(JSON_AGENCY_NAME));
                aExt.setExtensionAgencyURI(aObject.getAsString(JSON_AGENCY_URI));
                aExt.setExtensionVersionID(aObject.getAsString(JSON_VERSION_ID));
                aExt.setExtensionURI(aObject.getAsString(JSON_URI));
                aExt.setExtensionReasonCode(aObject.getAsString(JSON_REASON_CODE));
                aExt.setExtensionReason(aObject.getAsString(JSON_REASON));
                final String sAny = aObject.getAsString(JSON_ANY);
                if (StringHelper.hasText(sAny)) {
                    final Document aDoc = DOMReader.readXMLDOM(sAny);
                    if (aDoc != null)
                        aExt.setAny(aDoc.getDocumentElement());
                }
                ret.add(aExt);
            });
            return ret;
        }
    }
    return null;
}
Also used : IJsonObject(com.helger.json.IJsonObject) SMPExtensionType(com.helger.xsds.bdxr.smp2.ec.SMPExtensionType) ExtensionType(com.helger.xsds.bdxr.smp1.ExtensionType) IJson(com.helger.json.IJson) Document(org.w3c.dom.Document) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nullable(javax.annotation.Nullable)

Example 4 with ExtensionType

use of com.helger.xsds.bdxr.smp1.ExtensionType in project peppol-commons by phax.

the class SMPExtensionConverter method convert.

/**
 * Convert the passed XML string to an SMP extension type.
 *
 * @param sXML
 *        the XML representation to be converted.
 * @return <code>null</code> if the passed string is empty or does not
 *         represent valid XML.
 */
@Nullable
public static ExtensionType convert(@Nullable final String sXML) {
    if (StringHelper.hasText(sXML)) {
        // Try to interpret as XML
        final Document aDoc = DOMReader.readXMLDOM(sXML);
        if (aDoc != null) {
            final ExtensionType aExtension = new ExtensionType();
            aExtension.setAny(aDoc.getDocumentElement());
            return aExtension;
        }
    }
    return null;
}
Also used : ExtensionType(com.helger.xsds.peppol.smp1.ExtensionType) Document(org.w3c.dom.Document) Nullable(javax.annotation.Nullable)

Example 5 with ExtensionType

use of com.helger.xsds.bdxr.smp1.ExtensionType in project phoss-smp by phax.

the class SMPServiceGroupTest method testBDXRExtension.

@Test
public void testBDXRExtension() {
    final IParticipantIdentifier aPI = SMPMetaManager.getIdentifierFactory().createParticipantIdentifier(PeppolIdentifierHelper.DEFAULT_PARTICIPANT_SCHEME, "0088:dummy");
    final ExtensionType aExt = new ExtensionType();
    // The extension "any" MUST be in a different namespace that is not empty
    aExt.setAny(DOMReader.readXMLDOM("<foobar1 xmlns='abc'/>").getDocumentElement());
    final ExtensionType aExt2 = new ExtensionType();
    aExt2.setExtensionID("xyz");
    aExt2.setAny(DOMReader.readXMLDOM("<foobar2 xmlns='def'/>").getDocumentElement());
    // Must be an array!
    final SMPServiceGroup aSG = new SMPServiceGroup(CSecurity.USER_ADMINISTRATOR_ID, aPI, BDXR1ExtensionConverter.convertToString(new CommonsArrayList<>(aExt, aExt2)));
    assertTrue(StringHelper.hasText(aSG.getID()));
    assertEquals(CSecurity.USER_ADMINISTRATOR_ID, aSG.getOwnerID());
    assertEquals(aPI, aSG.getParticipantIdentifier());
    assertNotNull(aSG.getExtensionsAsString());
    final com.helger.xsds.bdxr.smp1.ServiceGroupType aSGBDXR = aSG.getAsJAXBObjectBDXR1();
    aSGBDXR.setServiceMetadataReferenceCollection(new com.helger.xsds.bdxr.smp1.ServiceMetadataReferenceCollectionType());
    assertEquals(2, aSGBDXR.getExtension().size());
    final Document aDoc = new BDXR1MarshallerServiceGroupType(true).getAsDocument(aSGBDXR);
    assertNotNull(aDoc);
}
Also used : BDXR1MarshallerServiceGroupType(com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerServiceGroupType) ExtensionType(com.helger.xsds.bdxr.smp1.ExtensionType) Document(org.w3c.dom.Document) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) IParticipantIdentifier(com.helger.peppolid.IParticipantIdentifier) Test(org.junit.Test)

Aggregations

ExtensionType (com.helger.xsds.bdxr.smp1.ExtensionType)5 ExtensionType (com.helger.xsds.peppol.smp1.ExtensionType)4 Test (org.junit.Test)4 Document (org.w3c.dom.Document)4 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 SMPExtensionType (com.helger.xsds.bdxr.smp2.ec.SMPExtensionType)3 Nullable (javax.annotation.Nullable)3 Nonnull (javax.annotation.Nonnull)2 IJson (com.helger.json.IJson)1 IJsonObject (com.helger.json.IJsonObject)1 IParticipantIdentifier (com.helger.peppolid.IParticipantIdentifier)1 BDXR1MarshallerServiceGroupType (com.helger.smpclient.bdxr1.marshal.BDXR1MarshallerServiceGroupType)1 ObjectFactory (com.helger.xsds.peppol.smp1.ObjectFactory)1 ServiceMetadataReferenceCollectionType (com.helger.xsds.peppol.smp1.ServiceMetadataReferenceCollectionType)1 ServiceMetadataReferenceType (com.helger.xsds.peppol.smp1.ServiceMetadataReferenceType)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 JAXBElement (javax.xml.bind.JAXBElement)1 QName (javax.xml.namespace.QName)1