diff --git a/src/com/github/miachm/sods/OdsWritter.java b/src/com/github/miachm/sods/OdsWritter.java index 186cccf..f2ea4b9 100644 --- a/src/com/github/miachm/sods/OdsWritter.java +++ b/src/com/github/miachm/sods/OdsWritter.java @@ -7,17 +7,12 @@ import java.util.HashMap; import java.util.Map; +import static com.github.miachm.sods.OpenDocumentNamespaces.*; + /** * Internal class for generate ODS files. */ class OdsWritter { - private final static String office = "urn:oasis:names:tc:opendocument:xmlns:office:1.0"; - private final static String table_namespace = "urn:oasis:names:tc:opendocument:xmlns:table:1.0"; - private final static String text_namespace = "urn:oasis:names:tc:opendocument:xmlns:text:1.0"; - private final static String font_namespace = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"; - private final static String style_namespace = "urn:oasis:names:tc:opendocument:xmlns:style:1.0"; - private final static String metadata_namespace = "http://purl.org/dc/elements/1.1/"; - private final static String datatype_namespace ="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"; private SpreadSheet spread; private Compressor out; @@ -56,33 +51,32 @@ private void writeManifest() { XMLStreamWriter out = XMLOutputFactory.newInstance().createXMLStreamWriter( new OutputStreamWriter(output, "utf-8")); - final String namespace = "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"; - out.writeStartDocument("UTF-8", "1.0"); - out.writeStartElement("manifest:manifest"); - out.writeAttribute("xmlns:manifest", namespace); - out.writeAttribute("manifest:version", "1.2"); - - out.writeStartElement("manifest:file-entry"); - out.writeAttribute("manifest:full-path", "/"); - out.writeAttribute("manifest:version", "1.2"); - out.writeAttribute("manifest:media-type", MIMETYPE); + out.setPrefix("manifest", MANIFEST); + out.writeStartElement(MANIFEST, "manifest"); + out.writeNamespace("manifest", MANIFEST); + out.writeAttribute(MANIFEST, "version", "1.2"); + + out.writeStartElement(MANIFEST, "file-entry"); + out.writeAttribute(MANIFEST, "full-path", "/"); + out.writeAttribute(MANIFEST, "version", "1.2"); + out.writeAttribute(MANIFEST, "media-type", MIMETYPE); out.writeEndElement(); - out.writeStartElement("manifest:file-entry"); - out.writeAttribute("manifest:full-path", "content.xml"); - out.writeAttribute("manifest:media-type", "text/xml"); + out.writeStartElement(MANIFEST, "file-entry"); + out.writeAttribute(MANIFEST, "full-path", "content.xml"); + out.writeAttribute(MANIFEST, "media-type", "text/xml"); out.writeEndElement(); - out.writeStartElement("manifest:file-entry"); - out.writeAttribute("manifest:full-path", "styles.xml"); - out.writeAttribute("manifest:media-type", "text/xml"); + out.writeStartElement(MANIFEST, "file-entry"); + out.writeAttribute(MANIFEST, "full-path", "styles.xml"); + out.writeAttribute(MANIFEST, "media-type", "text/xml"); out.writeEndElement(); for (FileEntry entry : spread.getExtraFiles()) { - out.writeStartElement("manifest:file-entry"); - out.writeAttribute("manifest:full-path", entry.path); - out.writeAttribute("manifest:media-type", entry.mimetype); + out.writeStartElement(MANIFEST, "file-entry"); + out.writeAttribute(MANIFEST, "full-path", entry.path); + out.writeAttribute(MANIFEST, "media-type", entry.mimetype); out.writeEndElement(); } @@ -108,16 +102,17 @@ private void writeSpreadsheet() throws UnsupportedEncodingException, XMLStreamEx new OutputStreamWriter(output, "utf-8")); out.writeStartDocument("UTF-8", "1.0"); - out.writeStartElement( "office:document-content"); - out.writeAttribute("xmlns:office", office); - out.writeAttribute("xmlns:table", table_namespace); - out.writeAttribute("xmlns:text",text_namespace); - out.writeAttribute("xmlns:fo",font_namespace); - out.writeAttribute("xmlns:style", style_namespace); - out.writeAttribute("xmlns:dc", metadata_namespace); - out.writeAttribute("xmlns:number", datatype_namespace); - - out.writeAttribute("office:version", "1.2"); + out.setPrefix("office", OFFICE); + out.writeStartElement(OFFICE, "document-content"); + out.writeNamespace("office", OFFICE); + out.writeNamespace("table", TABLE); + out.writeNamespace("text", TEXT); + out.writeNamespace("fo", FONT); + out.writeNamespace("style", STYLE); + out.writeNamespace("dc", METADATA); + out.writeNamespace("number", DATATYPE); + + out.writeAttribute(OFFICE, "version", "1.2"); writeStyles(out); writeContent(out); @@ -142,9 +137,10 @@ private void writeSettingsStyleFile() throws UnsupportedEncodingException, XMLSt XMLStreamWriter out = XMLOutputFactory.newInstance().createXMLStreamWriter( new OutputStreamWriter(output, "utf-8")); out.writeStartDocument("UTF-8", "1.0"); - out.writeStartElement( "office:document-styles"); - out.writeAttribute("xmlns:office", office); - out.writeAttribute("office:version", "1.2"); + out.setPrefix("office", OFFICE); + out.writeStartElement(OFFICE, "document-styles"); + out.writeNamespace("office", OFFICE); + out.writeAttribute(OFFICE, "version", "1.2"); out.writeEndElement(); out.writeEndDocument(); out.close(); @@ -157,23 +153,23 @@ private void writeSettingsStyleFile() throws UnsupportedEncodingException, XMLSt } private void writeContent(XMLStreamWriter out) throws XMLStreamException { - out.writeStartElement("office:body"); - out.writeStartElement("office:spreadsheet"); + out.writeStartElement(OFFICE, "body"); + out.writeStartElement(OFFICE, "spreadsheet"); for (Sheet sheet : spread.getSheets()) { - out.writeStartElement("table:table"); - out.writeAttribute("table:name", sheet.getName()); + out.writeStartElement(TABLE, "table"); + out.writeAttribute(TABLE, "name", sheet.getName()); if (sheet.isHidden()) { TableStyle tableStyle = new TableStyle(); tableStyle.setHidden(true); String name = tableStyleStringMap.get(tableStyle); if (name != null) - out.writeAttribute("table:style-name", name); + out.writeAttribute(TABLE, "style-name", name); } if (sheet.isProtected()) { - out.writeAttribute("table:protected", "true"); - out.writeAttribute("table:protection-key", sheet.getHashedPassword()); - out.writeAttribute("table:protection-key-digest-algorithm", sheet.getHashedAlgorithm()); + out.writeAttribute(TABLE, "protected", "true"); + out.writeAttribute(TABLE, "protection-key", sheet.getHashedPassword()); + out.writeAttribute(TABLE, "protection-key-digest-algorithm", sheet.getHashedAlgorithm()); } writeColumnsStyles(out, sheet); @@ -188,25 +184,25 @@ private void writeContent(XMLStreamWriter out) throws XMLStreamException { private void writeColumnsStyles(XMLStreamWriter out, Sheet sheet) throws XMLStreamException { for (Column column : sheet.columns){ - out.writeStartElement("table:table-column"); + out.writeStartElement(TABLE, "table-column"); if (column.num_repeated > 1) - out.writeAttribute("table:number-columns-repeated", "" + column.num_repeated); + out.writeAttribute(TABLE, "number-columns-repeated", "" + column.num_repeated); Double width = column.column_style.getWidth(); if (width != null) { String name = columnStyleStringMap.get(width); if (name != null) - out.writeAttribute("table:style-name", name); + out.writeAttribute(TABLE, "style-name", name); } if (column.column_style.isHidden()) - out.writeAttribute("table:visibility", "collapse"); + out.writeAttribute(TABLE, "visibility", "collapse"); Style defaultCellStyle = column.column_style.getDefaultCellStyleDangerous(); if (!defaultCellStyle.isDefault()) { String name = stylesUsed.get(defaultCellStyle); if (name != null) - out.writeAttribute("table:default-cell-style-name", name); + out.writeAttribute(TABLE, "default-cell-style-name", name); } out.writeEndElement(); @@ -215,9 +211,9 @@ private void writeColumnsStyles(XMLStreamWriter out, Sheet sheet) throws XMLStre private void writeContent(XMLStreamWriter out, Sheet sheet) throws XMLStreamException { for (Row row : sheet.rows) { - out.writeStartElement("table:table-row"); + out.writeStartElement(TABLE, "table-row"); if (row.num_repeated > 1) - out.writeAttribute("table:number-rows-repeated", ""+row.num_repeated); + out.writeAttribute(TABLE, "number-rows-repeated", ""+row.num_repeated); writeRowStyles(out, row); for (Cell cell : row.cells) { @@ -230,7 +226,7 @@ private void writeContent(XMLStreamWriter out, Sheet sheet) throws XMLStreamExce private void writeRowStyles(XMLStreamWriter out, Row row) throws XMLStreamException { if (row.row_style.isHidden()) - out.writeAttribute("table:visibility", "collapse"); + out.writeAttribute(TABLE, "visibility", "collapse"); writeRowHeight(out, row); } @@ -242,23 +238,23 @@ private void writeCell(XMLStreamWriter out, Cell cell) throws XMLStreamException GroupCell group = cell.getGroup(); if (group != null) { if (group.getCell() != cell) { - out.writeStartElement("table:covered-table-cell"); + out.writeStartElement(TABLE, "covered-table-cell"); out.writeEndElement(); return; } } - out.writeStartElement("table:table-cell"); + out.writeStartElement(TABLE, "table-cell"); if (cell.num_repeated > 1) - out.writeAttribute("table:number-columns-repeated", ""+ cell.num_repeated); + out.writeAttribute(TABLE, "number-columns-repeated", ""+ cell.num_repeated); if (group != null) { if (group.getLength().getY() > 1) - out.writeAttribute("table:number-columns-spanned", "" + group.getLength().getY()); + out.writeAttribute(TABLE, "number-columns-spanned", "" + group.getLength().getY()); if (group.getLength().getX() > 1) - out.writeAttribute("table:number-rows-spanned", "" + group.getLength().getX()); + out.writeAttribute(TABLE, "number-rows-spanned", "" + group.getLength().getX()); } if (formula != null) - out.writeAttribute("table:formula", formula); + out.writeAttribute(TABLE, "formula", formula); setCellStyle(out, style); writeValue(out, cell); @@ -273,7 +269,7 @@ private void setCellStyle(XMLStreamWriter out, Style style) throws XMLStreamExce stylesUsed.put(style, key); } - out.writeAttribute("table:style-name", key); + out.writeAttribute(TABLE, "style-name", key); } } @@ -290,27 +286,27 @@ private void writeValue(XMLStreamWriter out, Cell cell) throws XMLStreamExceptio valueType.write(v, out); } - out.writeStartElement("text:p"); + out.writeStartElement(TEXT, "p"); String text = v.toString(); for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == ' ') { - out.writeStartElement("text:s"); + out.writeStartElement(TEXT, "s"); int cnt = 0; while (i+cnt < text.length() && text.charAt(i + cnt) == ' ') { cnt++; } if (cnt > 1) - out.writeAttribute("text:c", "" + cnt); + out.writeAttribute(TEXT, "c", "" + cnt); i += cnt - 1 ; out.writeEndElement(); } else if (text.charAt(i) == '\t') { - out.writeEmptyElement("text:tab"); + out.writeEmptyElement(TEXT, "tab"); } else if (text.charAt(i) == '\n') { out.writeEndElement(); - out.writeStartElement("text:p"); + out.writeStartElement(TEXT, "p"); } else if (Character.isHighSurrogate(text.charAt(i)) && i + 1 < text.length() && Character.isLowSurrogate(text.charAt(i + 1))) { // write surrogate pair @@ -325,14 +321,14 @@ else if (Character.isHighSurrogate(text.charAt(i)) && i + 1 < text.length() && C } OfficeAnnotation annotation = cell.getAnnotation(); if (annotation != null) { - out.writeStartElement("office:annotation"); + out.writeStartElement(OFFICE, "annotation"); if (annotation.getLastModified() != null) { - out.writeStartElement("dc:date"); + out.writeStartElement(METADATA, "date"); out.writeCharacters(annotation.getLastModified().toString()); out.writeEndElement(); } if (annotation.getMsg() != null) { - out.writeStartElement("text:p"); + out.writeStartElement(TEXT, "p"); out.writeCharacters(annotation.getMsg()); out.writeEndElement(); } @@ -345,12 +341,12 @@ private void writeRowHeight(XMLStreamWriter out, Row row) throws XMLStreamExcept if (height != null) { String name = rowStyleStringMap.get(height); if (name != null) - out.writeAttribute("table:style-name", name); + out.writeAttribute(TABLE, "style-name", name); } } private void writeStyles(XMLStreamWriter out) throws XMLStreamException { - out.writeStartElement("office:automatic-styles"); + out.writeStartElement(OFFICE, "automatic-styles"); writeDataFormatStyles(out); @@ -391,12 +387,12 @@ private void writeStyles(XMLStreamWriter out) throws XMLStreamException { private void writeDataFormatStyles(XMLStreamWriter out) throws XMLStreamException { out.writeStartElement("number:text-style"); - out.writeAttribute("style:name", "textstyle"); + out.writeAttribute(STYLE, "name", "textstyle"); out.writeEmptyElement("number:text-content"); out.writeEndElement(); out.writeStartElement("number:date-style"); - out.writeAttribute("style:name", "datestyle"); + out.writeAttribute(STYLE, "name", "datestyle"); out.writeStartElement("number:year"); out.writeAttribute("number:style", "long"); out.writeEndElement(); @@ -428,29 +424,29 @@ private void writeCellStyle(XMLStreamWriter out, Style style, String key) throws for (ConditionalFormat conditionalFormat : style.getConditions()) { writeCellStyle(out, conditionalFormat.getStyleApplied()); } - out.writeStartElement("style:style"); - out.writeAttribute("style:family", "table-cell"); - out.writeAttribute("style:name", key); + out.writeStartElement(STYLE, "style"); + out.writeAttribute(STYLE, "family", "table-cell"); + out.writeAttribute(STYLE, "name", key); String dataStyle = style.getDataStyle(); if (Style.PLAIN_DATA_STYLE.equals(dataStyle)) - out.writeAttribute("style:data-style-name", "textstyle"); + out.writeAttribute(STYLE, "data-style-name", "textstyle"); else if (Style.ISO_DATE_DATA_STYLE.equals(dataStyle)) - out.writeAttribute("style:data-style-name", "datestyle"); + out.writeAttribute(STYLE, "data-style-name", "datestyle"); if (style.hasTableCellProperties()) { - out.writeStartElement("style:table-cell-properties"); + out.writeStartElement(STYLE, "table-cell-properties"); if (style.getBackgroundColor() != null) { - out.writeAttribute("fo:background-color", style.getBackgroundColor().toString()); + out.writeAttribute(FONT, "background-color", style.getBackgroundColor().toString()); } if (style.isWrap()) { - out.writeAttribute("fo:wrap-option", "wrap"); + out.writeAttribute(FONT, "wrap-option", "wrap"); } if (style.getVerticalTextAligment() != null) { - out.writeAttribute("style:vertical-align", style.getVerticalTextAligment().toString().toLowerCase()); + out.writeAttribute(STYLE, "vertical-align", style.getVerticalTextAligment().toString().toLowerCase()); } if(style.hasBorders()) { @@ -461,37 +457,37 @@ else if (Style.ISO_DATE_DATA_STYLE.equals(dataStyle)) } for (ConditionalFormat format : style.getConditions()) { - out.writeStartElement("style:map"); - out.writeAttribute("style:condition", format.getRawCondition()); - out.writeAttribute("style:apply-style-name", getConditionalFormatName(format.getStyleApplied())); + out.writeStartElement(STYLE, "map"); + out.writeAttribute(STYLE, "condition", format.getRawCondition()); + out.writeAttribute(STYLE, "apply-style-name", getConditionalFormatName(format.getStyleApplied())); out.writeEndElement(); } - out.writeStartElement("style:text-properties"); + out.writeStartElement(STYLE, "text-properties"); if (style.isItalic()) - out.writeAttribute("fo:font-style", "italic"); + out.writeAttribute(FONT, "font-style", "italic"); if (style.isBold()) - out.writeAttribute("fo:font-weight", "bold"); + out.writeAttribute(FONT, "font-weight", "bold"); if (style.isUnderline()) { - out.writeAttribute("style:text-underline-style", "solid"); - out.writeAttribute("style:text-underline-type", "single"); - out.writeAttribute("style:text-underline-width", "auto"); - out.writeAttribute("style:text-underline-color", "font-color"); + out.writeAttribute(STYLE, "text-underline-style", "solid"); + out.writeAttribute(STYLE, "text-underline-type", "single"); + out.writeAttribute(STYLE, "text-underline-width", "auto"); + out.writeAttribute(STYLE, "text-underline-color", "font-color"); } if (style.getFontSize() != -1) - out.writeAttribute("fo:font-size", "" + style.getFontSize() + "pt"); + out.writeAttribute(FONT, "font-size", "" + style.getFontSize() + "pt"); if (style.getFontColor() != null) - out.writeAttribute("fo:color", style.getFontColor().toString()); + out.writeAttribute(FONT, "color", style.getFontColor().toString()); out.writeEndElement(); if(style.getTextAligment() != null) { - out.writeStartElement("style:paragraph-properties"); - out.writeAttribute("fo:text-align", toValue(style.getTextAligment())); + out.writeStartElement(STYLE, "paragraph-properties"); + out.writeAttribute(FONT, "text-align", toValue(style.getTextAligment())); out.writeEndElement(); } @@ -520,11 +516,11 @@ private void writeColumnStyle(XMLStreamWriter out, Double width) throws XMLStrea if (!columnStyleStringMap.containsKey(width)) { String key = "co" + columnStyleStringMap.size(); - out.writeStartElement("style:style"); - out.writeAttribute("style:family", "table-column"); - out.writeAttribute("style:name", key); - out.writeStartElement("style:table-column-properties"); - out.writeAttribute("style:column-width", width.toString() + "mm"); + out.writeStartElement(STYLE, "style"); + out.writeAttribute(STYLE, "family", "table-column"); + out.writeAttribute(STYLE, "name", key); + out.writeStartElement(STYLE, "table-column-properties"); + out.writeAttribute(STYLE, "column-width", width.toString() + "mm"); out.writeEndElement(); out.writeEndElement(); @@ -535,11 +531,11 @@ private void writeColumnStyle(XMLStreamWriter out, Double width) throws XMLStrea private void writeRowStyle(XMLStreamWriter out, Double height) throws XMLStreamException { if (!rowStyleStringMap.containsKey(height)) { String key = "ro" + rowStyleStringMap.size(); - out.writeStartElement("style:style"); - out.writeAttribute("style:family", "table-row"); - out.writeAttribute("style:name", key); - out.writeStartElement("style:table-row-properties"); - out.writeAttribute("style:row-height", height.toString() + "mm"); + out.writeStartElement(STYLE, "style"); + out.writeAttribute(STYLE, "family", "table-row"); + out.writeAttribute(STYLE, "name", key); + out.writeStartElement(STYLE, "table-row-properties"); + out.writeAttribute(STYLE, "row-height", height.toString() + "mm"); out.writeEndElement(); out.writeEndElement(); @@ -552,11 +548,11 @@ private void writeTableStyle(XMLStreamWriter out, Sheet sheet) throws XMLStreamE tableStyle.setHidden(sheet.isHidden()); if (!tableStyleStringMap.containsKey(tableStyle)) { String key = "tb" + tableStyleStringMap.size(); - out.writeStartElement("style:style"); - out.writeAttribute("style:family", "table"); - out.writeAttribute("style:name", key); - out.writeStartElement("style:table-properties"); - out.writeAttribute("table:display", tableStyle.isHidden() ? "false" : "true"); + out.writeStartElement(STYLE, "style"); + out.writeAttribute(STYLE, "family", "table"); + out.writeAttribute(STYLE, "name", key); + out.writeStartElement(STYLE, "table-properties"); + out.writeAttribute(TABLE, "display", tableStyle.isHidden() ? "false" : "true"); out.writeEndElement(); out.writeEndElement(); @@ -568,23 +564,23 @@ private void writeBorderStyle(XMLStreamWriter out, Style style) throws XMLStream Borders borders = style.getBorders(); if (borders.isBorder()) { - out.writeAttribute("fo:border", borders.getBorderProperties()); + out.writeAttribute(FONT, "border", borders.getBorderProperties()); } if (borders.isBorderTop()) { - out.writeAttribute("fo:border-top", borders.getBorderTopProperties()); + out.writeAttribute(FONT, "border-top", borders.getBorderTopProperties()); } if (borders.isBorderBottom()) { - out.writeAttribute("fo:border-bottom", borders.getBorderBottomProperties()); + out.writeAttribute(FONT, "border-bottom", borders.getBorderBottomProperties()); } if (borders.isBorderLeft()) { - out.writeAttribute("fo:border-left", borders.getBorderLeftProperties()); + out.writeAttribute(FONT, "border-left", borders.getBorderLeftProperties()); } if (borders.isBorderRight()) { - out.writeAttribute("fo:border-right", borders.getBorderRightProperties()); + out.writeAttribute(FONT, "border-right", borders.getBorderRightProperties()); } } diff --git a/src/com/github/miachm/sods/OfficeValueType.java b/src/com/github/miachm/sods/OfficeValueType.java index 58eeec4..5c48f65 100644 --- a/src/com/github/miachm/sods/OfficeValueType.java +++ b/src/com/github/miachm/sods/OfficeValueType.java @@ -12,6 +12,8 @@ import java.util.Currency; import java.util.Locale; +import static com.github.miachm.sods.OpenDocumentNamespaces.OFFICE; + /** * see * http://docs.oasis-open.org/office/v1.2/part1/cd04/OpenDocument-v1.2-part1-cd04.html#a_19_387_office_value-type @@ -38,8 +40,8 @@ public Object read(XmlReaderInstance reader) { @Override public void write(Object value, XMLStreamWriter writer) throws XMLStreamException { if (value instanceof Boolean) { - writer.writeAttribute("office:value-type", getId()); - writer.writeAttribute("office:boolean-value", value.toString()); + writer.writeAttribute(OFFICE, "value-type", getId()); + writer.writeAttribute(OFFICE, "boolean-value", value.toString()); } } }, @@ -66,18 +68,18 @@ public Object read(XmlReaderInstance reader) { @Override public void write(Object value, XMLStreamWriter writer) throws XMLStreamException { if (value instanceof OfficeCurrency) { - writer.writeAttribute("office:value-type", getId()); + writer.writeAttribute(OFFICE, "value-type", getId()); OfficeCurrency currency = (OfficeCurrency) value; if (currency.getValue() != null) { NumberFormat formatter = NumberFormat.getInstance(Locale.US); formatter.setGroupingUsed(false); - writer.writeAttribute("office:value", formatter.format(currency.getValue())); + writer.writeAttribute(OFFICE, "value", formatter.format(currency.getValue())); } if (currency.getCurrency() != null) - writer.writeAttribute("office:currency", currency.getCurrency().getCurrencyCode()); + writer.writeAttribute(OFFICE, "currency", currency.getCurrency().getCurrencyCode()); } } }, @@ -102,11 +104,11 @@ public Object read(XmlReaderInstance reader) { @Override public void write(Object value, XMLStreamWriter writer) throws XMLStreamException { if (value instanceof LocalDateTime) { - writer.writeAttribute("office:value-type", getId()); - writer.writeAttribute("office:date-value", ((LocalDateTime) value).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + writer.writeAttribute(OFFICE, "value-type", getId()); + writer.writeAttribute(OFFICE, "date-value", ((LocalDateTime) value).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); } else if (value instanceof LocalDate) { - writer.writeAttribute("office:value-type", getId()); - writer.writeAttribute("office:date-value", ((LocalDate) value).format(DateTimeFormatter.ISO_LOCAL_DATE)); + writer.writeAttribute(OFFICE, "value-type", getId()); + writer.writeAttribute(OFFICE, "date-value", ((LocalDate) value).format(DateTimeFormatter.ISO_LOCAL_DATE)); } } }, @@ -131,8 +133,8 @@ public Object read(XmlReaderInstance reader) { @Override public void write(Object value, XMLStreamWriter writer) throws XMLStreamException { if (value instanceof Number) { - writer.writeAttribute("office:value-type", getId()); - writer.writeAttribute("office:value", value.toString()); + writer.writeAttribute(OFFICE, "value-type", getId()); + writer.writeAttribute(OFFICE, "value", value.toString()); } } }, @@ -153,13 +155,13 @@ public Object read(XmlReaderInstance reader) { @Override public void write(Object value, XMLStreamWriter writer) throws XMLStreamException { if (value instanceof OfficePercentage) { - writer.writeAttribute("office:value-type", getId()); + writer.writeAttribute(OFFICE, "value-type", getId()); OfficePercentage percentage = (OfficePercentage) value; if (percentage.getValue() != null) { NumberFormat formatter = NumberFormat.getInstance(Locale.US); - writer.writeAttribute("office:value", formatter.format(percentage.getValue())); + writer.writeAttribute(OFFICE, "value", formatter.format(percentage.getValue())); } } } @@ -173,8 +175,8 @@ public Object read(XmlReaderInstance reader) { @Override public void write(Object value, XMLStreamWriter writer) throws XMLStreamException { if (value instanceof String) { - writer.writeAttribute("office:value-type", this.getId()); - writer.writeAttribute("office:string-value", value.toString().replace("\n", " ")); + writer.writeAttribute(OFFICE, "value-type", this.getId()); + writer.writeAttribute(OFFICE, "string-value", value.toString().replace("\n", " ")); } } }, @@ -195,8 +197,8 @@ public Object read(XmlReaderInstance reader) { @Override public void write(Object value, XMLStreamWriter writer) throws XMLStreamException { if (value instanceof Duration) { - writer.writeAttribute("office:value-type", getId()); - writer.writeAttribute("office:time-value", value.toString()); + writer.writeAttribute(OFFICE, "value-type", getId()); + writer.writeAttribute(OFFICE, "time-value", value.toString()); } } }, diff --git a/src/com/github/miachm/sods/OpenDocumentNamespaces.java b/src/com/github/miachm/sods/OpenDocumentNamespaces.java new file mode 100644 index 0000000..a3e05d5 --- /dev/null +++ b/src/com/github/miachm/sods/OpenDocumentNamespaces.java @@ -0,0 +1,12 @@ +package com.github.miachm.sods; + +public class OpenDocumentNamespaces { + final static String MANIFEST = "urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"; + final static String OFFICE = "urn:oasis:names:tc:opendocument:xmlns:office:1.0"; + final static String TABLE = "urn:oasis:names:tc:opendocument:xmlns:table:1.0"; + final static String TEXT = "urn:oasis:names:tc:opendocument:xmlns:text:1.0"; + final static String FONT = "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"; + final static String STYLE = "urn:oasis:names:tc:opendocument:xmlns:style:1.0"; + final static String METADATA = "http://purl.org/dc/elements/1.1/"; + final static String DATATYPE ="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"; +} diff --git a/tests/com/github/miachm/sods/OfficeValueTypeTest.java b/tests/com/github/miachm/sods/OfficeValueTypeTest.java index 257ab99..cc8f21a 100644 --- a/tests/com/github/miachm/sods/OfficeValueTypeTest.java +++ b/tests/com/github/miachm/sods/OfficeValueTypeTest.java @@ -27,6 +27,8 @@ import javax.xml.stream.XMLStreamWriter; import javax.xml.transform.dom.DOMResult; import org.testng.annotations.Test; + +import static com.github.miachm.sods.OpenDocumentNamespaces.OFFICE; import static org.testng.AssertJUnit.*; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -213,6 +215,7 @@ private static ObjectAssert assertRead(OfficeValueType valueType, String key, St private static NodeAssert assertWrite(OfficeValueType valueType, Object value) throws Exception { return NodeAssert.of(writer -> { writer.writeStartDocument(); + writer.setPrefix("office", OFFICE); writer.writeStartElement("root"); valueType.write(value, writer); writer.writeEndElement(); @@ -295,9 +298,15 @@ public NodeAssert(Element node) { } public NodeAssert containsAttribute(String attrKey, String attrValue) { - assertTrue(target.hasAttribute(attrKey)); - assertEquals(attrValue, target.getAttributeNode(attrKey).getValue()); - return this; + if (attrKey.startsWith("office:")) { + String[] parts = attrKey.split(":"); + assertEquals(attrValue, target.getAttributeNS(OFFICE, parts[1])); + return this; + } else { + assertTrue(target.hasAttribute(attrKey)); + assertEquals(attrValue, target.getAttributeNode(attrKey).getValue()); + return this; + } } public NodeAssert doesNotContainAttribute(String attrKey) {