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);
}
}
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);
}
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;
}
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);
}
Aggregations