diff options
Diffstat (limited to 'plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core')
5 files changed, 134 insertions, 95 deletions
diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/AbstractSheetWriter.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/AbstractSheetWriter.java index 56ec653942d..19a591995c7 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/AbstractSheetWriter.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/AbstractSheetWriter.java @@ -13,6 +13,9 @@ package org.eclipse.osee.framework.jdk.core.util.io.xml; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.util.Collection; @@ -28,6 +31,11 @@ public abstract class AbstractSheetWriter implements ISheetWriter { implicitCellIndex = 0; } + @Override + public void startSheet(String worksheetName, int columnCount, File file) throws IOException { + startSheet(worksheetName, columnCount, file == null ? null : new BufferedWriter(new FileWriter(file))); + } + /** * must be called by subclasses in their implementations of writeCell(String data, int cellIndex) */ @@ -52,7 +60,7 @@ public abstract class AbstractSheetWriter implements ISheetWriter { endRow(); } - /* + /** * when calling writeCell with an index, the implicit index will be set to one greater than the given index */ @Override @@ -70,7 +78,7 @@ public abstract class AbstractSheetWriter implements ISheetWriter { writeEndRow(); } - /* + /** * every time you call writeCell, the implicit index will be incremented */ @Override @@ -89,4 +97,4 @@ public abstract class AbstractSheetWriter implements ISheetWriter { protected abstract void writeCellText(Object data, int cellIndex) throws IOException; -} +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ExcelXmlWriter.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ExcelXmlWriter.java index 290999e62a8..66978e63f02 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ExcelXmlWriter.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ExcelXmlWriter.java @@ -14,10 +14,11 @@ package org.eclipse.osee.framework.jdk.core.util.io.xml; import java.io.BufferedWriter; +import java.io.Closeable; import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.io.Writer; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -77,9 +78,11 @@ public final class ExcelXmlWriter extends AbstractSheetWriter { "<Style ss:ID=\"OseeCentered\"><Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Bottom\"/></Style>\n" + // "<Style ss:ID=\"OseeWraped\"><Alignment ss:Vertical=\"Top\" ss:WrapText=\"1\"/></Style>"; - private final BufferedWriter out; + private Appendable out; private boolean inSheet; private final String emptyStringRepresentation; + private final String style; + private final int defaultFontSize; private int previouslyWrittenCellIndex; private boolean applyStyle = false; @@ -96,12 +99,24 @@ public final class ExcelXmlWriter extends AbstractSheetWriter { private int numSheetsWritten = 0; private int activeSheetNum = -1; - public ExcelXmlWriter(File file) throws IOException { - this(new FileWriter(file)); + public ExcelXmlWriter() { + this(null, defaultEmptyStringXmlRep); } - public ExcelXmlWriter(Writer writer) throws IOException { - this(writer, null); + public ExcelXmlWriter(File file) { + this(toAppendable(file), null, defaultEmptyStringXmlRep); + } + + private static Appendable toAppendable(File file) { + try { + return new BufferedWriter(new FileWriter(file)); + } catch (IOException ex) { + throw OseeCoreException.wrap(ex); + } + } + + public ExcelXmlWriter(Appendable out) { + this(out, null, defaultEmptyStringXmlRep); } /** @@ -110,39 +125,62 @@ public final class ExcelXmlWriter extends AbstractSheetWriter { * @param writer output * @param style Excel Style XML of form <Styles><Style/><Style/></Styles> */ - public ExcelXmlWriter(Writer writer, String style) throws IOException { - this(writer, style, defaultEmptyStringXmlRep); + public ExcelXmlWriter(Appendable out, String style) { + this(out, style, defaultEmptyStringXmlRep); } - public ExcelXmlWriter(Writer writer, String style, String emptyStringRepresentation) throws IOException { - this(writer, style, emptyStringRepresentation, DEFAULT_FONT_SIZE); + public ExcelXmlWriter(Appendable out, String style, String emptyStringRepresentation) { + this(out, style, emptyStringRepresentation, DEFAULT_FONT_SIZE); } - public ExcelXmlWriter(Writer writer, String style, String emptyStringRepresentation, int defaultFontSize) throws IOException { - out = new BufferedWriter(writer); + public ExcelXmlWriter(Appendable out, String style, String emptyStringRepresentation, int defaultFontSize) { mStyleMap = new HashMap<>(); + this.out = out; this.emptyStringRepresentation = emptyStringRepresentation; - out.write(XML_HEADER); + this.style = style; + this.defaultFontSize = defaultFontSize; + initWorkbook(out); + } - out.write("<Styles>\n"); + private void initWorkbook(Appendable out) { + if (out != null) { + this.out = out; + try { + out.append(XML_HEADER); - out.write(String.format(DEFAULT_OSEE_STYLES, defaultFontSize)); - if (Strings.isValid(style)) { - if (stylePattern.matcher(style).matches()) { - out.write(style); - } else { - throw new IllegalArgumentException("incomingStyle must match the pattern " + stylePattern); + out.append("<Styles>\n"); + + out.append(String.format(DEFAULT_OSEE_STYLES, defaultFontSize)); + if (Strings.isValid(style)) { + if (stylePattern.matcher(style).matches()) { + out.append(style); + } else { + throw new IllegalArgumentException("incomingStyle must match the pattern " + stylePattern); + } + } + out.append("</Styles>\n"); + } catch (IOException ex) { + OseeCoreException.wrapAndThrow(ex); } } - out.write("</Styles>\n"); } @Override public void startSheet(String worksheetName, int columnCount) throws IOException { - startSheet(worksheetName, ExcelColumn.newEmptyColumns(columnCount)); + startSheet(worksheetName, null, ExcelColumn.newEmptyColumns(columnCount)); + } + + @Override + public void startSheet(String worksheetName, int columnCount, Appendable out) throws IOException { + startSheet(worksheetName, out, ExcelColumn.newEmptyColumns(columnCount)); } public void startSheet(String worksheetName, ExcelColumn... columns) throws IOException { + startSheet(worksheetName, null, columns); + } + + public void startSheet(String worksheetName, Appendable newOut, ExcelColumn... columns) throws IOException { + initWorkbook(newOut); if (inSheet) { throw new OseeCoreException("Cannot start a new sheet until the current sheet is closed"); } @@ -150,22 +188,21 @@ public final class ExcelXmlWriter extends AbstractSheetWriter { worksheetName = worksheetName.substring(0, 31); } numColumns = columns.length; + rowBuffer = new String[numColumns]; - out.write(" <Worksheet ss:Name=\""); - out.write(worksheetName); - out.write("\">\n"); + out.append(" <Worksheet ss:Name=\""); + out.append(worksheetName); + out.append("\">\n"); - out.write(" <Table x:FullColumns=\"1\" x:FullRows=\"1\" ss:ExpandedColumnCount=\""); - out.write(String.valueOf(numColumns)); - out.write("\">\n"); + out.append(" <Table x:FullColumns=\"1\" x:FullRows=\"1\" ss:ExpandedColumnCount=\""); + out.append(String.valueOf(numColumns)); + out.append("\">\n"); for (ExcelColumn column : columns) { column.writeColumnDefinition(out); } if (columns[0].getName() != null) { - - rowBuffer = new String[numColumns]; for (ExcelColumn column : columns) { writeCell(column.getName()); } @@ -177,8 +214,8 @@ public final class ExcelXmlWriter extends AbstractSheetWriter { @Override public void endSheet() throws IOException { - out.write(" </Table>\n"); - out.write(" </Worksheet>\n"); + out.append(" </Table>\n"); + out.append(" </Worksheet>\n"); inSheet = false; numColumns = -1; ++numSheetsWritten; @@ -191,13 +228,15 @@ public final class ExcelXmlWriter extends AbstractSheetWriter { endSheet(); } if (activeSheetNum >= 0) { - out.write(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n"); - out.write(" <ActiveSheet>" + activeSheetNum + "</ActiveSheet>\n"); - out.write(" </ExcelWorkbook>\n"); + out.append(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n"); + out.append(" <ActiveSheet>" + activeSheetNum + "</ActiveSheet>\n"); + out.append(" </ExcelWorkbook>\n"); } - out.write("</Workbook>\n"); + out.append("</Workbook>\n"); } finally { - Lib.close(out); + if (out instanceof Closeable) { + Lib.close((Closeable) out); + } } } @@ -224,13 +263,13 @@ public final class ExcelXmlWriter extends AbstractSheetWriter { @Override protected void startRow() throws IOException { - out.write(" <Row"); + out.append(" <Row"); if (rowHeight != 0.0) { - out.write(String.format(" ss:AutoFitHeight=\"0\" ss:Height=\"%f\"", rowHeight)); + out.append(String.format(" ss:AutoFitHeight=\"0\" ss:Height=\"%f\"", rowHeight)); } - out.write(">\n"); + out.append(">\n"); - rowBuffer = new String[numColumns]; + Arrays.fill(rowBuffer, null); previouslyWrittenCellIndex = -1; } @@ -239,11 +278,10 @@ public final class ExcelXmlWriter extends AbstractSheetWriter { public void writeEndRow() throws IOException { for (int i = 0; i < numColumns; i++) { if (rowBuffer[i] != null && rowBuffer[i].length() > 0) { - out.write(rowBuffer[i]); + out.append(rowBuffer[i]); } } - out.write(" </Row>\n"); - rowBuffer = null; + out.append(" </Row>\n"); } @Override @@ -380,10 +418,6 @@ public final class ExcelXmlWriter extends AbstractSheetWriter { applyStyle = mStyleMap.size() > 0 || mColSpanMap.size() > 0; } - public BufferedWriter getOut() { - return out; - } - /* * @param sheetNum - the sheet number uses 0 based counting, i.e. 0 is the first sheet. */ diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ISheetWriter.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ISheetWriter.java index 8372cbc3971..a274f3592d9 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ISheetWriter.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/ISheetWriter.java @@ -13,6 +13,7 @@ package org.eclipse.osee.framework.jdk.core.util.io.xml; +import java.io.File; import java.io.IOException; import java.util.Collection; @@ -22,6 +23,18 @@ import java.util.Collection; */ public interface ISheetWriter { + /** + * Use this to start a sheet that will use a its own writer (rather than a shared writer for all sheets) + * + * @param worksheetName + * @param columnCount + * @param out + * @throws IOException + */ + public void startSheet(String worksheetName, int columnCount, Appendable out) throws IOException; + + public void startSheet(String worksheetName, int columnCount, File file) throws IOException; + public void startSheet(String worksheetName, int columnCount) throws IOException; public void endSheet() throws IOException; diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/TextSheetWriter.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/TextSheetWriter.java index a58f5f7f797..1d456dd00f2 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/TextSheetWriter.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/TextSheetWriter.java @@ -14,46 +14,25 @@ package org.eclipse.osee.framework.jdk.core.util.io.xml; import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; import org.eclipse.osee.framework.jdk.core.util.Strings; -import org.eclipse.osee.framework.jdk.core.util.io.CharBackedInputStream; /** * @author Ryan D. Brooks */ public final class TextSheetWriter extends AbstractSheetWriter { - - private final Map<String, CharBackedInputStream> sheetMap; - private CharBackedInputStream currentStream; + private Appendable out; private boolean wasDataAdded; private final String lineSeparator; private int columnCount; public TextSheetWriter() { - sheetMap = new LinkedHashMap<>(); lineSeparator = System.getProperty("line.separator", "\r\n"); - currentStream = null; - wasDataAdded = false; } public boolean hasData() { return wasDataAdded; } - public CharBackedInputStream getInputStream() { - return currentStream; - } - - public Set<String> getSheetNames() { - return sheetMap.keySet(); - } - - public CharBackedInputStream getSheetBackerByName(String tabName) { - return sheetMap.get(tabName); - } - @Override protected void startRow() { // do nothing @@ -64,10 +43,10 @@ public final class TextSheetWriter extends AbstractSheetWriter { if (data instanceof String) { String dataStr = (String) data; if (Strings.isValid(dataStr)) { - currentStream.append(dataStr); + out.append(dataStr); } if (cellIndex < columnCount - 1) { - currentStream.append("\t"); + out.append("\t"); } wasDataAdded = true; } @@ -75,13 +54,13 @@ public final class TextSheetWriter extends AbstractSheetWriter { @Override protected void writeEndRow() throws IOException { - currentStream.append(lineSeparator); + out.append(lineSeparator); wasDataAdded = true; } @Override public void endSheet() { - currentStream = null; + out = null; } @Override @@ -90,10 +69,13 @@ public final class TextSheetWriter extends AbstractSheetWriter { } @Override - public void startSheet(String worksheetName, int columnCount) throws IOException { + public void startSheet(String worksheetName, int columnCount) { this.columnCount = columnCount; - currentStream = new CharBackedInputStream(); - sheetMap.put(worksheetName, currentStream); + } + + @Override + public void startSheet(String worksheetName, int columnCount, Appendable out) { + this.out = out; } @Override diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/WordMlTableWriter.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/WordMlTableWriter.java index 631510321e5..f2884e9729d 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/WordMlTableWriter.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/io/xml/WordMlTableWriter.java @@ -29,24 +29,26 @@ public class WordMlTableWriter extends AbstractSheetWriter { private static final String CELL_END = "</w:t></w:r></w:p></w:tc>"; private static final String ROW_START = "<w:tr>"; private static final String ROW_END = "</w:tr>"; - private final Appendable str; + private Appendable out; private int columnSize; private boolean startTable; - public WordMlTableWriter(Appendable str) { - this.str = str; - } - @Override public void startSheet(String worksheetName, int columnCount) throws IOException { - str.append(TABLE_START); + out.append(TABLE_START); columnSize = columnCount; startTable = true; } @Override + public void startSheet(String worksheetName, int columnCount, Appendable out) throws IOException { + this.out = out; + startSheet(worksheetName, columnCount); + } + + @Override public void endSheet() throws IOException { - str.append(TABLE_END); + out.append(TABLE_END); } @Override @@ -56,30 +58,30 @@ public class WordMlTableWriter extends AbstractSheetWriter { writeTableGridData(); } - str.append(ROW_START); + out.append(ROW_START); } @Override public void writeEndRow() throws IOException { - str.append(ROW_END); + out.append(ROW_END); } private void writeTableGridData() throws IOException { - str.append(START_TABLE_GRID); + out.append(START_TABLE_GRID); for (int i = 0; i < columnSize; i++) { - str.append(TABLE_GRID); + out.append(TABLE_GRID); } - str.append(END_TABLE_GRID); + out.append(END_TABLE_GRID); } @Override public void writeCellText(Object cellData, int cellIndex) throws IOException { if (cellData instanceof String) { String cellDataStr = (String) cellData; - str.append(CELL_STRART); - str.append(cellDataStr); - str.append(CELL_END); + out.append(CELL_STRART); + out.append(cellDataStr); + out.append(CELL_END); } } |