Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #176 #197

Merged
merged 3 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.odftoolkit.odfdom.doc.OdfDocument.OdfMediaType;
import org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument;
import org.odftoolkit.odfdom.dom.OdfContentDom;
import org.odftoolkit.odfdom.dom.OdfContentOrStylesDomBase;
import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;
import org.odftoolkit.odfdom.dom.OdfSchemaDocument;
import org.odftoolkit.odfdom.dom.OdfStylesDom;
Expand Down Expand Up @@ -84,9 +85,7 @@ public class OdfTable {
private static final double DEFAULT_TABLE_WIDTH = 6;
private static final int DEFAULT_REL_TABLE_WIDTH = 65535;
private static final String DEFAULT_TABLE_ALIGN = "margins";
// TODO: should save seperately for different dom tree
static IdentityHashMap<TableTableElement, OdfTable> mTableRepository =
new IdentityHashMap<TableTableElement, OdfTable>();

IdentityHashMap<TableTableCellElementBase, Vector<OdfTableCell>> mCellRepository =
new IdentityHashMap<TableTableCellElementBase, Vector<OdfTableCell>>();
IdentityHashMap<TableTableRowElement, Vector<OdfTableRow>> mRowRepository =
Expand All @@ -110,12 +109,14 @@ private OdfTable(TableTableElement table) {
* @param odfElement an instance of <code>TableTableElement</code>
* @return an instance of <code>OdfTable</code> that can represent <code>odfElement</code>
*/
public static synchronized OdfTable getInstance(TableTableElement odfElement) {
if (mTableRepository.containsKey(odfElement)) {
return mTableRepository.get(odfElement);
public static OdfTable getInstance(TableTableElement odfElement) {
IdentityHashMap<TableTableElement, OdfTable> tableRepository =
((OdfContentOrStylesDomBase) odfElement.getOwnerDocument()).getTableRepository();
if (tableRepository.containsKey(odfElement)) {
return tableRepository.get(odfElement);
} else {
OdfTable newTable = new OdfTable(odfElement);
mTableRepository.put(odfElement, newTable);
tableRepository.put(odfElement, newTable);
return newTable;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.dom.OdfContentDom;
import org.odftoolkit.odfdom.dom.OdfContentOrStylesDomBase;
import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;
import org.odftoolkit.odfdom.dom.element.table.TableCoveredTableCellElement;
import org.odftoolkit.odfdom.dom.element.table.TableTableCellElement;
Expand Down Expand Up @@ -341,7 +341,7 @@ public OdfTableRow getPreviousRow() {
} else if (aPrevNode instanceof TableTableRowsElement
|| aPrevNode instanceof TableTableHeaderRowsElement
|| aPrevNode instanceof TableTableRowGroupElement) {
XPath xpath = ((OdfContentDom) aPrevNode.getOwnerDocument()).getXPath();
XPath xpath = ((OdfContentOrStylesDomBase) aPrevNode.getOwnerDocument()).getXPath();
synchronized (mDocument) {
lastRow =
(TableTableRowElement)
Expand Down Expand Up @@ -400,7 +400,7 @@ public OdfTableRow getNextRow() {
} else if (aNextNode instanceof TableTableRowsElement
|| aNextNode instanceof TableTableHeaderRowsElement
|| aNextNode instanceof TableTableRowGroupElement) {
XPath xpath = ((OdfContentDom) aNextNode.getOwnerDocument()).getXPath();
XPath xpath = ((OdfContentOrStylesDomBase) aNextNode.getOwnerDocument()).getXPath();
synchronized (mDocument) {
firstRow =
(TableTableRowElement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.odftoolkit.odfdom.changes.ChangesFileSaxHandler;
import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.doc.OdfTextDocument;
Expand All @@ -39,7 +37,6 @@
import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeMasterStyles;
import org.odftoolkit.odfdom.pkg.NamespaceName;
import org.odftoolkit.odfdom.pkg.OdfElement;
import org.odftoolkit.odfdom.pkg.OdfFileDom;
import org.odftoolkit.odfdom.pkg.OdfFileSaxHandler;
import org.odftoolkit.odfdom.pkg.OdfPackageDocument;
import org.odftoolkit.odfdom.pkg.OdfValidationException;
Expand All @@ -48,7 +45,7 @@
import org.xml.sax.SAXException;

/** The DOM representation of the ODF content.xml file of an ODF document. */
public class OdfContentDom extends OdfFileDom {
public class OdfContentDom extends OdfContentOrStylesDomBase {

private static final long serialVersionUID = 766167617530147883L;

Expand Down Expand Up @@ -96,16 +93,6 @@ protected void initialize() throws SAXException, IOException, ParserConfiguratio
}
}

/**
* Retrieves the ODF Document
*
* @return The <code>OdfDocument</code>
*/
@Override
public OdfSchemaDocument getDocument() {
return (OdfSchemaDocument) mPackageDocument;
}

/**
* @return The root element <office:document-content> of the content.xml file as <code>
* OfficeDocumentContentElement</code>.
Expand All @@ -115,21 +102,6 @@ public OfficeDocumentContentElement getRootElement() {
return (OfficeDocumentContentElement) getDocumentElement();
}

/**
* Creates an JDK <code>XPath</code> instance. Initialized with ODF namespaces from <code>
* OdfDocumentNamespace</code>. Updated with all namespace of the XML file.
*
* @return an XPath instance with namespace context set to include the standard ODFDOM prefixes.
*/
@Override
public XPath getXPath() {
if (mXPath == null) {
mXPath = XPathFactory.newInstance().newXPath();
mXPath.setNamespaceContext(this);
}
return mXPath;
}

/**
* Retrieve the ODF AutomaticStyles
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* **********************************************************************
*
* <p>DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
*
* <p>Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved.
*
* <p>Use is subject to license terms.
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0. You can also obtain a copy of the License at
* http://odftoolkit.org/docs/license.txt
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
*
* <p>See the License for the specific language governing permissions and limitations under the
* License.
*
* <p>**********************************************************************
*/
package org.odftoolkit.odfdom.dom;

import java.util.IdentityHashMap;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.odftoolkit.odfdom.doc.table.OdfTable;
import org.odftoolkit.odfdom.dom.element.table.TableTableElement;
import org.odftoolkit.odfdom.pkg.NamespaceName;
import org.odftoolkit.odfdom.pkg.OdfFileDom;
import org.odftoolkit.odfdom.pkg.OdfPackage;
import org.odftoolkit.odfdom.pkg.OdfPackageDocument;

/** Base class for an XML stream that contains document content, i.e., styles.xml or content.xml. */
public class OdfContentOrStylesDomBase extends OdfFileDom {

private static final long serialVersionUID = 6823264460360047745L;

IdentityHashMap<TableTableElement, OdfTable> mTableRepository =
new IdentityHashMap<TableTableElement, OdfTable>();

public OdfContentOrStylesDomBase(OdfPackageDocument packageDocument, String packagePath) {
super(packageDocument, packagePath);
}

public OdfContentOrStylesDomBase(OdfPackage pkg, String packagePath) {
super(pkg, packagePath);
}

/**
* Retrieves the ODF Document
*
* @return The <code>OdfDocument</code>
*/
@Override
public OdfSchemaDocument getDocument() {
return (OdfSchemaDocument) mPackageDocument;
}

/**
* Creates an JDK <code>XPath</code> instance. Initialized with ODF namespaces from <code>
* OdfDocumentNamespace</code>. Updated with all namespace of the XML file.
*
* @return an XPath instance with namespace context set to include the standard ODFDOM prefixes.
*/
@Override
public XPath getXPath() {
if (mXPath == null) {
mXPath = XPathFactory.newInstance().newXPath();
mXPath.setNamespaceContext(this);
for (NamespaceName name : OdfDocumentNamespace.values()) {
mUriByPrefix.put(name.getPrefix(), name.getUri());
mPrefixByUri.put(name.getUri(), name.getPrefix());
}
}
return mXPath;
}

public IdentityHashMap<TableTableElement, OdfTable> getTableRepository() {
return mTableRepository;
}
}
34 changes: 1 addition & 33 deletions odfdom/src/main/java/org/odftoolkit/odfdom/dom/OdfStylesDom.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.odftoolkit.odfdom.changes.ChangesFileSaxHandler;
import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.doc.OdfTextDocument;
Expand All @@ -41,7 +39,6 @@
import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeStyles;
import org.odftoolkit.odfdom.pkg.NamespaceName;
import org.odftoolkit.odfdom.pkg.OdfElement;
import org.odftoolkit.odfdom.pkg.OdfFileDom;
import org.odftoolkit.odfdom.pkg.OdfFileSaxHandler;
import org.odftoolkit.odfdom.pkg.OdfPackageDocument;
import org.odftoolkit.odfdom.pkg.OdfValidationException;
Expand All @@ -50,7 +47,7 @@
import org.xml.sax.SAXException;

/** The DOM representation of the ODF styles.xml file of an ODF document. */
public class OdfStylesDom extends OdfFileDom {
public class OdfStylesDom extends OdfContentOrStylesDomBase {

private static final long serialVersionUID = 766167617530147886L;

Expand Down Expand Up @@ -98,16 +95,6 @@ protected void initialize() throws SAXException, IOException, ParserConfiguratio
}
}

/**
* Retrieves the Odf Document
*
* @return The <code>OdfDocument</code>
*/
@Override
public OdfSchemaDocument getDocument() {
return (OdfSchemaDocument) mPackageDocument;
}

/**
* * @return The root element <office:document-styles> of the styles.xml file as <code>
* OfficeDocumentStylesElement</code>.
Expand All @@ -117,25 +104,6 @@ public OfficeDocumentStylesElement getRootElement() {
return (OfficeDocumentStylesElement) getDocumentElement();
}

/**
* Creates an JDK <code>XPath</code> instance. Initialized with ODF namespaces from <code>
* OdfDocumentNamespace</code>. Updated with all namespace of the XML file.
*
* @return an XPath instance with namespace context set to include the standard ODFDOM prefixes.
*/
@Override
public XPath getXPath() {
if (mXPath == null) {
mXPath = XPathFactory.newInstance().newXPath();
mXPath.setNamespaceContext(this);
for (NamespaceName name : OdfDocumentNamespace.values()) {
mUriByPrefix.put(name.getPrefix(), name.getUri());
mPrefixByUri.put(name.getUri(), name.getPrefix());
}
}
return mXPath;
}

// ToDo bug 72 - STYLE REFACTORING - THE FOLLOWING METHODS WILL BE RE/MOVED
// As Package layer should not refer to DOM/DOC layer and DOM files should not
// handle automatic styles the upcoming DOM Document should capsulate this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.doc.table.OdfTable;
import org.odftoolkit.odfdom.doc.table.OdfTableRow;
import org.odftoolkit.odfdom.dom.OdfContentDom;
import org.odftoolkit.odfdom.dom.OdfContentOrStylesDomBase;
import org.odftoolkit.odfdom.dom.OdfMetaDom;
import org.odftoolkit.odfdom.dom.OdfStylesDom;
import org.odftoolkit.odfdom.dom.element.draw.DrawObjectElement;
Expand Down Expand Up @@ -100,7 +100,7 @@ public static OdfEditableTextExtractor newOdfEditableTextExtractor(OdfElement el
public void visit(DrawObjectElement element) {
String embedDocPath = element.getXlinkHrefAttribute();
OdfDocument embedDoc =
((OdfDocument) (((OdfContentDom) element.getOwnerDocument()).getDocument()))
((OdfDocument) (((OdfContentOrStylesDomBase) element.getOwnerDocument()).getDocument()))
.loadSubDocument(embedDocPath);
if (embedDoc != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.junit.runner.RunWith;
import org.odftoolkit.junit.AlphabeticalOrderedRunner;
import org.odftoolkit.odfdom.dom.OdfContentDom;
import org.odftoolkit.odfdom.dom.OdfContentOrStylesDomBase;
import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;
import org.odftoolkit.odfdom.dom.attribute.text.TextAnchorTypeAttribute;
import org.odftoolkit.odfdom.dom.element.draw.DrawFrameElement;
Expand Down Expand Up @@ -512,7 +513,8 @@ public void _8_testWritingCorrectMimetype() {
}
}

private void addImageToDocument(OdfContentDom dom, TextPElement para) throws Exception {
private void addImageToDocument(OdfContentOrStylesDomBase dom, TextPElement para)
throws Exception {
OdfDrawFrame drawFrame = new OdfDrawFrame(dom);
drawFrame.setDrawNameAttribute("graphics1");
drawFrame.setTextAnchorTypeAttribute(TextAnchorTypeAttribute.Value.PARAGRAPH.toString());
Expand All @@ -526,7 +528,7 @@ private void addImageToDocument(OdfContentDom dom, TextPElement para) throws Exc
image.newImage(ResourceUtilities.getURI("test-input" + File.separatorChar + TEST_PIC));
}

private void addFrameForEmbeddedDoc(OdfContentDom dom, TextPElement para, String path)
private void addFrameForEmbeddedDoc(OdfContentOrStylesDomBase dom, TextPElement para, String path)
throws Exception {
OdfDrawFrame drawFrame = new OdfDrawFrame(dom);
drawFrame.setDrawNameAttribute(path);
Expand Down