use of datawave.webservice.model.Model in project datawave by NationalSecurityAgency.
the class LoadModelFromXml method loadModelFromXml.
public static QueryModel loadModelFromXml(InputStream stream) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
spf.setNamespaceAware(true);
Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(stream));
JAXBContext ctx = JAXBContext.newInstance(Model.class);
Unmarshaller um = ctx.createUnmarshaller();
Model xmlModel = (Model) um.unmarshal(xmlSource);
if (log.isDebugEnabled()) {
log.debug(xmlModel.getName());
for (FieldMapping fieldMapping : xmlModel.getFields()) {
log.debug(fieldMapping.toString());
}
}
QueryModel model = new QueryModel();
for (FieldMapping mapping : xmlModel.getFields()) {
switch(mapping.getDirection()) {
case FORWARD:
model.addTermToModel(mapping.getModelFieldName(), mapping.getFieldName());
break;
case REVERSE:
model.addTermToReverseModel(mapping.getFieldName(), mapping.getModelFieldName());
break;
default:
log.error("Unknown direction: " + mapping.getDirection());
}
}
if (model.getForwardQueryMapping().isEmpty() && model.getReverseQueryMapping().isEmpty()) {
throw new IllegalArgumentException("The resulting, loaded query model was empty.");
}
return model;
}
use of datawave.webservice.model.Model in project datawave by NationalSecurityAgency.
the class TestCardinalityConfiguration method init.
@Before
public void init() throws Exception {
URL mUrl = TestCardinalityConfiguration.class.getResource("/models/CardinalityModel.xml");
JAXBContext ctx = JAXBContext.newInstance(datawave.webservice.model.Model.class);
Unmarshaller u = ctx.createUnmarshaller();
Model MODEL = (datawave.webservice.model.Model) u.unmarshal(mUrl);
QUERY_MODEL = new QueryModel();
for (FieldMapping mapping : MODEL.getFields()) {
if (mapping.getDirection().equals(Direction.FORWARD)) {
QUERY_MODEL.addTermToModel(mapping.getModelFieldName(), mapping.getFieldName());
} else {
QUERY_MODEL.addTermToReverseModel(mapping.getFieldName(), mapping.getModelFieldName());
}
}
reverseMap = new HashMap<>();
reverseMap.put("FIELD_1", "UUID");
reverseMap.put("FIELD_3", "R_LABEL");
reverseMap.put("FIELD_4", "FIELD_4B");
reverseMap.put("FIELD_2", "PROTOCOL");
}
Aggregations