Search in sources :

Example 1 with NSDOMBuilder

use of eu.esdihumboldt.util.groovy.xml.NSDOMBuilder in project hale by halestudio.

the class CustomTypeContentConfigurationType method toDOM.

@Override
public Element toDOM(CustomTypeContentConfiguration value) {
    ValueList list = new ValueList();
    for (CustomTypeContentAssociation assoc : value.getAssociations()) {
        list.add(Value.complex(assoc));
    }
    Map<String, String> prefixes = new HashMap<>();
    prefixes.put("xsd", XMLSchemaIO.NS_HALE_XSD);
    NSDOMBuilder builder;
    try {
        builder = NSDOMBuilder.newBuilder(prefixes);
        Element element = DOMValueUtil.valueTag(builder, "xsd:typeContentConfig", Value.complex(list));
        return element;
    } catch (Exception e) {
        throw new IllegalStateException("Error creating validator DOM representation", e);
    }
}
Also used : CustomTypeContentAssociation(eu.esdihumboldt.hale.io.xsd.anytype.CustomTypeContentAssociation) NSDOMBuilder(eu.esdihumboldt.util.groovy.xml.NSDOMBuilder) ValueList(eu.esdihumboldt.hale.common.core.io.ValueList) HashMap(java.util.HashMap) Element(org.w3c.dom.Element)

Example 2 with NSDOMBuilder

use of eu.esdihumboldt.util.groovy.xml.NSDOMBuilder in project hale by halestudio.

the class AbstractCachedSchemaReader method storeInCache.

/**
 * Stores the schema as HSD DOM.
 *
 * @see AbstractCachedSchemaReaderBase#storeInCache(Schema)
 */
@Override
protected Value storeInCache(Schema schema) throws Exception {
    NSDOMBuilder builder = SchemaToXml.createBuilder();
    Element root = new SchemaToXml().schemaToXml(builder, schema);
    return new ElementValue(root, null);
}
Also used : NSDOMBuilder(eu.esdihumboldt.util.groovy.xml.NSDOMBuilder) Element(org.w3c.dom.Element) SchemaToXml(eu.esdihumboldt.hale.common.schema.persist.hsd.SchemaToXml) ElementValue(eu.esdihumboldt.hale.common.core.io.impl.ElementValue)

Example 3 with NSDOMBuilder

use of eu.esdihumboldt.util.groovy.xml.NSDOMBuilder in project hale by halestudio.

the class HaleSchemaWriter method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Save schema", ProgressIndicator.UNKNOWN);
    try (OutputStream out = getTarget().getOutput()) {
        // create DOM
        NSDOMBuilder builder = SchemaToXml.createBuilder();
        Element root = new SchemaToXml().schemasToXml(builder, getSchemas().getSchemas());
        // configure transformer for serialization
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        // $NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        // $NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        // TODO configurable?!
        // $NON-NLS-1$
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        // serialize DOM
        DOMSource source = new DOMSource(root);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl(e.getMessage(), e));
        reporter.setSuccess(false);
    } finally {
        progress.end();
    }
    return reporter;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) NSDOMBuilder(eu.esdihumboldt.util.groovy.xml.NSDOMBuilder) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) OutputStream(java.io.OutputStream) Element(org.w3c.dom.Element) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Example 4 with NSDOMBuilder

use of eu.esdihumboldt.util.groovy.xml.NSDOMBuilder in project hale by halestudio.

the class AbstractValueConstraintFactoryTest method storeRestoreTest.

/**
 * Test storing a constraint, restoring it and compare both.
 *
 * @param constraint the constraint to store
 * @param typeIndex the type index (as context for storing/restoring),
 *            <code>null</code> for an empty index
 * @param constraintDef the definition the constraint is associated to (as
 *            context for storing/restoring), for <code>null</code> the
 *            method will try to generate a default definition based on the
 *            constraint type
 * @throws Exception if an error occurs during storing, restoring or
 *             comparing the constraint
 */
@SuppressWarnings("unchecked")
protected void storeRestoreTest(T constraint, Map<TypeDefinition, Value> typeIndex, Definition<?> constraintDef) throws Exception {
    // conversion service may be needed for Value conversions
    TestUtil.startConversionService();
    ValueConstraintFactoryDescriptor desc = ValueConstraintExtension.INSTANCE.getForConstraint(constraint);
    // provide defaults for null parameters
    if (typeIndex == null) {
        typeIndex = new HashMap<>();
    }
    if (constraintDef == null) {
        constraintDef = getDefaultConstraintDefinition(constraint.getClass());
    }
    @SuppressWarnings("rawtypes") ValueConstraintFactory factory = desc.getFactory();
    Value val = factory.store(constraint, new MapTypeReferenceBuilder(typeIndex));
    T read;
    if (val != null) {
        // to DOM
        NSDOMBuilder builder = NSDOMBuilder.newBuilder(new HashMap<String, String>());
        Element elem = DOMValueUtil.valueTag(builder, "test", val);
        // from DOM
        Value res = DOMValueUtil.fromTag(elem);
        // bimap for reverse index
        BiMap<TypeDefinition, Value> types = HashBiMap.create(typeIndex);
        read = (T) factory.restore(res, constraintDef, new MapTypeResolver(types.inverse()), new OsgiClassResolver());
    } else {
        // fall back to default constraint
        Class<?> constraintType = ConstraintUtil.getConstraintType(constraint.getClass());
        read = (T) ConstraintUtil.getDefaultConstraint(constraintType, constraintDef);
    }
    compare(constraint, read);
}
Also used : NSDOMBuilder(eu.esdihumboldt.util.groovy.xml.NSDOMBuilder) Element(org.w3c.dom.Element) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition) DefaultTypeDefinition(eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition) ValueConstraintFactoryDescriptor(eu.esdihumboldt.hale.common.schema.model.constraint.factory.extension.ValueConstraintFactoryDescriptor) Value(eu.esdihumboldt.hale.common.core.io.Value)

Aggregations

NSDOMBuilder (eu.esdihumboldt.util.groovy.xml.NSDOMBuilder)4 Element (org.w3c.dom.Element)4 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 ValueList (eu.esdihumboldt.hale.common.core.io.ValueList)1 ElementValue (eu.esdihumboldt.hale.common.core.io.impl.ElementValue)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 TypeDefinition (eu.esdihumboldt.hale.common.schema.model.TypeDefinition)1 ValueConstraintFactoryDescriptor (eu.esdihumboldt.hale.common.schema.model.constraint.factory.extension.ValueConstraintFactoryDescriptor)1 DefaultTypeDefinition (eu.esdihumboldt.hale.common.schema.model.impl.DefaultTypeDefinition)1 SchemaToXml (eu.esdihumboldt.hale.common.schema.persist.hsd.SchemaToXml)1 CustomTypeContentAssociation (eu.esdihumboldt.hale.io.xsd.anytype.CustomTypeContentAssociation)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 Transformer (javax.xml.transform.Transformer)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1