document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new ByteArrayInputStream(template));
} catch (SAXException e) {
String msg = e.getMessage();
logger.error(msg, e);
throw new LDPathException(msg, e);
} catch (IOException e) {
String msg = e.getMessage();
logger.error(msg, e);
throw new LDPathException(msg, e);
} catch (ParserConfigurationException e) {
String msg = e.getMessage();
logger.error(msg, e);
throw new LDPathException(msg, e);
}
Element schemaNode = document.getDocumentElement();
NodeList fieldsNodeList = schemaNode.getElementsByTagName("fields");
if (fieldsNodeList.getLength() != 1) {
throw new LDPathException("Template is an invalid SOLR schema. It should be a valid a byte array");
}
Node fieldsNode = fieldsNodeList.item(0);
for (FieldMapping<?,Object> fieldMapping : program.getFields()) {
String fieldName = fieldMapping.getFieldName();
String solrType = getSolrFieldType(fieldMapping.getFieldType());
if (solrType == null) {
logger.error("field {} has an invalid field type; ignoring field definition", fieldName);
} else {
Element fieldElement = document.createElement("field");
fieldElement.setAttribute("name", fieldName);
fieldElement.setAttribute("type", solrType);
fieldElement.setAttribute("stored", "true");
fieldElement.setAttribute("indexed", "true");
fieldElement.setAttribute("multiValued", "true");
// Handle extra field configuration
final Map<String,String> fieldConfig = fieldMapping.getFieldConfig();
if (fieldConfig != null) {
for (String attr : fieldConfig.keySet()) {
if (SOLR_FIELD_OPTIONS.contains(attr)) {
fieldElement.setAttribute(attr, fieldConfig.get(attr));
}
}
}
fieldsNode.appendChild(fieldElement);
if (fieldConfig != null && fieldConfig.keySet().contains(SOLR_COPY_FIELD_OPTION)) {
String[] copyFields = fieldConfig.get(SOLR_COPY_FIELD_OPTION).split(",\\s*");
for (String copyField : copyFields) {
Element copyElement = document.createElement("copyField");
copyElement.setAttribute("source", fieldName);
copyElement.setAttribute("dest", copyField);
schemaNode.appendChild(copyElement);
}
} else {
Element copyElement = document.createElement("copyField");
copyElement.setAttribute("source", fieldName);
copyElement.setAttribute("dest", SOLR_ALLTEXT_FIELD);
schemaNode.appendChild(copyElement);
}
}
}
DOMImplementationRegistry registry;
try {
registry = DOMImplementationRegistry.newInstance();
} catch (ClassCastException e) {
String msg = e.getMessage();
logger.error(msg, e);
throw new LDPathException(msg, e);
} catch (ClassNotFoundException e) {
String msg = e.getMessage();
logger.error(msg, e);
throw new LDPathException(msg, e);
} catch (InstantiationException e) {
String msg = e.getMessage();
logger.error(msg, e);
throw new LDPathException(msg, e);
} catch (IllegalAccessException e) {
String msg = e.getMessage();
logger.error(msg, e);
throw new LDPathException(msg, e);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DOMImplementationLS lsImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer lsSerializer = lsImpl.createLSSerializer();