Apache POI can not add CustomProperty to Doc - java

I'm trying to add some Custom Properties to an existing document:
HWPFDocument document = new HWPFDocument(new FileInputStream(sourceFile));
DocumentSummaryInformation docSumInf = document.getDocumentSummaryInformation();
CustomProperties customProperties = docSumInf.getCustomProperties();
CustomProperty singleProp = null;
//...
singleProp = new CustomProperty();
singleProp.setName(entry.getKey());
singleProp.setType(Variant.VT_LPWSTR);
singleProp.setValue((String) entry.getValue());
//..
customProperties.put(entry.getKey(), singleProp);
docSumInf.setCustomProperties(customProperties);
return document;
However, the properties never make it to the file. I tried to
document.getDocumentSummaryInformation().getCustomProperties().putAll(customProperties);
I also tried
document.getDocumentSummaryInformation().getCustomProperties().put(entry.getKey(), singleProp);
System.out.println(document.getDocumentSummaryInformation().getCustomProperties().size() + " Elemente in Map");
in a loop. The printed size was allways one.
With the first attemp (docSumInf.setCustomProperties(customProperties);) I printed out customProperties before setting it to docSumInf. There where all new Properties I miss, as soon as I set them to the document summary.
I don't see what I am missing...

entry.getKey() = null
or entry.getKey() has common value for all CustomProperties in the map.
and because of that you have only one element in the map of CustomProperties.
You need to set non null values here
singleProp.setName(entry.getKey());
CustomProperty class represents custom properties in the document summary information stream. The difference to normal properties is that custom properties have an optional name. If the name is not null it will be maintained in the section's dictionary.

Related

How to write List<Number> with OpenCSV?

