use of org.apache.xmlbeans.XmlOptions in project poi by apache.
the class XWPFHeader method commit.
/**
* save and commit footer
*/
@Override
protected void commit() throws IOException {
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "hdr"));
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
super._getHdrFtr().save(out, xmlOptions);
out.close();
}
use of org.apache.xmlbeans.XmlOptions in project poi by apache.
the class XWPFStyles method commit.
@Override
protected void commit() throws IOException {
if (ctStyles == null) {
throw new IllegalStateException("Unable to write out styles that were never read in!");
}
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles"));
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
ctStyles.save(out, xmlOptions);
out.close();
}
use of org.apache.xmlbeans.XmlOptions in project poi by apache.
the class XSSFSheet method write.
protected void write(OutputStream out) throws IOException {
boolean setToNull = false;
if (worksheet.sizeOfColsArray() == 1) {
CTCols col = worksheet.getColsArray(0);
if (col.sizeOfColArray() == 0) {
setToNull = true;
// this is necessary so that we do not write an empty <cols/> item into the sheet-xml in the xlsx-file
// Excel complains about a corrupted file if this shows up there!
worksheet.setColsArray(null);
} else {
setColWidthAttribute(col);
}
}
// Now re-generate our CTHyperlinks, if needed
if (hyperlinks.size() > 0) {
if (worksheet.getHyperlinks() == null) {
worksheet.addNewHyperlinks();
}
CTHyperlink[] ctHls = new CTHyperlink[hyperlinks.size()];
for (int i = 0; i < ctHls.length; i++) {
// If our sheet has hyperlinks, have them add
// any relationships that they might need
XSSFHyperlink hyperlink = hyperlinks.get(i);
hyperlink.generateRelationIfNeeded(getPackagePart());
// Now grab their underling object
ctHls[i] = hyperlink.getCTHyperlink();
}
worksheet.getHyperlinks().setHyperlinkArray(ctHls);
} else {
if (worksheet.getHyperlinks() != null) {
final int count = worksheet.getHyperlinks().sizeOfHyperlinkArray();
for (int i = count - 1; i >= 0; i--) {
worksheet.getHyperlinks().removeHyperlink(i);
}
// For some reason, we have to remove the hyperlinks one by one from the CTHyperlinks array
// before unsetting the hyperlink array.
// Resetting the hyperlink array seems to break some XML nodes.
//worksheet.getHyperlinks().setHyperlinkArray(new CTHyperlink[0]);
worksheet.unsetHyperlinks();
} else {
// nothing to do
}
}
int minCell = Integer.MAX_VALUE, maxCell = Integer.MIN_VALUE;
for (XSSFRow row : _rows.values()) {
// first perform the normal write actions for the row
row.onDocumentWrite();
// then calculate min/max cell-numbers for the worksheet-dimension
if (row.getFirstCellNum() != -1) {
minCell = Math.min(minCell, row.getFirstCellNum());
}
if (row.getLastCellNum() != -1) {
maxCell = Math.max(maxCell, row.getLastCellNum());
}
}
// finally, if we had at least one cell we can populate the optional dimension-field
if (minCell != Integer.MAX_VALUE) {
String ref = new CellRangeAddress(getFirstRowNum(), getLastRowNum(), minCell, maxCell).formatAsString();
if (worksheet.isSetDimension()) {
worksheet.getDimension().setRef(ref);
} else {
worksheet.addNewDimension().setRef(ref);
}
}
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorksheet.type.getName().getNamespaceURI(), "worksheet"));
worksheet.save(out, xmlOptions);
// Bug 52233: Ensure that we have a col-array even if write() removed it
if (setToNull) {
worksheet.addNewCols();
}
}
use of org.apache.xmlbeans.XmlOptions in project poi by apache.
the class XSSFWorkbook method commit.
@Override
protected void commit() throws IOException {
saveNamedRanges();
saveCalculationChain();
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook"));
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
workbook.save(out, xmlOptions);
out.close();
}
use of org.apache.xmlbeans.XmlOptions in project hackpad by dropbox.
the class XML method createFromJS.
static XML createFromJS(XMLLibImpl lib, Object inputObject) {
XmlObject xo;
boolean isText = false;
String frag;
if (inputObject == null || inputObject == Undefined.instance) {
frag = "";
} else if (inputObject instanceof XMLObjectImpl) {
// todo: faster way for XMLObjects?
frag = ((XMLObjectImpl) inputObject).toXMLString(0);
} else {
if (inputObject instanceof Wrapper) {
Object wrapped = ((Wrapper) inputObject).unwrap();
if (wrapped instanceof XmlObject) {
return createFromXmlObject(lib, (XmlObject) wrapped);
}
}
frag = ScriptRuntime.toString(inputObject);
}
if (frag.trim().startsWith("<>")) {
throw ScriptRuntime.typeError("Invalid use of XML object anonymous tags <></>.");
}
if (frag.indexOf("<") == -1) {
// Must be solo text node, wrap in XML fragment
isText = true;
frag = "<textFragment>" + frag + "</textFragment>";
}
XmlOptions options = new XmlOptions();
if (lib.ignoreComments) {
options.put(XmlOptions.LOAD_STRIP_COMMENTS);
}
if (lib.ignoreProcessingInstructions) {
options.put(XmlOptions.LOAD_STRIP_PROCINSTS);
}
if (lib.ignoreWhitespace) {
options.put(XmlOptions.LOAD_STRIP_WHITESPACE);
}
try {
xo = XmlObject.Factory.parse(frag, options);
// Apply the default namespace
Context cx = Context.getCurrentContext();
String defaultURI = lib.getDefaultNamespaceURI(cx);
if (defaultURI.length() > 0) {
XmlCursor cursor = xo.newCursor();
boolean isRoot = true;
while (!cursor.toNextToken().isEnddoc()) {
if (!cursor.isStart())
continue;
// Check if this element explicitly sets the
// default namespace
boolean defaultNSDeclared = false;
cursor.push();
while (cursor.toNextToken().isAnyAttr()) {
if (cursor.isNamespace()) {
if (cursor.getName().getLocalPart().length() == 0) {
defaultNSDeclared = true;
break;
}
}
}
cursor.pop();
if (defaultNSDeclared) {
cursor.toEndToken();
continue;
}
// Check if this element's name is in no namespace
javax.xml.namespace.QName qname = cursor.getName();
if (qname.getNamespaceURI().length() == 0) {
// Change the namespace
qname = new javax.xml.namespace.QName(defaultURI, qname.getLocalPart());
cursor.setName(qname);
}
if (isRoot) {
// Declare the default namespace
cursor.push();
cursor.toNextToken();
cursor.insertNamespace("", defaultURI);
cursor.pop();
isRoot = false;
}
}
cursor.dispose();
}
} catch (XmlException xe) {
/*
todo need to handle namespace prefix not found in XML look for namespace type in the scope change.
String errorMsg = "Use of undefined namespace prefix: ";
String msg = xe.getError().getMessage();
if (msg.startsWith(errorMsg))
{
String prefix = msg.substring(errorMsg.length());
}
*/
String errMsg = xe.getMessage();
if (errMsg.equals("error: Unexpected end of file after null")) {
// Create an empty document.
xo = XmlObject.Factory.newInstance();
} else {
throw ScriptRuntime.typeError(xe.getMessage());
}
} catch (Throwable e) {
// todo: TLL Catch specific exceptions during parse.
throw ScriptRuntime.typeError("Not Parsable as XML");
}
XmlCursor curs = xo.newCursor();
if (curs.currentTokenType().isStartdoc()) {
curs.toFirstContentToken();
}
if (isText) {
// Move it to point to the text node
curs.toFirstContentToken();
}
XScriptAnnotation anno;
try {
anno = new XScriptAnnotation(curs);
curs.setBookmark(anno);
} finally {
curs.dispose();
}
return new XML(lib, anno);
}
Aggregations