metadata-editor/source/pro/litvinovg/libreoffice/metadata/Document.java

302 lines
11 KiB
Java
Raw Normal View History

2020-04-07 18:09:17 +02:00
package pro.litvinovg.libreoffice.metadata;
2020-04-08 13:42:49 +02:00
import java.nio.file.attribute.UserDefinedFileAttributeView;
2020-04-07 18:09:17 +02:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import com.sun.org.apache.bcel.internal.generic.NEW;
2020-04-08 13:42:49 +02:00
import com.sun.star.beans.IllegalTypeException;
import com.sun.star.beans.NotRemoveableException;
2020-04-07 18:09:17 +02:00
import com.sun.star.beans.Property;
2020-04-08 13:42:49 +02:00
import com.sun.star.beans.PropertyExistException;
import com.sun.star.beans.UnknownPropertyException;
2020-04-07 18:09:17 +02:00
import com.sun.star.beans.XPropertyContainer;
import com.sun.star.beans.XPropertySet;
import com.sun.star.beans.XPropertySetInfo;
2020-04-09 15:03:47 +02:00
import com.sun.star.container.NoSuchElementException;
import com.sun.star.container.XEnumeration;
import com.sun.star.container.XEnumerationAccess;
2020-04-07 18:09:17 +02:00
import com.sun.star.document.XDocumentProperties;
import com.sun.star.document.XDocumentPropertiesSupplier;
import com.sun.star.frame.XDesktop;
2020-04-08 13:42:49 +02:00
import com.sun.star.lang.IllegalArgumentException;
2020-04-09 15:03:47 +02:00
import com.sun.star.lang.WrappedTargetException;
2020-04-07 18:09:17 +02:00
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
2020-04-09 15:03:47 +02:00
import com.sun.star.lang.XServiceInfo;
import com.sun.star.text.XText;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
2020-04-07 18:09:17 +02:00
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
public class Document {
private static final String DOC_AUTHOR = "Document author";
private static final String DOC_TITLE = "Document title";
private static final String DOC_SUBJECT = "Document subject";
private static final String DOC_DESCRIPTION = "Document description";
private static final String DOC_KEYWORDS = "Document keywords";
2020-04-08 13:42:49 +02:00
private static final short REMOVEABLE_ATTRIBUTE = 128;
2020-04-07 18:09:17 +02:00
private XComponentContext context;
private XDesktop xDesktop;
private XMultiComponentFactory multiComponentFactory;
private XComponent currentDocument;
private XDocumentProperties documentProperties;
private XDocumentPropertiesSupplier documentPropertiesSupplier;
2020-04-09 15:03:47 +02:00
private XText text = null;
2020-04-07 18:09:17 +02:00
public Document(XComponentContext componentContext) {
context = componentContext;
multiComponentFactory = context.getServiceManager();
try {
Object oDesktop = multiComponentFactory.createInstanceWithContext("com.sun.star.frame.Desktop", context);
xDesktop = UnoRuntime.queryInterface(XDesktop.class, oDesktop);
getCurrentDocument();
2020-04-09 15:03:47 +02:00
getDocumentText();
updateChapterMetadata();
2020-04-07 18:09:17 +02:00
} catch (Exception e) {
System.out.println("xDesktop inaccessible. Can not proceed.");
e.printStackTrace();
System.exit(1);
}
2020-04-08 13:42:49 +02:00
//logProperties();
2020-04-07 18:09:17 +02:00
}
2020-04-09 15:03:47 +02:00
private void getDocumentText() {
XTextDocument textDocument = UnoRuntime.queryInterface(XTextDocument.class,currentDocument);
text = textDocument.getText();
}
private void updateChapterMetadata() {
findAllChapters();
}
private void findAllChapters() {
XEnumerationAccess enumAccess = UnoRuntime.queryInterface(XEnumerationAccess.class, text);
XEnumeration textEnum = enumAccess.createEnumeration();
while (textEnum.hasMoreElements()) {
try {
XTextContent textContent = UnoRuntime.queryInterface(XTextContent.class, textEnum.nextElement());
if(isSupported(textContent, "com.sun.star.text.TextTable")) {
//TODO: Go over table to check for outline elements
} else
if (isSupported(textContent, "com.sun.star.style.ParagraphProperties")) {
Short outlineLevel = null;
XPropertySet properties = UnoRuntime.queryInterface(XPropertySet.class, textContent);
try {
outlineLevel = (Short) properties.getPropertyValue("OutlineLevel");
} catch (UnknownPropertyException e) {
System.out.println("Shouldn't be here!");
e.printStackTrace();
}
if (outlineLevel != null && outlineLevel > 0) {
XTextRange textRange = textContent.getAnchor();
String name = textRange.getString();
System.out.println("Outline element found! " + name);
new OutlineElement(textRange);
//TODO: outline element found. Save outline element name and anchor
}
}
} catch (NoSuchElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WrappedTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2020-04-07 18:09:17 +02:00
private void getCurrentDocument() throws MetadataInaccessableException {
currentDocument = xDesktop.getCurrentComponent();
if (currentDocument == null) {
throw new MetadataInaccessableException("Could not access current document.");
}
}
public void logProperties() {
logDocumentProperties();
logDocumentCustomProperties();
}
2020-04-08 13:42:49 +02:00
public void setCustomDocumentProperties(ArrayList<CustomDocumentProperty> docCustomProps) {
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
documentProperties = documentPropertiesSupplier.getDocumentProperties();
removeStringProperties();
addStringProperties(docCustomProps);
}
2020-04-07 18:09:17 +02:00
2020-04-08 13:42:49 +02:00
private void addStringProperties(ArrayList<CustomDocumentProperty> docCustomProps) {
XPropertyContainer userDifinedProperties = documentProperties.getUserDefinedProperties();
for (int i = 0; i < docCustomProps.size();i++) {
CustomDocumentProperty property = docCustomProps.get(i);
try {
userDifinedProperties.addProperty(property.getName(), REMOVEABLE_ATTRIBUTE, property.getValue());
// System.out.println("added "+ property.getName() + " value " + property.getValue());
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException while adding new property");
e.printStackTrace();
} catch (PropertyExistException e) {
System.out.println("Property already exists");
e.printStackTrace();
} catch (IllegalTypeException e) {
System.out.println("Property type illegal");
e.printStackTrace();
}
}
}
private void removeStringProperties() {
XPropertyContainer userDifinedProperties = documentProperties.getUserDefinedProperties();
XPropertySet propertySet = UnoRuntime.queryInterface(XPropertySet.class, userDifinedProperties);
if (propertySet != null) {
XPropertySetInfo propertySetInfo = propertySet.getPropertySetInfo();
if (propertySetInfo != null) {
Property[] props = propertySetInfo.getProperties();
for (Property prop : props) {
if (prop.Type.getTypeName().equals("string")){
try {
userDifinedProperties.removeProperty(prop.Name);
// System.out.println("removed "+ prop.Name);
} catch (UnknownPropertyException e) {
System.out.println("Property " + prop.Name + " does not exist.");
e.printStackTrace();
} catch (NotRemoveableException e) {
System.out.println("Property " + prop.Name + " is not removeable.");
e.printStackTrace();
}
}
}
}
}
}
2020-04-07 18:09:17 +02:00
public ArrayList<CustomDocumentProperty> getDocumentCustomProperties() {
ArrayList<CustomDocumentProperty> customProps = new ArrayList<CustomDocumentProperty>();
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
documentProperties = documentPropertiesSupplier.getDocumentProperties();
XPropertyContainer userDifinedProperties = documentProperties.getUserDefinedProperties();
XPropertySet propertySet = UnoRuntime.queryInterface(XPropertySet.class, userDifinedProperties);
if (propertySet != null) {
XPropertySetInfo propertySetInfo = propertySet.getPropertySetInfo();
if (propertySetInfo != null) {
Property[] props = propertySetInfo.getProperties();
Arrays.sort(props, new Comparator<Property>() {
public int compare(Property p1, Property p2) {
return (p1.Name).compareTo(p2.Name);
}
});
for (Property prop : props) {
Object propValue = getProperty(propertySet, prop.Name);
if (prop.Type.getTypeName().equals("string")){
customProps.add(new CustomDocumentProperty(prop.Name, propValue.toString(), prop.Type.getTypeName()));
}
}
}
}
return customProps;
}
public Map<String, String> getDocumentProperties() {
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
documentProperties = documentPropertiesSupplier.getDocumentProperties();
Map<String, String> docProps = new HashMap<String, String>();
docProps.put(DOC_AUTHOR, documentProperties.getAuthor());
docProps.put(DOC_TITLE, documentProperties.getTitle());
docProps.put(DOC_SUBJECT, documentProperties.getSubject());
docProps.put(DOC_DESCRIPTION, documentProperties.getDescription());
String[] keywords = documentProperties.getKeywords();
StringBuilder keys = new StringBuilder();
for (int i = 0; i < keywords.length; i++) {
if (i > 0) {
keys.append(",");
}
keys.append(keywords[i]);
}
docProps.put(DOC_KEYWORDS, keys.toString());
return docProps;
}
public void setDocumentProperties(Map<String, String> docProps) {
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
documentProperties = documentPropertiesSupplier.getDocumentProperties();
if (docProps.containsKey(DOC_AUTHOR)) {
documentProperties.setAuthor(docProps.get(DOC_AUTHOR));
}
if (docProps.containsKey(DOC_TITLE)) {
documentProperties.setTitle(docProps.get(DOC_TITLE));
}
if (docProps.containsKey(DOC_SUBJECT)) {
documentProperties.setSubject(docProps.get(DOC_SUBJECT));
}
if (docProps.containsKey(DOC_DESCRIPTION)) {
documentProperties.setDescription(docProps.get(DOC_DESCRIPTION));
}
if (docProps.containsKey(DOC_KEYWORDS)) {
documentProperties.setKeywords(docProps.get(DOC_KEYWORDS).split(","));
}
}
private void logDocumentProperties() {
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
documentProperties = documentPropertiesSupplier.getDocumentProperties();
System.out.println(" Author: " + documentProperties.getAuthor());
System.out.println(" Title: " + documentProperties.getTitle());
System.out.println(" Subject: " + documentProperties.getSubject());
System.out.println(" Description: " + documentProperties.getDescription());
}
private void logDocumentCustomProperties() {
documentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, currentDocument);
documentProperties = documentPropertiesSupplier.getDocumentProperties();
XPropertyContainer userDifinedProperties = documentProperties.getUserDefinedProperties();
XPropertySet propertySet = UnoRuntime.queryInterface(XPropertySet.class, userDifinedProperties);
if (propertySet != null) {
XPropertySetInfo propertySetInfo = propertySet.getPropertySetInfo();
if (propertySetInfo != null) {
Property[] props = propertySetInfo.getProperties();
Arrays.sort(props, new Comparator<Property>() {
public int compare(Property p1, Property p2) {
return (p1.Name).compareTo(p2.Name);
}
});
for (Property prop : props) {
Object propValue = getProperty(propertySet, prop.Name);
System.out.println(" " + prop.Name + ": " + prop.Type.getTypeName() + " == " + propValue);
}
}
}
}
private static Object getProperty(XPropertySet xProps, String propName) {
Object value = null;
try {
value = xProps.getPropertyValue(propName);
} catch (Exception e) {
System.out.println("Could not get property " + propName);
}
return value;
}
2020-04-09 15:03:47 +02:00
private boolean isSupported(Object object, String service) {
XServiceInfo info = UnoRuntime.queryInterface(XServiceInfo.class, object);
if (info == null) {
return false;
}
return info.supportsService(service);
2020-04-08 13:42:49 +02:00
2020-04-09 15:03:47 +02:00
}
2020-04-08 13:42:49 +02:00
2020-04-07 18:09:17 +02:00
}