I have the following field inside a StacItem Object:
#JsonProperty
private List<Number> bbox = null;
I made a basic implementation with OpenCSV to write this Object into a CSV, and it mostly works with this code (i'm showing just the relevant part):
final StatefulBeanToCsv<Object> beanToCSV = new StatefulBeanToCsvBuilder<>(writer)
.withSeparator(';')
.build();
for(StacItem item : items){
beanToCSV.write(item);
}
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set(HttpHeaders.CONTENT_TYPE, "text/csv");
httpHeaders.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=exportItems.csv");
logger.info("END WRITING");
return new ResponseEntity<>(new FileSystemResource(file), HttpStatus.OK);
Here's the twist! In the logs of my Microservice, I see the full structure of that StacItem, and it should have this bbox field:
bbox=[8.24275148213394, 45.5050129344147, 7.62767704092889, 45.0691351737573]
While my implementation returns just this:
"8.24637830863774"
So when I open my CSV I just found the column "bbox" with one value but I need the others too..Can you please tell me why it stops on the first one or how to get the others 3?
UPDATE:
I found that this does the trick! But then...it exports just this single field for every StacItem so I lose every other field in my Object.
#CsvBindAndSplitByName(elementType = Number.class, writeDelimiter = ",")
#JsonProperty("bbox")
private List<Number> bbox = null;
Thanks
Try using CsvBindByName on every field you want to map (specify the column attribute of the annotation is not mandatory).
You can even use CsvBindByPosition if you prefer.
Did you try to change ?
beanToCSV.write(item); -> beanToCSV.writeNext(item);
or
for(StacItem item : items){
beanToCSV.write(item);
}
// to
beanToCSV.write(items);

How to extract Wikipedia entity matched to CoreEntityMention (WikiDictAnnotator)

I am running CoreNLP over some text, and matching the entities found to Wikipedia entities. I want to reconstruct the sentence providing the link and other useful information for the entities found.
The CoreEntityMention has an entity() method, but it just returns a String.
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,entitylink");
// set up pipeline
pipeline = new StanfordCoreNLP(props);
String doc = "text goes here";
pipeline.annotate(doc);
// Iterate the sentences
for (CoreSentence sentence : doc.sentences()) {
Go through all mentions
for (CoreEntityMention em : sentence.entityMentions()) {
System.out.println(em.sentence());
// Here I would like to extract the Wikipedia entity information
System.out.println(em.entity());
}
}
You just need to add the wikipedia page url.
So Neil_Armstrong maps to https://en.wikipedia.org/wiki/Neil_Armstrong.

Remove paragraph style with docx4j

In my word template file I have some tables and sometimes the second column of them is formatted as an enumeration.
Using docx4j I'm filling it with dynamic content and if there's only one entry I need to get rid of the enumeration style.
I found a place deep down in the structure that has a value for enumeration but when setting it to null, I don't see any changes in my template.
//This value is "Listenabsatz" (German) and I want to get rid of it
//Setting this value to "" or setting pStyle to null didn't help
((PStyle)((PPr)((P)((java.util.ArrayList)((Tc)((JAXBElement)templateRow.content.get(1)).value).content).get(0)).pPr).pStyle).val
In my actual code this is the place where I'm trying to change it:
Tr templateRow = (Tr) rows.get(0);
Tc cell = (Tc) ((javax.xml.bind.JAXBElement) templateRow.getContent().get(1)).getValue();
P par = (P) (cell.getContent().get(0));
PPr parStyle = par.getPPr();
if (parStyle.getPStyle() != null && parStyle.getPStyle().getVal() != null) {
parStyle.setPStyle(null);
//parStyle.getPStyle().setVal("");
}
How can I remove that enumeration style succesfully?

How to dynamically create a document in the GAE Search API?

I am trying to understand how i can dynamically add fields in my document.
I am following the doc . However i'd like to use the addField method inside a "for" statement and thus programmatically generate fields.
I've tried this :
Builder document = Document.newBuilder();
document.setId(id);
// for statement start
com.google.appengine.api.search.Field.Builder field = Field.newBuilder().setName(key).setAtom(value);
document.addField(field);
// for statement end
document.build();
But it doesn't seems to work. When i'm trying i got this printed out
com.google.appengine.api.search.Document$Builder#2604ed54
Whereas when i use the original code :
String myDocId = "PA6-5000";
Document doc = Document.newBuilder()
// Setting the document identifer is optional.
// If omitted, the search service will create an identifier.
.setId(myDocId)
.addField(Field.newBuilder().setName("content").setText("the rain in spain"))
.addField(Field.newBuilder().setName("email").setText(userEmail))
.addField(Field.newBuilder().setName("domain").setAtom(userDomain))
.addField(Field.newBuilder().setName("published").setDate(new Date()))
.build();
I get the expected result with the code above.
How i can manipulate this in order to put addfield() into for statement ?
thanks for helping.
The docs imply the Document.Builder is mutable, but typically you should reassign the builder after each invocation in case it isn't.
Builder document = Document.newBuilder();
document.setId(id);
com.google.appengine.api.search.Field.Builder field = Field.newBuilder().setName(key).setAtom(value);
document = document.addField(field);
document.build();
In your case what you're seeing is the toString() method on the builder, not the document itself. The problem is the last line.
Try this:
Builder builder = Document.newBuilder();
builder.setId(id);
com.google.appengine.api.search.Field.Builder field = Field.newBuilder().setName(key).setAtom(value);
builder = builder.addField(field);
Document document = builder.build();
Then you can just drop in your for loop and away you go.

Using OntProperty and DatatypeProperty - Jena Ontology

OntModel onto = ModelFactory.createOntologyModel(
OntModelSpec.OWL_MEM_MICRO_RULE_INF, null );
String inputFileName = "./src/test.xml";
InputStream in = FileManager.get().open(inputFileName);
if (in == null) {
throw new IllegalArgumentException( "File: " + inputFileName + " not found");
}
onto.read(new InputStreamReader(in), "");
//ns is the namespace...
OntClass userClass = onto.getOntClass(ns+"User");
Individual dada = onto.createIndividual(ns+"Daryl", userClass);
Property prefBathtub = onto.getProperty(ns+"prefersBathtub");
Property prefBathtubWt = onto.getProperty(ns+"prefersBathtubWeight");
dada.addLiteral(prefBathtub, true);
dada.addLiteral(prefBathtubWt, 0.30);
OutputStream out = new FileOutputStream("./src/test2.xml");
onto.write( out, "RDF/XML"); // readable rdf/xml
out.close();
How do I use OntProperty and/or DatatypeProperty instead of just Property?
By using Property do I get the same amount of expressiveness?
To get an ObjectProperty object from an ontology model, use OntModel.getObjectProperty(). Likewise for datatype properties, etc. The Ont classes are more expressive in the sense that they contain convenience API for getting, for example, the super-properties of a property, with one method call. However, as the convenience API only accesses the underlying triples in the graph, there is strictly speaking nothing you can do with an ObjectProperty that you can't do with a Property. It's just harder work!
Incidentally, Jena allows you to access other facets of an underlying RDF resource with the .as() method. So:
Property p = myModel.getProperty( "http://example.com/foo#p" );
OntProperty op = p.as( OntProperty.class );

Categories

Resources