Skip to content

Commit

Permalink
Inclusion de meta XMP dans les PDFs
Browse files Browse the repository at this point in the history
  • Loading branch information
tfrancart committed Jan 26, 2016
1 parent e3cf3ac commit 211ad2a
Show file tree
Hide file tree
Showing 43 changed files with 7,756 additions and 6,505 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ build/
target/
.sesame-toolkit-repositories/
pom.xml.releaseBackup
.DisplayPrinter-debug.xml
.FopProcessor-debug.xml
output.pdf
display-test.pdf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* An abstraction for objects capable of constructing a SPARQL query, telling how
* to map the key to the SPARQL query, and how to read the correspondance <Key, Value>
* to map the key to the SPARQL query, and how to read the mapping <Key, Value>
* from a result line of the executed query.
*
* @param <Key> The type of the key being read (typically Resource or URILang)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import fr.sparna.rdf.sesame.toolkit.query.SparqlQuery;
import fr.sparna.rdf.sesame.toolkit.query.builder.SparqlQueryBuilder;
import fr.sparna.rdf.sesame.toolkit.query.builder.ValuesSparqlQueryBuilder;
import fr.sparna.rdf.sesame.toolkit.reader.KeyValueHelperBase;
import fr.sparna.rdf.sesame.toolkit.reader.KeyValueHelperIfc;

