Serialization
This commit is contained in:
parent
cebf79d67e
commit
da9eb999c1
3 changed files with 62 additions and 25 deletions
|
@ -1,15 +1,11 @@
|
||||||
package pro.litvinovg.libreoffice.metadata;
|
package pro.litvinovg.libreoffice.metadata;
|
||||||
|
|
||||||
import static pro.litvinovg.libreoffice.metadata.MetadataElement.METADATA_PREFIX;
|
import java.io.Serializable;
|
||||||
|
|
||||||
public class MetadataElement {
|
|
||||||
|
|
||||||
public static final String METADATA_START = "## Metadata Editor extension. Outline metadata folllows: ";
|
|
||||||
public static String METADATA_PREFIX = "##";
|
|
||||||
public static String METADATA_END = "## Metadata Editor extension. Outline metadata ends.";
|
|
||||||
|
|
||||||
|
public class MetadataElement implements Serializable{
|
||||||
|
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
private String name;
|
private String name;
|
||||||
private String value;
|
private String value;
|
||||||
|
|
||||||
|
@ -17,11 +13,7 @@ public class MetadataElement {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
public MetadataElement(String encodedPair) {
|
|
||||||
//TODO: Parse encoded pair
|
|
||||||
this.name = "";
|
|
||||||
this.value = "";
|
|
||||||
}
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
@ -29,15 +21,6 @@ public class MetadataElement {
|
||||||
public String getValue() {
|
public String getValue() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
public static boolean isValidMetadataString(String cursorContent) {
|
|
||||||
if (!cursorContent.startsWith(METADATA_PREFIX) ||
|
|
||||||
cursorContent.equals(METADATA_START) ||
|
|
||||||
cursorContent.equals(METADATA_END)){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
String data = cursorContent.substring(cursorContent.lastIndexOf(METADATA_PREFIX));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
package pro.litvinovg.libreoffice.metadata;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
public class MetadataElementFactory {
|
||||||
|
public static final String METADATA_START = "## Metadata Editor extension. Outline metadata folllows: ";
|
||||||
|
public static String METADATA_PREFIX = "##";
|
||||||
|
public static String METADATA_END = "## Metadata Editor extension. Outline metadata ends.";
|
||||||
|
|
||||||
|
|
||||||
|
public static boolean isValidMetadataString(String cursorContent) {
|
||||||
|
if (!cursorContent.startsWith(METADATA_PREFIX) ||
|
||||||
|
cursorContent.equals(METADATA_START) ||
|
||||||
|
cursorContent.equals(METADATA_END)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String data = cursorContent.substring(cursorContent.lastIndexOf(METADATA_PREFIX));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static MetadataElement fromString(String string) throws IOException, ClassNotFoundException {
|
||||||
|
byte [] data = Base64.getDecoder().decode( string );
|
||||||
|
ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( data ) );
|
||||||
|
Object object = ois.readObject();
|
||||||
|
ois.close();
|
||||||
|
return (MetadataElement) object;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String toString( MetadataElement object ) throws IOException {
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
ObjectOutputStream oos = new ObjectOutputStream( baos );
|
||||||
|
oos.writeObject( object );
|
||||||
|
oos.close();
|
||||||
|
return Base64.getEncoder().encodeToString(baos.toByteArray());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package pro.litvinovg.libreoffice.metadata;
|
package pro.litvinovg.libreoffice.metadata;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import com.sun.star.text.XParagraphCursor;
|
import com.sun.star.text.XParagraphCursor;
|
||||||
|
@ -7,7 +8,7 @@ import com.sun.star.text.XText;
|
||||||
import com.sun.star.text.XTextCursor;
|
import com.sun.star.text.XTextCursor;
|
||||||
import com.sun.star.text.XTextRange;
|
import com.sun.star.text.XTextRange;
|
||||||
import com.sun.star.uno.UnoRuntime;
|
import com.sun.star.uno.UnoRuntime;
|
||||||
import static pro.litvinovg.libreoffice.metadata.MetadataElement.*;
|
import static pro.litvinovg.libreoffice.metadata.MetadataElementFactory.*;
|
||||||
|
|
||||||
public class OutlineElement {
|
public class OutlineElement {
|
||||||
|
|
||||||
|
@ -52,8 +53,18 @@ public class OutlineElement {
|
||||||
metadataElements = metadataTempElements;
|
metadataElements = metadataTempElements;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (MetadataElement.isValidMetadataString(cursorContent)) {
|
if (MetadataElementFactory.isValidMetadataString(cursorContent)) {
|
||||||
metadataTempElements.add(new MetadataElement(cursorContent));
|
MetadataElement metadataElement = null;
|
||||||
|
try {
|
||||||
|
metadataElement = MetadataElementFactory.fromString(cursorContent);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
metadataTempElements.add(metadataElement);
|
||||||
} else {
|
} else {
|
||||||
//If reading fails exit immediately
|
//If reading fails exit immediately
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Add table
Reference in a new issue