My recent positive experiences with Jackson led me to define my own, new, defacto standard ... Jackson. That is--- after I realized Jackson supported XML - this was a no brainer. Because rather than modelling my document - I could simply print it out as a map - and look at the fields which I wanted , decide on the necessary resolution (i.e. is a List okay --- or do I need a bean for this entity?) --- and then serialize away.
Below is the source code for a SOLR xml parser . This code was obviated very quickly when mister Domen Grabec and I learned that its possible to simply create a SolrCore object by using an AbstractSolrBaseTest object (we have'nt yet found a simple way to create a SolrCore object yet, but I'm sure there is a trick buried somewhere in the API). Nevertheless, this test is functionally identical and requires no inheritance, and no external libraries, and most importantly, demonstrates how easy it is to de/serialize xml objects to validate xml input/output in an application using Jackson.
The purpose of this class is simply to make sure that a SolrIndex creator is creating field names that correspond to the dynamic and/or static field names defined in the Solr xml schema. It could similarly be used, thus, to validate a hibernate schema . That is, it could be genericized to validate that any "map-like" data class had fields which corresponded to field names (including regular expressions for names) found in an XML file.
//You have to create your own *XMLSchema class. The easiest way to start with this is to create a class with a few simple fields that map to some of the top level attributes in your XML data. Jackson will magically take care of the rest with Lists/Maps.
//This method reads in a solr schema
public static List<Pattern> fields(String xmlFileName) throws IOException, Exception {
File pathToSolrSchema = new File(xmlFileName);
String schema = FileUtils.readFileToString(pathToSolrSchema );
SolrXMLSchema nodes = new XmlMapper().readValue(schema, MyXMLSchemaBean.class);
List<Pattern> fieldNames = new ArrayList<Pattern>();
for(Map l : nodes.getFields())
{
String fieldPattern = l.get("name").toString();
fieldPattern=fieldPattern.replaceAll("\\*", ".*");
fieldPattern = StringEscapeUtils.escapeJava(fieldPattern);
fieldNames.add(Pattern.compile(fieldPattern) );
}
return fieldNames;
}
Oh by the way -- heres the SolrSchemaBean that I used to test this.
package net.peerindex.pisae.solr;
import java.util.List;
import java.util.Map;
/**
* Can be used to manually parse the solr index files.
* Now not necssary -- now we just use @link{TestHarness}.getCore().
* @author Jpeerindex
*/
public class SolrXMLSchema {
public Object getDefaultSearchField() {
return defaultSearchField;
}
public void setDefaultSearchField(Object defaultSearchField) {
this.defaultSearchField = defaultSearchField;
}
Object name,solrQueryParser,types,uniqueKey,version,defaultSearchField;
List<Map> fields;
public Object getName() {
return name;
}
public void setName(Object name) {
this.name = name;
}
public Object getSolrQueryParser() {
return solrQueryParser;
}
public void setSolrQueryParser(Object solrQueryParser) {
this.solrQueryParser = solrQueryParser;
}
public Object getTypes() {
return types;
}
public void setTypes(Object types) {
this.types = types;
}
public Object getUniqueKey() {
return uniqueKey;
}
public void setUniqueKey(Object uniqueKey) {
this.uniqueKey = uniqueKey;
}
public Object getVersion() {
return version;
}
public void setVersion(Object version) {
this.version = version;
}
public List<Map> getFields() {
return fields;
}
public void setFields(List<Map> fields) {
this.fields = fields;
}
}
Oh by the way -- heres the SolrSchemaBean that I used to test this.
package net.peerindex.pisae.solr;
import java.util.List;
import java.util.Map;
/**
* Can be used to manually parse the solr index files.
* Now not necssary -- now we just use @link{TestHarness}.getCore().
* @author Jpeerindex
*/
public class SolrXMLSchema {
public Object getDefaultSearchField() {
return defaultSearchField;
}
public void setDefaultSearchField(Object defaultSearchField) {
this.defaultSearchField = defaultSearchField;
}
Object name,solrQueryParser,types,uniqueKey,version,defaultSearchField;
List<Map> fields;
public Object getName() {
return name;
}
public void setName(Object name) {
this.name = name;
}
public Object getSolrQueryParser() {
return solrQueryParser;
}
public void setSolrQueryParser(Object solrQueryParser) {
this.solrQueryParser = solrQueryParser;
}
public Object getTypes() {
return types;
}
public void setTypes(Object types) {
this.types = types;
}
public Object getUniqueKey() {
return uniqueKey;
}
public void setUniqueKey(Object uniqueKey) {
this.uniqueKey = uniqueKey;
}
public Object getVersion() {
return version;
}
public void setVersion(Object version) {
this.version = version;
}
public List<Map> getFields() {
return fields;
}
public void setFields(List<Map> fields) {
this.fields = fields;
}
}
No comments:
Post a Comment