/**
* Returns a list of values for an ordered list of properties, for given resource or list of resources, in a given language.
Expand Down Expand Up @@ -329,6 +331,14 @@ public void setProperties(List<java.net.URI> properties) {
this.properties = properties;
}

public String getPreferredLanguage() {
return preferredLanguage;
}

public List<String> getFallbackLanguages() {
return fallbackLanguages;
}

private List<Value> findValues(List<SparqlQuery> queries)
throws SparqlPerformException {
ReadValueListHandler h = new ReadValueListHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ public static void main(String... args) throws Exception {
KosDocumentHeader header = headerReader.readHeader(LANG, (args.length > 1)?URI.create(args[1]):null);
document.setHeader(header);

// build and set metadata
DocumentMetadataReader metaReader = new DocumentMetadataReader(r);
document.setKosDocumentMetadata(
metaReader.readKosDocumentMetadata(LANG, (args.length > 1)?URI.create(args[1]):null)
);

ConceptBlockReader cbReader = new ConceptBlockReader(r);
cbReader.setSkosPropertiesToRead(EXPANDED_SKOS_PROPERTIES_WITH_MT);
cbReader.setAdditionalLabelLanguagesToInclude(Arrays.asList(new String[] { "en", "es", "ru" }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class ConceptBlockReader {
// prefLabelReader
protected PropertyReader prefLabelReader;

// notationReader
protected PropertyReader notationReader;

// should we include linguistic equivalents ?
protected List<String> additionalLabelLanguagesToInclude = null;
// use a TreeMap to garantee ordering by language code
Expand Down Expand Up @@ -82,6 +85,12 @@ protected void initInternal(
);
// if we need to disable preload
prefLabelReader.setPreLoad(false);

notationReader = new PropertyReader(
this.repository,
URI.create(SKOS.NOTATION)
);
notationReader.setPreLoad(false);

// setup additional readers
this.additionalReaders = new HashMap<String, Object>();
Expand Down Expand Up @@ -161,10 +170,16 @@ public ConceptBlock readConceptBlockForSynonym(final String uri, final String al
}


public ConceptBlock readConceptBlock(final String uri, boolean styleLabel)
public ConceptBlock readConceptBlock(final String uri, boolean styleLabel, boolean prependNotation)
throws SparqlPerformException {
// set sourceConceptLabel (or URI if no sourceConceptLabel can be found)
String label = LabelReader.display(prefLabelReader.read(URI.create(uri)));

if(prependNotation) {
List<Value> notations = notationReader.read(URI.create(uri));
label = ((notations.size() > 0)?notations.get(0).stringValue()+" ":"")+label;
}

label = (label.trim().equals(""))?uri:label;
return this.readConceptBlock(uri, label, styleLabel);
}
Expand Down Expand Up @@ -266,11 +281,19 @@ public ConceptBlock readConceptBlock(final String uri, String prefLabel, String
for (org.openrdf.model.URI aValue : values) {
List<Value> prefs = prefLabelReader.read(URI.create(aValue.stringValue()));
String refPrefLabel = (prefs.size() > 0)?prefs.get(0).stringValue():aValue.stringValue();

String refNotation = null;
if(entry.getKey().equals(SKOSPLAY.MEMBER_OF)) {
// in case we are referencing a collection / micro-thesaurus, attempt to fetch the notation (UNESCO thesaurus)
List<Value> notations = notationReader.read(URI.create(aValue.stringValue()));
refNotation = (notations.size() > 0)?notations.get(0).stringValue():null;
}

cb.getAtt().add(
SchemaFactory.createAttLink(
computeRefId(aValue.stringValue(), refPrefLabel, true),
aValue.stringValue(),
refPrefLabel,
((refNotation != null)?refNotation+" ":"")+refPrefLabel,
SKOSTags.getString(entry.getKey()),
(styleAttributes)?"pref":null
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package fr.sparna.rdf.skos.printer.reader;

import java.net.URI;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

import org.openrdf.model.Literal;
import org.openrdf.model.Resource;
import org.openrdf.model.Value;
import org.openrdf.model.vocabulary.DC;
import org.openrdf.model.vocabulary.DCTERMS;
import org.openrdf.query.TupleQueryResultHandlerException;
import org.openrdf.repository.Repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.sparna.rdf.sesame.toolkit.query.Perform;
import fr.sparna.rdf.sesame.toolkit.query.SparqlPerformException;
import fr.sparna.rdf.sesame.toolkit.util.LabelReader;
import fr.sparna.rdf.sesame.toolkit.util.PreferredPropertyReader;
import fr.sparna.rdf.skos.printer.schema.ElementContainer;
import fr.sparna.rdf.skos.printer.schema.ObjectFactory;
import fr.sparna.rdf.skos.printer.schema.SimpleLiteral;
import fr.sparna.rdf.skos.toolkit.GetConceptSchemesHelper;

public class DocumentMetadataReader {

private Logger log = LoggerFactory.getLogger(this.getClass().getName());

protected Repository repository;

public DocumentMetadataReader(Repository repository) {
super();
this.repository = repository;
}

public ElementContainer readKosDocumentMetadata(final String lang, final URI conceptScheme)
throws SparqlPerformException {
ElementContainer m = new ElementContainer();
final ObjectFactory objectFactory = new ObjectFactory();


URI conceptSchemeToUse = conceptScheme;
if (conceptSchemeToUse == null) {
// try to find if their is a single concept scheme in the data, then take this one.
List<Resource> conceptSchemes = findConceptSchemes();
if(conceptSchemes.size() > 1) {
log.debug("Found "+conceptSchemes.size()+" ConceptSchemes ("+conceptSchemes+"), can't determine which one to use");
} else if (conceptSchemes.size() == 0) {
log.debug("Found 0 ConceptSchemes, can't generate header.");
} else {
conceptSchemeToUse = URI.create(conceptSchemes.get(0).stringValue());
log.debug("Determined ConceptScheme automatically : "+conceptSchemeToUse);
}
}

if (conceptSchemeToUse != null) {
// this will try to read in turn all the properties defined in a LabelReader
// skos:prefLabel, rdfs:sourceConceptLabel
LabelReader labelReader = new LabelReader(this.repository, "", lang);
// add dcterms title and dc title
labelReader.getProperties().add(URI.create(DCTERMS.TITLE.toString()));
labelReader.getProperties().add(URI.create(DC.TITLE.toString()));
String label = LabelReader.display(labelReader.getValues(conceptSchemeToUse));
if(label != null) {
m.getAny().add(createSimpleLiteralValue(label, lang, new DCCreator() {
public JAXBElement<SimpleLiteral> createDc(SimpleLiteral value) {
return objectFactory.createTitle(value);
}
}));
}

MetadataReader reader = new MetadataReader(conceptSchemeToUse, lang);

// read a description in the given language
reader.process(m,
Arrays.asList(new URI[]{java.net.URI.create(DCTERMS.DESCRIPTION.stringValue()), java.net.URI.create(DC.DESCRIPTION.stringValue())}),
new DCCreator() {
public JAXBElement<SimpleLiteral> createDc(SimpleLiteral value) {
return objectFactory.createDescription(value);
}
}
);

// read a date
reader.process(m,
Arrays.asList(new URI[]{java.net.URI.create(DCTERMS.ISSUED.stringValue()), java.net.URI.create(DCTERMS.MODIFIED.stringValue()), java.net.URI.create(DCTERMS.CREATED.stringValue()), java.net.URI.create(DC.DATE.stringValue())}),
new DCCreator() {
public JAXBElement<SimpleLiteral> createDc(SimpleLiteral value) {
return objectFactory.createDate(value);
}
}
);

// read an author/creator
reader.process(m,
Arrays.asList(new URI[]{java.net.URI.create(DCTERMS.CREATOR.stringValue()), java.net.URI.create(DC.CREATOR.stringValue())}),
new DCCreator() {
public JAXBElement<SimpleLiteral> createDc(SimpleLiteral value) {
return objectFactory.createCreator(value);
}
}
);

// read subject
reader.process(m,
Arrays.asList(new URI[]{java.net.URI.create(DCTERMS.SUBJECT.stringValue()), java.net.URI.create(DC.SUBJECT.stringValue())}),
new DCCreator() {
public JAXBElement<SimpleLiteral> createDc(SimpleLiteral value) {
return objectFactory.createSubject(value);
}
}
);
}

return m;
}


protected List<Resource> findConceptSchemes() throws SparqlPerformException {
final List<Resource> conceptSchemeList = new ArrayList<Resource>();

Perform.on(repository).select(new GetConceptSchemesHelper(null) {
@Override
protected void handleConceptScheme(Resource conceptScheme)
throws TupleQueryResultHandlerException {
conceptSchemeList.add(conceptScheme);
}
});

return conceptSchemeList;
}

protected List<Value> readProperties(URI subject, List<URI> uris, String lang)
throws SparqlPerformException {
PreferredPropertyReader reader = new PreferredPropertyReader(
this.repository,
uris,
Arrays.asList(new String[] { "" }),
lang
);
List<Value> v = reader.getValues(subject);
return v;
}

protected String formatDate(String date, String lang) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date d = sdf.parse(date);
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, new Locale(lang));
String dateString = df.format(d);

return dateString;
} catch (ParseException e) {
// return the original date by default
log.error(e.getMessage());
return date;
}
}

interface DCCreator {
public JAXBElement<SimpleLiteral> createDc(SimpleLiteral value);
}

protected JAXBElement<SimpleLiteral> createSimpleLiteralValue(String value, String lang, DCCreator dcCreator) {
SimpleLiteral literal = new SimpleLiteral();
literal.getContent().add(value);
literal.setLang(lang);
return dcCreator.createDc(literal);
}

class MetadataReader {
private URI conceptSchemeToUse;
private String lang;

public MetadataReader(URI conceptSchemeToUse, String lang) {
super();
this.conceptSchemeToUse = conceptSchemeToUse;
this.lang = lang;
}

public void process(ElementContainer container, List<URI> properties, DCCreator dcCreator)
throws SparqlPerformException {
List<Value> values = readProperties(
conceptSchemeToUse,
properties,
lang
);
if(values != null) {
container.getAny().add(createSimpleLiteralValue(LabelReader.display(values), null, dcCreator));
// for (Value aValue : values) {
// String theLang = (aValue instanceof Literal)?((Literal)aValue).getLanguage():lang;
// String theValue = (aValue instanceof Literal)?((Literal)aValue).getLabel():aValue.stringValue();
// container.getAny().add(createSimpleLiteralValue(theValue, theLang, dcCreator));
// }
}
}
}

}
Loading

0 comments on commit 211ad2a

Please sign in to comment.