Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update pdfbox to 2.0.0 and migrate from jempbox to xmpbox #1096

Closed
wants to merge 13 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

import com.google.common.base.Strings;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.IOException;
import java.io.InputStream;
Expand Down
95 changes: 33 additions & 62 deletions src/main/java/net/sf/jabref/logic/xmp/XMPSchemaBibtex.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package net.sf.jabref.logic.xmp;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;

import javax.xml.transform.TransformerException;
Expand All @@ -24,10 +26,13 @@

import net.sf.jabref.model.entry.*;
import net.sf.jabref.model.database.BibDatabase;
import org.apache.jempbox.xmp.XMPMetadata;
import org.apache.jempbox.xmp.XMPSchema;
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.schema.XMPSchema;
import org.apache.xmpbox.type.AbstractField;
import org.apache.xmpbox.type.Attribute;
import org.apache.xmpbox.xml.DomXmpParser;
import org.apache.xmpbox.xml.XmpParsingException;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

Expand Down Expand Up @@ -58,14 +63,11 @@ public XMPSchemaBibtex(XMPMetadata parent) {
super(parent, XMPSchemaBibtex.KEY, XMPSchemaBibtex.NAMESPACE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either you apply the constructor or have a look at the DublinCore Property Annotation:
@StructuredType(preferedPrefix = "dc", namespace = "http://purl.org/dc/elements/1.1/")

    public XMPSchemaBibtex(XMPMetadata parent) {
        super(parent,  XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.KEY);
    }

}

/**
* Create schema from an existing XML element.
*
* @param e
* The existing XML element.
*/
public XMPSchemaBibtex(Element e, String namespace) {
super(e, XMPSchemaBibtex.KEY);
public static XMPSchemaBibtex parseFromXml(Element e) throws XmpParsingException {
DomXmpParser parser = new DomXmpParser();
XMPMetadata meta = parser.parse(e.toString().getBytes());
XMPSchemaBibtex schema = (XMPSchemaBibtex)meta.getSchema(XMPSchemaBibtex.KEY);
return schema;
}

private static String makeProperty(String propertyName) {
Expand Down Expand Up @@ -96,59 +98,36 @@ public void setPersonList(String field, String value) {
}
}

@Override
public String getTextProperty(String field) {
return super.getTextProperty(makeProperty(field));
return super.getUnqualifiedTextProperty(makeProperty(field)).getStringValue();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would completely remove that code and all related as a) it is inconsistent and uncessary b) From the javadoc: getUnqualified...() does not need the namespace to acces the property. However, with calling MakeProperty, you are explicit adding a namespace. c) Callin these methods on the class would automatically call the super methods via polymorphism

Edit2::// The prefix is already set in the constructor

}

@Override
public void setTextProperty(String field, String value) {
super.setTextProperty(makeProperty(field), value);
super.setTextPropertyValue(makeProperty(field), value);
}

@Override
public List<String> getBagList(String bagName) {
return super.getBagList(makeProperty(bagName));
return super.getUnqualifiedBagValueList(makeProperty(bagName));
}

@Override
public void removeBagValue(String bagName, String value) {
super.removeBagValue(makeProperty(bagName), value);
super.removeUnqualifiedBagValue(makeProperty(bagName), value);
}

@Override
public void addBagValue(String bagName, String value) {
super.addBagValue(makeProperty(bagName), value);
super.addBagValueAsSimple(makeProperty(bagName), value);
}

@Override
public List<String> getSequenceList(String seqName) {
return super.getSequenceList(makeProperty(seqName));
return super.getUnqualifiedSequenceValueList(makeProperty(seqName));
}

@Override
public void removeSequenceValue(String seqName, String value) {
super.removeSequenceValue(makeProperty(seqName), value);
super.removeUnqualifiedSequenceValue(makeProperty(seqName), value);
}

@Override
public void addSequenceValue(String seqName, String value) {
super.addSequenceValue(makeProperty(seqName), value);
}

@Override
public List<Calendar> getSequenceDateList(String seqName) throws IOException {
return super.getSequenceDateList(makeProperty(seqName));
}

@Override
public void removeSequenceDateValue(String seqName, Calendar date) {
super.removeSequenceDateValue(makeProperty(seqName), date);
}

@Override
public void addSequenceDateValue(String field, Calendar date) {
super.addSequenceDateValue(makeProperty(field), date);
super.addUnqualifiedSequenceValue(makeProperty(seqName), value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

}

private static String getContents(NodeList seqList) {
Expand Down Expand Up @@ -177,11 +156,11 @@ private static String getContents(NodeList seqList) {
* are concatenated using " and ".
*
* @return Map from name of textproperty (String) to value (String). For
* instance: "year" => "2005". Empty map if none found.
* instance: "year" => "2005". Empty map if none found.
* @throws TransformerException
*/
public static Map<String, String> getAllProperties(XMPSchema schema, String namespaceName) {
NodeList nodes = schema.getElement().getChildNodes();
List<AbstractField> nodes = schema.getAllProperties();

Map<String, String> result = new HashMap<>();

Expand All @@ -190,16 +169,12 @@ public static Map<String, String> getAllProperties(XMPSchema schema, String name
}

// Check child-nodes first
int n = nodes.getLength();
int n = nodes.size();

for (int i = 0; i < n; i++) {
Node node = nodes.item(i);
if ((node.getNodeType() != Node.ATTRIBUTE_NODE)
&& (node.getNodeType() != Node.ELEMENT_NODE)) {
continue;
}
AbstractField node = nodes.get(i);

String nodeName = node.getNodeName();
String nodeName = node.getPropertyName();

String[] split = nodeName.split(":");

Expand All @@ -222,22 +197,20 @@ public static Map<String, String> getAllProperties(XMPSchema schema, String name
result.put(split[1], seq);
}
} else {
result.put(split[1], XMPSchemaBibtex.getTextContent(node));
result.put(split[1], ((Element) node).getTextContent());
}
}
}
}

// Then check Attributes
NamedNodeMap attrs = schema.getElement().getAttributes();
int m = attrs.getLength();
for (int j = 0; j < m; j++) {
Node attr = attrs.item(j);
List<Attribute> attrs = schema.getAllAttributes();
for (Attribute attribute: attrs) {

String nodeName = attr.getNodeName();
String nodeName = attribute.getName();
String[] split = nodeName.split(":");
if ((split.length == 2) && split[0].equals(namespaceName)) {
result.put(split[1], attr.getNodeValue());
result.put(split[1], attribute.getValue());
}
}

Expand Down Expand Up @@ -265,13 +238,11 @@ public static Map<String, String> getAllProperties(XMPSchema schema, String name
}



public void setBibtexEntry(BibEntry entry) {
setBibtexEntry(entry, null);
}

/**
*
* @param entry
* @param database maybenull
*/
Expand Down Expand Up @@ -312,9 +283,9 @@ public BibEntry getBibtexEntry() {

/**
* Taken from DOM2Utils.java:
*
* <p>
* JBoss, the OpenSource EJB server
*
* <p>
* Distributable under LGPL license. See terms of license at gnu.org.
*/
public static String getTextContent(Node node) {
Expand Down
Loading