From 2bfb174ec57c78236e2350c8ae3be9bf524e811c Mon Sep 17 00:00:00 2001 From: jmzambon Date: Sun, 3 Sep 2017 19:09:57 +0200 Subject: tdf#97362: TextPortionEnumerationTest partially migrated to python (last part) Convert tests: - test_meta_xtextattach_toxmark - test_meta_xtextattach_refmark - test_meta_xtextattach_textfield - test_meta_xtextattach_footnote - test_meta_xtextattach_meta - do_meta_xtextattach - test_metafield_xtextfield - test_metafield_xpropertyset - test_load_store - test_load_store_xmlid Change-Id: I471329e8088b533eedec6a55c521ed313f1960b4 Reviewed-on: https://gerrit.libreoffice.org/41863 Tested-by: Jenkins Reviewed-by: Miklos Vajna --- sw/JunitTest_sw_complex.mk | 2 - .../complex/writer/TextPortionEnumerationTest.java | 2058 -------------------- sw/qa/python/text_portion_enumeration_test.py | 542 +++++- 3 files changed, 541 insertions(+), 2061 deletions(-) delete mode 100644 sw/qa/complex/writer/TextPortionEnumerationTest.java (limited to 'sw') diff --git a/sw/JunitTest_sw_complex.mk b/sw/JunitTest_sw_complex.mk index 67eb2711350e..79b87a5094cb 100644 --- a/sw/JunitTest_sw_complex.mk +++ b/sw/JunitTest_sw_complex.mk @@ -28,7 +28,6 @@ $(eval $(call gb_JunitTest_add_sourcefiles,sw_complex,\ sw/qa/complex/accessibility/AccessibleRelationSet \ sw/qa/complex/indeterminateState/CheckIndeterminateState \ sw/qa/complex/writer/TestDocument \ - sw/qa/complex/writer/TextPortionEnumerationTest \ )) $(eval $(call gb_JunitTest_use_jars,sw_complex,\ @@ -41,7 +40,6 @@ $(eval $(call gb_JunitTest_use_jars,sw_complex,\ $(eval $(call gb_JunitTest_add_classes,sw_complex,\ complex.accessibility.AccessibleRelationSet \ - complex.writer.TextPortionEnumerationTest \ )) # vim: set noet sw=4 ts=4: diff --git a/sw/qa/complex/writer/TextPortionEnumerationTest.java b/sw/qa/complex/writer/TextPortionEnumerationTest.java deleted file mode 100644 index 255d39d619aa..000000000000 --- a/sw/qa/complex/writer/TextPortionEnumerationTest.java +++ /dev/null @@ -1,2058 +0,0 @@ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you 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 . - */ - -package complex.writer; - -import com.sun.star.uno.UnoRuntime; -import com.sun.star.uno.XComponentContext; -import com.sun.star.lang.XMultiServiceFactory; -import com.sun.star.lang.XComponent; -import com.sun.star.lang.XServiceInfo; -import com.sun.star.lang.NoSupportException; -import com.sun.star.beans.XPropertySet; -import com.sun.star.beans.XPropertySetInfo; -import com.sun.star.beans.PropertyValue; -import com.sun.star.beans.Pair; -import com.sun.star.beans.StringPair; -import com.sun.star.container.XNamed; -import com.sun.star.container.XChild; -import com.sun.star.container.XIndexAccess; -import com.sun.star.container.XNameAccess; -import com.sun.star.container.XContentEnumerationAccess; -import com.sun.star.container.XEnumerationAccess; -import com.sun.star.container.XEnumeration; -import com.sun.star.frame.XStorable; -import com.sun.star.util.XCloseable; -import com.sun.star.text.XText; -import com.sun.star.text.XTextContent; -import com.sun.star.text.XTextDocument; -import com.sun.star.text.XTextRange; -import com.sun.star.text.XTextCursor; -import com.sun.star.text.XWordCursor; -import com.sun.star.text.XSentenceCursor; -import com.sun.star.text.XParagraphCursor; -import com.sun.star.text.XFootnote; -import com.sun.star.text.XTextField; -import com.sun.star.text.XBookmarksSupplier; -import com.sun.star.text.XTextSectionsSupplier; -import com.sun.star.text.XDocumentIndexesSupplier; -import com.sun.star.text.TextContentAnchorType; -import static com.sun.star.text.TextContentAnchorType.*; -import static com.sun.star.text.ControlCharacter.*; -import com.sun.star.rdf.XMetadatable; -import com.sun.star.rdf.Statement; -import com.sun.star.rdf.XDocumentRepository; -import com.sun.star.rdf.XRepositorySupplier; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.openoffice.test.OfficeConnection; -import static org.junit.Assert.*; - -import java.util.Map; -import java.util.HashMap; -import java.util.List; -import java.util.ArrayList; -import java.util.Stack; - -class TreeNodeEnum -{ - private final Object[] m_Elements; - private int m_Pos; - TreeNodeEnum(Object[] Elements) { m_Elements = Elements; m_Pos = 0; } - boolean hasNext() { return m_Pos < m_Elements.length; } - TreeNode next() { return (TreeNode) m_Elements[m_Pos++]; } -} - -/** base class for tree nodes. only instance: root of tree. */ -class TreeNode -{ - private final List m_Children; - String getType() { return "__ROOT__"; } - boolean hasContent() { return false; } - boolean isNesting() { return false; } - TreeNode dup() { throw new RuntimeException("TreeNode.dup"); } - TreeNodeEnum createEnumeration() { - return new TreeNodeEnum(m_Children.toArray()); - } - TreeNode() { m_Children = new ArrayList(); } - TreeNode appendChild(TreeNode child) - { m_Children.add(child); return this; } - @Override - public String toString() { - return "<" + getType() + ">"; - } -} - -abstract class MarkNode extends TreeNode -{ - boolean m_isPoint; - boolean m_isStart = false; - private final String m_Name; - boolean isPoint() { return m_isPoint; } - boolean isStart() { return m_isStart; } - String getName() { return m_Name; } - MarkNode(String name) { m_Name = name; m_isPoint = true; } - boolean equals(MarkNode other) { - return (other.m_Name.equals(m_Name)) && (other.m_isPoint == m_isPoint) - && (other.m_isStart == m_isStart); - } - @Override - public String toString() { - return super.toString() + "\tname: " + m_Name + - "\tisPoint: " + m_isPoint + "\tisStart: " + m_isStart; - } -} - -class BookmarkNode extends MarkNode -{ - private StringPair m_XmlId; - StringPair getXmlId() { return m_XmlId; } - @Override - BookmarkNode dup() { return new BookmarkNode(getName(), getXmlId()); } - BookmarkNode(String name) { this(name, new StringPair()); } - BookmarkNode(String name, StringPair xmlid) { - super(name); m_XmlId = xmlid; - } - @Override - String getType() { return "Bookmark"; } - @Override - public boolean equals(Object other) { - return (other instanceof BookmarkNode) - && super.equals((MarkNode) other) - && MetaNode.eq(((BookmarkNode) other).m_XmlId, m_XmlId); - } - @Override - public String toString() { - return super.toString() - + "\txmlid: " + m_XmlId.First + "#" + m_XmlId.Second; - } -} - -class BookmarkStartNode extends BookmarkNode -{ - @Override - BookmarkStartNode dup() { return new BookmarkStartNode(getName()); } - BookmarkStartNode(String name) { this(name, new StringPair()); } - BookmarkStartNode(String name, StringPair xmlid) { - super(name, xmlid); m_isPoint = false; m_isStart = true; - } -} - -class BookmarkEndNode extends BookmarkNode -{ - @Override - BookmarkEndNode dup() { return new BookmarkEndNode(getName()); } - BookmarkEndNode(String name) { this(name, new StringPair()); } - BookmarkEndNode(String name, StringPair xmlid) { - super(name, xmlid); m_isPoint = false; m_isStart = false; - } -} - -class ReferenceMarkNode extends MarkNode -{ - @Override - ReferenceMarkNode dup() { return new ReferenceMarkNode(getName()); } - ReferenceMarkNode(String name) { super(name); } - @Override - String getType() { return "ReferenceMark"; } - @Override - public boolean equals(Object other) { - return (other instanceof ReferenceMarkNode) - && super.equals((MarkNode) other); - } -} - -class ReferenceMarkStartNode extends ReferenceMarkNode -{ - @Override - ReferenceMarkStartNode dup() - { return new ReferenceMarkStartNode(getName()); } - ReferenceMarkStartNode(String name) { - super(name); m_isPoint = false; m_isStart = true; - } -} - -class ReferenceMarkEndNode extends ReferenceMarkNode -{ - @Override - ReferenceMarkEndNode dup() - { return new ReferenceMarkEndNode(getName()); } - ReferenceMarkEndNode(String name) { - super(name); m_isPoint = false; m_isStart = false; - } -} - -class DocumentIndexMarkNode extends MarkNode -{ - @Override - DocumentIndexMarkNode dup() - { return new DocumentIndexMarkNode(getName()); } - DocumentIndexMarkNode(String name) { super(name); } - @Override - String getType() { return "DocumentIndexMark"; } - @Override - public boolean equals(Object other) { - return (other instanceof DocumentIndexMarkNode) - && super.equals((MarkNode) other); - } -} - -class DocumentIndexMarkStartNode extends DocumentIndexMarkNode -{ - @Override - DocumentIndexMarkStartNode dup() - { return new DocumentIndexMarkStartNode(getName()); } - DocumentIndexMarkStartNode(String name) { - super(name); m_isPoint = false; m_isStart = true; - } -} - -class DocumentIndexMarkEndNode extends DocumentIndexMarkNode -{ - @Override - DocumentIndexMarkEndNode dup() - { return new DocumentIndexMarkEndNode(getName()); } - DocumentIndexMarkEndNode(String name) { - super(name); m_isPoint = false; m_isStart = false; - } -} - -abstract class ContentNode extends TreeNode -{ - private final String m_Content; - String getContent() { return m_Content; } - @Override - boolean hasContent() { return true; } - ContentNode(String content) { - m_Content = content; - } - @Override - TreeNode appendChild(TreeNode t) { - throw new RuntimeException("ContentNode.appendChild"); - } - @Override - public String toString() { - return super.toString() + "\tcontent: " + m_Content; - } - boolean equals(ContentNode other) { - return other.m_Content.equals(m_Content); - } -} - -class TextNode extends ContentNode -{ - @Override - TextNode dup() { return new TextNode(getContent()); } - TextNode(String content) { super(content); } - @Override - String getType() { return "Text"; } - @Override - public boolean equals(Object other) { - return (other instanceof TextNode) && super.equals((ContentNode) other); - } -} - -class TextFieldNode extends ContentNode -{ - @Override - TextFieldNode dup() { return new TextFieldNode(getContent()); } - TextFieldNode(String content) { super(content); } - @Override - String getType() { return "TextField"; } - @Override - public boolean equals(Object other) { - return (other instanceof TextFieldNode) - && super.equals((ContentNode) other); - } -} - -class FrameNode extends TreeNode -{ - private final String m_Name; - private final TextContentAnchorType m_Anchor; - String getName() { return m_Name; } - TextContentAnchorType getAnchor() { return m_Anchor; } - @Override - FrameNode dup() { return new FrameNode(getName(), getAnchor()); } - FrameNode(String name, TextContentAnchorType anchor) { - m_Name = name; m_Anchor = anchor; - } - @Override - String getType() { return "Frame"; } - @Override - public boolean equals(Object other) { - return (other instanceof FrameNode) - && (((FrameNode) other).m_Name.equals(m_Name)) - && (((FrameNode) other).m_Anchor == m_Anchor); - } - @Override - public String toString() { - return super.toString() - + "\tname: " + m_Name + "\tanchor: " + toString(m_Anchor); - } - private static String toString(TextContentAnchorType anchor) { - switch (anchor.getValue()) { - case AS_CHARACTER_value: return "AS_CHARACTER"; - case AT_CHARACTER_value: return "AT_CHARACTER"; - case AT_PARAGRAPH_value: return "AT_PARAGRAPH"; - case AT_PAGE_value: return "AT_PAGE"; - case AT_FRAME_value: return "AT_FRAME"; - default: throw new RuntimeException("unknown anchor"); - } - } -} - -class FootnoteNode extends TreeNode -{ - private final String m_Label; - String getLabel() { return m_Label; } - @Override - FootnoteNode dup() { return new FootnoteNode(getLabel()); } - FootnoteNode(String label) { m_Label = label; } - @Override - String getType() { return "Footnote"; } - @Override - public boolean equals(Object other) { - return (other instanceof FootnoteNode) - && (((FootnoteNode) other).m_Label.equals(m_Label)); - } - @Override - public String toString() { - return super.toString() + "\tlabel: " + m_Label; - } -} - -class ControlCharacterNode extends TreeNode -{ - private final short m_Char; - short getChar() { return m_Char; } - @Override - ControlCharacterNode dup() { return new ControlCharacterNode(getChar()); } - ControlCharacterNode(short c) { m_Char = c; } - @Override - String getType() { return "ControlCharacter"; } - @Override - public boolean equals(Object other) { - return (other instanceof ControlCharacterNode) - && (((ControlCharacterNode) other).m_Char == m_Char); - } - @Override - public String toString() { - return super.toString() + "\tchar: " + m_Char; - } -} - -class SoftPageBreakNode extends TreeNode -{ - @Override - String getType() { return "SoftPageBreak"; } - @Override - public boolean equals(Object other) { - return (other instanceof SoftPageBreakNode); - } -} - -class HyperlinkNode extends TreeNode -{ - private final String m_URL; - String getURL() { return m_URL; } - @Override - HyperlinkNode dup() { return new HyperlinkNode(getURL()); } - HyperlinkNode(String url) { - if (url.length() == 0) throw new RuntimeException("HyperlinkNode"); - m_URL = url; - } - @Override - String getType() { return "Hyperlink"; } - @Override - boolean isNesting() { return true; } - @Override - public boolean equals(Object other) { - return (other instanceof HyperlinkNode) - && (((HyperlinkNode) other).m_URL.equals(m_URL)); - } - @Override - public String toString() { - return super.toString() + "\turl: " + m_URL; - } -} - -class RubyNode extends TreeNode -{ - private final String m_Ruby; - String getRubyText() { return m_Ruby; } - @Override - RubyNode dup() { return new RubyNode(getRubyText()); } - RubyNode(String ruby) { - if (ruby.length() == 0) throw new RuntimeException("RubyNode"); - m_Ruby = ruby; - } - @Override - String getType() { return "Ruby"; } - @Override - boolean isNesting() { return true; } - @Override - public boolean equals(Object other) { - return (other instanceof RubyNode) - && (((RubyNode) other).m_Ruby.equals(m_Ruby)); - } - @Override - public String toString() { - return super.toString() + "\trubytext: " + m_Ruby; - } -} - -class MetaNode extends TreeNode -{ - private final StringPair m_XmlId; - StringPair getXmlId() { return m_XmlId; } - @Override - MetaNode dup() { return new MetaNode(getXmlId()); } - MetaNode (StringPair xmlid) { m_XmlId = xmlid; } - @Override - String getType() { return "InContentMetadata"; } - @Override - boolean isNesting() { return true; } - @Override - public boolean equals(Object other) { - return (other instanceof MetaNode) - && eq(((MetaNode) other).m_XmlId, m_XmlId); - } - static boolean eq(StringPair left, StringPair right) - { - return left.First.equals(right.First) - && left.Second.equals(right.Second); - } - @Override - public String toString() { - return super.toString() - + "\txmlid: " + m_XmlId.First + "#" + m_XmlId.Second; - } -} - -class MetaFieldNode extends MetaNode -{ - @Override - MetaFieldNode dup() { return new MetaFieldNode(getXmlId()); } - MetaFieldNode (StringPair xmlid) { super(xmlid); } - @Override - String getType() { return "MetadataField"; } -} - -class Range -{ - private final short m_Start; - private final short m_End; - private final TreeNode m_Node; - short getStart() { return m_Start; } - short getExtent() { return (short) (m_End - m_Start); } - TreeNode getNode() { return m_Node; } - Range(int start, int end, TreeNode node) - { m_Start = (short) start; m_End = (short) end; m_Node = node; } -} - -/* this is where we nail the pudding to the wall */ -class FuzzyTester -{ - private long m_DiffContent = 0; - private long m_DiffMissing = 0; - private long m_DiffNesting = 0; - private long m_DiffSpuriousEmptyText = 0; - private long m_DiffSequence = 0; // ignored? - private final Stack> m_StackExpected; - private final Stack> m_StackActual; - private final List m_BufferExpected; - private final List m_BufferActual; - - FuzzyTester() { - m_BufferExpected = new ArrayList(); - m_BufferActual = new ArrayList(); - m_StackExpected = new Stack>(); - m_StackActual = new Stack>(); - } - - /** idea: traverse both trees, enumerate nodes, stopping at content nodes. - then compare buffers. */ - void doTest(TreeNode expected, TreeNode actual) - { - assertEquals("__ROOT__", expected.getType()); - assertEquals("__ROOT__", actual.getType()); - m_StackExpected.push(new Pair(expected, expected.createEnumeration())); - m_StackActual.push(new Pair(actual, actual.createEnumeration())); - do { - traverse(m_StackExpected, m_BufferExpected); - traverse(m_StackActual, m_BufferActual); - //??? root? - testBuffer(); - } while (!m_StackExpected.empty() || !m_StackActual.empty()); - if (m_DiffSequence != 0) { - System.out.println("warning: " + m_DiffSequence - + " differences in sequence"); - } - if (m_DiffSpuriousEmptyText != 0) { - System.out.println("warning: " + m_DiffSpuriousEmptyText - + " spurious empty text nodes"); - } - if (m_DiffNesting != 0) { - System.out.println("WARNING: " + m_DiffNesting - + " differences in nesting"); - } - assertEquals(0, m_DiffContent); - assertEquals(0, m_DiffMissing); - } - - private void traverse(Stack> stack, - List buffer) - { - while (!stack.empty()) { - TreeNodeEnum topEnum = stack.peek().Second; - if (topEnum.hasNext()) { - TreeNode node = topEnum.next(); - buffer.add(node); - TreeNodeEnum nodeEnum = node.createEnumeration(); - if (nodeEnum.hasNext()) { - stack.push(new Pair(node, nodeEnum)); - } - if (node.hasContent()) { - if (!((node instanceof TextNode) // spurious empty text? - && ((TextNode) node).getContent().length() == 0)) { - return; // break here - } - } - } else { - buffer.add(stack.peek().First); - stack.pop(); - } - } - } - - private void testTerminatingNode() - { - int lenExpected = m_BufferExpected.size(); - int lenActual = m_BufferActual.size(); - if (lenExpected == 0 || lenActual == 0) - return; - TreeNode expected = m_BufferExpected.get(lenExpected - 1); - TreeNode actual = m_BufferActual.get(lenActual - 1); - - boolean eRoot = expected.getType().equals("__ROOT__"); - boolean aRoot = actual.getType().equals("__ROOT__"); - if (eRoot || aRoot) { - if (!(eRoot && aRoot)) { - if (aRoot) printMissing(expected); - else printUnexpected(actual); - m_DiffMissing++; - } - return; - } - - testContentNode((ContentNode) expected, (ContentNode) actual); - - m_BufferExpected.set(lenExpected - 1, null); - m_BufferActual.set(lenActual - 1, null); - } - - private void testContentNode(ContentNode expected, ContentNode actual) - { - String contentExpected = expected.getContent(); - String contentActual = actual.getContent(); - if (!expected.equals(actual)) { - printDiff("text content differs", contentExpected, contentActual); - m_DiffContent++; - } - } - - private void testBuffer() - { - int lenExpected = m_BufferExpected.size(); - int lenActual = m_BufferActual.size(); - for (int i = 0; i < lenExpected - 1; i++ ) - { - TreeNode node = m_BufferExpected.get(i); - int j = m_BufferActual.indexOf(node); - if (j >= 0) { - m_BufferActual.get(j); - if (j != i) - { - //FIXME how bad is this? - printDiff("position differs", - String.valueOf(i), String.valueOf(j)); - // a hacky hack - int min = Math.min(i,j); - int max = Math.max(Math.min(lenActual - 1, i),j); - for (int k = min; k != max; k ++) { - TreeNode tmp = m_BufferActual.get(k); - if (tmp != null && tmp.isNesting()) { - printNesting(node, tmp); - m_DiffNesting++; - } - } - m_DiffSequence++; - } - m_BufferActual.set(j, null); - } else { - printMissing(node); - m_DiffMissing++; - } - } - for (int j = 0; j < lenActual - 1; j++) - { - TreeNode node = m_BufferActual.get(j); - if (node != null) - { - printUnexpected(node); - if ((node instanceof TextNode) && - ((TextNode) node).getContent().length() == 0) { - m_DiffSpuriousEmptyText++; - } else { - m_DiffMissing++; - } - } - } - testTerminatingNode(); - m_BufferExpected.clear(); - m_BufferActual.clear(); - } - - private void printDiff(String prefix, String expected, String actual) - { - System.out.println(prefix + - ":\texpected: " + expected + "\tactual: " + actual); - } - - private void printNesting(TreeNode node, TreeNode nesting) - { - System.out.println("node: " + node.toString() - + " possibly moved across nesting " + nesting.toString()); - } - - private void printMissing(TreeNode node) - { - System.out.println(" missing node: " + node.toString()); - - } - - private void printUnexpected(TreeNode node) - { - System.out.println("unexpected node: " + node.toString()); - - } -} - -class EnumConverter -{ - private final Stack m_Stack; - - EnumConverter() { - m_Stack = new Stack(); - } - - TreeNode convert(XEnumeration xEnum) throws Exception - { - TreeNode root = new TreeNode(); - m_Stack.push(root); - TreeNode ret = convertChildren(xEnum); - assertTrue("EnumConverter.convert: stack", m_Stack.empty()); - return ret; - } - - private TreeNode convertChildren(XEnumeration xEnum) throws Exception - { - while (xEnum.hasMoreElements()) { - TreeNode node; - Object xElement = xEnum.nextElement(); - XTextRange xPortion = UnoRuntime.queryInterface(XTextRange.class, xElement); - XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xPortion); - String type = (String) xPropSet.getPropertyValue("TextPortionType"); - if (type.equals("Text")) { - String text = xPortion.getString(); - node = new TextNode(text); - String url = (String) xPropSet.getPropertyValue("HyperLinkURL"); - if (url.length() > 0) { - TreeNode temp = node; - node = new HyperlinkNode(url); - node.appendChild(temp); - } - } else if (type.equals("TextField")) { - Object xField = xPropSet.getPropertyValue("TextField"); - XServiceInfo xService = UnoRuntime.queryInterface(XServiceInfo.class, xField); - if (xService.supportsService( - "com.sun.star.text.textfield.MetadataField")) - { - XMetadatable xMeta = UnoRuntime.queryInterface(XMetadatable.class, xField); - StringPair xmlid = xMeta.getMetadataReference(); - node = new MetaFieldNode(xmlid); - m_Stack.push(node); - XEnumerationAccess xEA = UnoRuntime.queryInterface(XEnumerationAccess.class, - xMeta); - XEnumeration xEnumChildren = xEA.createEnumeration(); - TreeNode node2 = convertChildren(xEnumChildren); - assertSame("stack error: meta-field", node2, node); - } else { - XPropertySet xFieldPropSet = UnoRuntime.queryInterface(XPropertySet.class, xField); - String content = (String) - xFieldPropSet.getPropertyValue("Content"); - boolean isFixed = (Boolean) - xFieldPropSet.getPropertyValue("IsFixed"); - assertTrue("field not fixed?", isFixed); - node = new TextFieldNode(content); - } - } else if (type.equals("Footnote")) { - Object xNote = xPropSet.getPropertyValue("Footnote"); - XFootnote xFootnote = UnoRuntime.queryInterface(XFootnote.class, xNote); - String label = xFootnote.getLabel(); - node = new FootnoteNode(label); - } else if (type.equals("Frame")) { - XContentEnumerationAccess xCEA = UnoRuntime.queryInterface(XContentEnumerationAccess.class, - xPortion); - XEnumeration xContentEnum = xCEA.createContentEnumeration(""); - while (xContentEnum.hasMoreElements()) { - Object xFrame = xContentEnum.nextElement(); - XPropertySet xFramePropSet = UnoRuntime.queryInterface(XPropertySet.class, xFrame); - TextContentAnchorType anchor = (TextContentAnchorType) - xFramePropSet.getPropertyValue("AnchorType"); - XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, xFrame); - String name = xNamed.getName(); - node = new FrameNode(name, anchor); - m_Stack.peek().appendChild(node); - } - continue; - } else if (type.equals("ControlCharacter")) { - short c = (Short) - xPropSet.getPropertyValue("ControlCharacter"); - node = new ControlCharacterNode(c); - } else if (type.equals("Bookmark")) { - Object xMark = xPropSet.getPropertyValue("Bookmark"); - XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, xMark); - String name = xNamed.getName(); - XMetadatable xMetadatable = UnoRuntime.queryInterface(XMetadatable.class, xMark); - StringPair xmlid = xMetadatable.getMetadataReference(); - boolean isCollapsed = (Boolean) - xPropSet.getPropertyValue("IsCollapsed"); - if (isCollapsed) { - node = new BookmarkNode(name, xmlid); - } else { - boolean isStart = (Boolean) - xPropSet.getPropertyValue("IsStart"); - if (isStart) { - node = new BookmarkStartNode(name, xmlid); - } else { - node = new BookmarkEndNode(name, xmlid); - } - } - } else if (type.equals("ReferenceMark")) { - Object xMark = xPropSet.getPropertyValue("ReferenceMark"); - XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, xMark); - String name = xNamed.getName(); - boolean isCollapsed = (Boolean) - xPropSet.getPropertyValue("IsCollapsed"); - if (isCollapsed) { - node = new ReferenceMarkNode(name); - } else { - boolean isStart = (Boolean) - xPropSet.getPropertyValue("IsStart"); - if (isStart) { - node = new ReferenceMarkStartNode(name); - } else { - node = new ReferenceMarkEndNode(name); - } - } - } else if (type.equals("DocumentIndexMark")) { - Object xMark = xPropSet.getPropertyValue("DocumentIndexMark"); - XPropertySet xMarkSet = UnoRuntime.queryInterface(XPropertySet.class, xMark); - String name = (String) xMarkSet.getPropertyValue("PrimaryKey"); - boolean isCollapsed = (Boolean) - xPropSet.getPropertyValue("IsCollapsed"); - if (isCollapsed) { - node = new DocumentIndexMarkNode(name); - } else { - boolean isStart = (Boolean) - xPropSet.getPropertyValue("IsStart"); - if (isStart) { - node = new DocumentIndexMarkStartNode(name); - } else { - node = new DocumentIndexMarkEndNode(name); - } - } - } else if (type.equals("SoftPageBreak")) { - node = new SoftPageBreakNode(); - } else if (type.equals("Ruby")) { - boolean isStart = (Boolean) - xPropSet.getPropertyValue("IsStart"); - if (isStart) { - // ARRGH!!! stupid api... - // the text is ONLY at the start! - String ruby = (String) - xPropSet.getPropertyValue("RubyText"); - node = new RubyNode(ruby); - m_Stack.push(node); - continue; - } else { - node = m_Stack.pop(); - assertTrue("stack error: Ruby expected; is: " + - node.toString(), node instanceof RubyNode); - } - } else if (type.equals("InContentMetadata")) { - Object xMeta = xPropSet.getPropertyValue("InContentMetadata"); - XMetadatable xMetadatable = UnoRuntime.queryInterface(XMetadatable.class, xMeta); - StringPair xmlid = xMetadatable.getMetadataReference(); - node = new MetaNode(xmlid); - m_Stack.push(node); - XEnumerationAccess xEA = UnoRuntime.queryInterface(XEnumerationAccess.class, xMeta); - XEnumeration xEnumChildren = xEA.createEnumeration(); - TreeNode node2 = convertChildren(xEnumChildren); - assertSame("stack error: meta", node2, node); - } else { - throw new RuntimeException("unexpected type: " + type); - } - m_Stack.peek().appendChild(node); - } - TreeNode ret = m_Stack.pop(); - return ret; - } -} - -abstract class Inserter -{ - private final XMultiServiceFactory m_xDocFactory; - XText m_xText; - XParagraphCursor m_xCursor; - - Inserter(XTextDocument xDoc) - { - m_xDocFactory = UnoRuntime.queryInterface(XMultiServiceFactory.class, xDoc); - m_xText = xDoc.getText(); - XTextCursor xCursor = m_xText.createTextCursor(); - m_xCursor = UnoRuntime.queryInterface(XParagraphCursor.class, xCursor); - } - - void initParagraph() throws Exception - { - m_xCursor.gotoStartOfParagraph(false); - m_xText.insertControlCharacter(m_xCursor, PARAGRAPH_BREAK, false); - // we split the first (empty) paragraph, and then insert into the - // second (empty) paragraph; this ensures first is always empty! - } - - void insertControlCharacter(XTextCursor xCursor, short cchar) - throws Exception - { - m_xText.insertControlCharacter(xCursor, cchar, false); - } - - void insertText(XTextCursor xCursor, String text) - { - xCursor.setString(text); - } - - void insertTextField(XTextCursor xCursor, String content) throws Exception - { - XTextContent xContent = makeTextField(content); - xContent.attach(xCursor); - } - - XTextContent makeTextField(String content) - throws Exception - { - Object xField = - m_xDocFactory.createInstance("com.sun.star.text.textfield.Author"); - XTextContent xContent = UnoRuntime.queryInterface(XTextContent.class, xField); - XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xField); - xPropSet.setPropertyValue("IsFixed", true); - xPropSet.setPropertyValue("FullName", false); - xPropSet.setPropertyValue("Content", content); - return xContent; - } - - void insertFrame(XTextRange xCursor, String name, - TextContentAnchorType anchor) throws Exception - { - XTextContent xContent = makeFrame(name, anchor); - xContent.attach(xCursor); - } - - private XTextContent makeFrame(String name, TextContentAnchorType anchor) - throws Exception - { - Object xFrame = - m_xDocFactory.createInstance("com.sun.star.text.TextFrame"); - XTextContent xContent = UnoRuntime.queryInterface(XTextContent.class, xFrame); - XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xFrame); - xPropSet.setPropertyValue("AnchorType", anchor); - XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, xContent); - xNamed.setName(name); - return xContent; - } - - void insertFootnote(XTextCursor xCursor, String label) throws Exception - { - XTextContent xContent = makeFootnote(label); - xContent.attach(xCursor); - } - - XTextContent makeFootnote(String label) throws Exception - { - Object xNote = - m_xDocFactory.createInstance("com.sun.star.text.Footnote"); - XTextContent xContent = UnoRuntime.queryInterface(XTextContent.class, xNote); - XFootnote xFootnote = UnoRuntime.queryInterface(XFootnote.class, xNote); - xFootnote.setLabel(label); - return xContent; - } - - void insertBookmark(XTextCursor xCursor, String name, StringPair xmlid) - throws Exception - { - XTextContent xContent = makeBookmark(name); - xContent.attach(xCursor); - if (!xmlid.First.equals("")) - { - XMetadatable xMetadatable = UnoRuntime.queryInterface(XMetadatable.class, xContent); - xMetadatable.setMetadataReference(xmlid); - } - } - - private XTextContent makeBookmark(String name) throws Exception - { - Object xBookmark = - m_xDocFactory.createInstance("com.sun.star.text.Bookmark"); - XTextContent xContent = UnoRuntime.queryInterface(XTextContent.class, xBookmark); - XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, xContent); - xNamed.setName(name); - return xContent; - } - - void insertReferenceMark(XTextCursor xCursor, String name) throws Exception - { - XTextContent xContent = makeReferenceMark(name); - xContent.attach(xCursor); - } - - XTextContent makeReferenceMark(String name) throws Exception - { - Object xMark = - m_xDocFactory.createInstance("com.sun.star.text.ReferenceMark"); - XTextContent xContent = UnoRuntime.queryInterface(XTextContent.class, xMark); - XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, xContent); - xNamed.setName(name); - return xContent; - } - - void insertDocumentIndexMark(XTextCursor xCursor, String key) - throws Exception - { - XTextContent xContent = makeDocumentIndexMark(key); - xContent.attach(xCursor); - } - - XTextContent makeDocumentIndexMark(String key) throws Exception - { - Object xMark = - m_xDocFactory.createInstance("com.sun.star.text.DocumentIndexMark"); - XTextContent xContent = UnoRuntime.queryInterface(XTextContent.class, xMark); - XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xMark); - xPropSet.setPropertyValue("PrimaryKey", key); - return xContent; - } - - void insertHyperlink(XTextCursor xCursor, String url) throws Exception - { - XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xCursor); - xPropSet.setPropertyValue("HyperLinkURL", url); - } - - void insertRuby(XTextCursor xCursor, String rubytext) throws Exception - { - XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xCursor); - xPropSet.setPropertyValue("RubyText", rubytext); - } - - XTextContent insertMeta(XTextCursor xCursor, StringPair xmlid) - throws Exception - { - XTextContent xContent = makeMeta(); - xContent.attach(xCursor); - XMetadatable xMetadatable = UnoRuntime.queryInterface(XMetadatable.class, xContent); - xMetadatable.setMetadataReference(xmlid); - return xContent; - } - - XTextContent makeMeta() throws Exception - { - Object xMeta = m_xDocFactory.createInstance( - "com.sun.star.text.InContentMetadata"); - XTextContent xContent = UnoRuntime.queryInterface(XTextContent.class, xMeta); - return xContent; - } - - XTextField insertMetaField(XTextCursor xCursor, StringPair xmlid) - throws Exception - { - XTextField xContent = makeMetaField(); - xContent.attach(xCursor); - XMetadatable xMetadatable = UnoRuntime.queryInterface(XMetadatable.class, xContent); - xMetadatable.setMetadataReference(xmlid); - return xContent; - } - - XTextField makeMetaField() throws Exception - { - Object xMeta = m_xDocFactory.createInstance( - "com.sun.star.text.textfield.MetadataField"); - XTextField xContent = UnoRuntime.queryInterface(XTextField.class, xMeta); - return xContent; - } - -} - -class TreeInserter extends Inserter -{ - private final Map m_BookmarkStarts; - private final Map m_ReferenceMarkStarts; - private final Map m_DocumentIndexMarkStarts; - private final List> m_FrameHints; - - TreeInserter(XTextDocument xDoc) - { - super(xDoc); - m_BookmarkStarts = new HashMap(); - m_ReferenceMarkStarts = new HashMap(); - m_DocumentIndexMarkStarts = new HashMap(); - m_FrameHints = new ArrayList>(); - } - - void insertTree(TreeNode tree) throws Exception - { - if (!tree.getType().equals("__ROOT__")) - throw new RuntimeException("insertTree: test error: no root"); - initParagraph(); - insertChildren(tree.createEnumeration()); - for (int i = 0; i < m_FrameHints.size(); ++i) { - Pair p = m_FrameHints.get(i); - insertFrame(p.First, p.Second.getName(), p.Second.getAnchor()); - } - } - - private void insertChildren(TreeNodeEnum children) throws Exception - { - while (children.hasNext()) { - m_xCursor.gotoEndOfParagraph(false); - TreeNode node = children.next(); - String type = node.getType(); - if (type.equals("Bookmark")) { - BookmarkNode bkmk = (BookmarkNode) node; - String name = bkmk.getName(); - StringPair id = bkmk.getXmlId(); - if (bkmk.isPoint()) { - insertBookmark(m_xCursor, name, id); - } else if (bkmk.isStart()) { - m_BookmarkStarts.put(name, m_xCursor.getStart()); - } else { - XTextRange xRange = m_BookmarkStarts.get(name); - XParagraphCursor xCursor = mkCursor(xRange); - insertBookmark(xCursor, name, id); - } - } else if (type.equals("ReferenceMark")) { - ReferenceMarkNode mark = (ReferenceMarkNode) node; - String name = mark.getName(); - if (mark.isPoint()) { - insertReferenceMark(m_xCursor, name); - } else if (mark.isStart()) { - m_ReferenceMarkStarts.put(name, m_xCursor.getStart()); - } else { - XTextRange xRange = m_ReferenceMarkStarts.get(name); - XParagraphCursor xCursor = mkCursor(xRange); - insertReferenceMark(xCursor, name); - } - } else if (type.equals("DocumentIndexMark")) { - DocumentIndexMarkNode mark = (DocumentIndexMarkNode) node; - String name = mark.getName(); - if (mark.isPoint()) { - insertDocumentIndexMark(m_xCursor, name); - } else if (mark.isStart()) { - m_DocumentIndexMarkStarts.put(name, m_xCursor.getStart()); - } else { - XTextRange xRange = m_DocumentIndexMarkStarts.get(name); - XParagraphCursor xCursor = mkCursor(xRange); - insertDocumentIndexMark(xCursor, name); - } - } else if (type.equals("Hyperlink")) { - HyperlinkNode href = (HyperlinkNode) node; - XTextRange xRange = m_xCursor.getStart(); - insertChildren(href.createEnumeration()); - XParagraphCursor xCursor = mkCursor(xRange); - insertHyperlink(xCursor, href.getURL()); - } else if (type.equals("Ruby")) { - RubyNode ruby = (RubyNode) node; - XTextRange xRange = m_xCursor.getStart(); - insertChildren(ruby.createEnumeration()); - XParagraphCursor xCursor = mkCursor(xRange); - insertRuby(xCursor, ruby.getRubyText()); - } else if (type.equals("InContentMetadata")) { - MetaNode meta = (MetaNode) node; - XTextRange xRange = m_xCursor.getStart(); - insertChildren(meta.createEnumeration()); - XParagraphCursor xCursor = mkCursor(xRange); - insertMeta(xCursor, meta.getXmlId()); - } else if (type.equals("MetadataField")) { - MetaFieldNode meta = (MetaFieldNode) node; - XTextRange xRange = m_xCursor.getStart(); - insertChildren(meta.createEnumeration()); - XParagraphCursor xCursor = mkCursor(xRange); - insertMetaField(xCursor, meta.getXmlId()); - } else if (type.equals("Text")) { - TextNode text = (TextNode) node; - insertText(m_xCursor, text.getContent()); - } else if (type.equals("TextField")) { - TextFieldNode field = (TextFieldNode) node; - insertTextField(m_xCursor, field.getContent()); - } else if (type.equals("Footnote")) { - FootnoteNode note = (FootnoteNode) node; - insertFootnote(m_xCursor, note.getLabel()); - } else if (type.equals("Frame")) { - FrameNode frame = (FrameNode) node; - if (frame.getAnchor() == AT_CHARACTER) { - m_FrameHints.add( new Pair(m_xCursor.getStart(), frame) ); - } else { - insertFrame(m_xCursor, frame.getName(), frame.getAnchor()); - } - } else if (type.equals("ControlCharacter")) { - ControlCharacterNode cchar = (ControlCharacterNode) node; - insertControlCharacter(m_xCursor, cchar.getChar()); - } else if (type.equals("SoftPageBreak")) { - SoftPageBreakNode spbk = (SoftPageBreakNode) node; - throw new RuntimeException("sorry, cannot test SoftPageBreak"); - } else { - throw new RuntimeException("unexpected type: " + type); - } - } - } - - private XParagraphCursor mkCursor(XTextRange xRange) - { - XTextCursor xCursor = m_xText.createTextCursorByRange(xRange); - XParagraphCursor xParaCursor = UnoRuntime.queryInterface(XParagraphCursor.class, xCursor); - xParaCursor.gotoEndOfParagraph(true); - return xParaCursor; - } -} - -// FIXME: this does not account for inserted dummy characters! -class RangeInserter extends Inserter -{ - RangeInserter(XTextDocument xDoc) throws Exception - { super(xDoc); initParagraph(); } - - /* - void insertText(int pos, String text) - { - m_xCursor.gotoStartOfParagraph(false); - m_xCursor.goRight((short) pos, false); - insertText(m_xCursor, text); - } - */ - - XTextContent insertRange(Range range) throws Exception - { - m_xCursor.gotoStartOfParagraph(false); - m_xCursor.goRight(range.getStart(), false); - m_xCursor.goRight(range.getExtent(), true); - return insertNode(m_xCursor, range.getNode()); - } - - private XTextContent insertNode(XParagraphCursor xCursor, TreeNode node) - throws Exception - { - String type = node.getType(); - if (type.equals("Bookmark")) { - BookmarkNode bkmk = (BookmarkNode) node; - if (bkmk.isPoint()) throw new RuntimeException("range only"); - insertBookmark(xCursor, bkmk.getName(), bkmk.getXmlId()); - } else if (type.equals("ReferenceMark")) { - ReferenceMarkNode mark = (ReferenceMarkNode) node; - if (mark.isPoint()) throw new RuntimeException("range only"); - insertReferenceMark(xCursor, mark.getName()); - } else if (type.equals("DocumentIndexMark")) { - DocumentIndexMarkNode mark = (DocumentIndexMarkNode) node; - if (mark.isPoint()) throw new RuntimeException("range only"); - insertDocumentIndexMark(xCursor, mark.getName()); - } else if (type.equals("Hyperlink")) { - HyperlinkNode href = (HyperlinkNode) node; - insertHyperlink(xCursor, href.getURL()); - } else if (type.equals("Ruby")) { - RubyNode ruby = (RubyNode) node; - insertRuby(xCursor, ruby.getRubyText()); - } else if (type.equals("InContentMetadata")) { - MetaNode meta = (MetaNode) node; - return insertMeta(xCursor, meta.getXmlId()); - } else if (type.equals("MetadataField")) { - MetaFieldNode meta = (MetaFieldNode) node; - return insertMetaField(xCursor, meta.getXmlId()); - } else if (type.equals("Text")) { - TextNode text = (TextNode) node; - insertText(xCursor, text.getContent()); - } else if (type.equals("TextField")) { - TextFieldNode field = (TextFieldNode) node; - insertTextField(m_xCursor, field.getContent()); - } else if (type.equals("Footnote")) { - FootnoteNode note = (FootnoteNode) node; - insertFootnote(m_xCursor, note.getLabel()); - } else if (type.equals("Frame")) { - FrameNode frame = (FrameNode) node; - insertFrame(xCursor, frame.getName(), frame.getAnchor()); - } else if (type.equals("ControlCharacter")) { - ControlCharacterNode cchar = (ControlCharacterNode) node; - insertControlCharacter(m_xCursor, cchar.getChar()); - } else if (type.equals("SoftPageBreak")) { - throw new RuntimeException("sorry, cannot test SoftPageBreak"); - } else { - throw new RuntimeException("unexpected type: " + type); - } - return null; - } -} - -public class TextPortionEnumerationTest -{ - private XMultiServiceFactory m_xMSF = null; - private XComponentContext m_xContext = null; - private XTextDocument m_xDoc = null; - private String m_TmpDir = null; - - private int m_Count = 1; - - @Before public void before() throws Exception - { - m_xMSF = UnoRuntime.queryInterface( - XMultiServiceFactory.class, - connection.getComponentContext().getServiceManager()); - XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, m_xMSF); - Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext"); - m_xContext = UnoRuntime.queryInterface(XComponentContext.class, defaultCtx); - assertNotNull("could not get component context.", m_xContext); - m_xDoc = util.WriterTools.createTextDoc(m_xMSF); - m_TmpDir = util.utils.getOfficeTemp/*Dir*/(m_xMSF); - System.out.println("tempdir: " + m_TmpDir); - } - - @After public void after() - { - util.DesktopTools.closeDoc(m_xDoc); - } - - private abstract class AttachHelper - { - abstract boolean isAttribute(); - abstract TreeNode mkTreeNode(); - abstract XTextContent mkTextContent(Inserter inserter, TreeNode node) - throws Exception; - void postInserted(TreeNode node, XTextContent xContent) - throws Exception { } - } - - @Test public void testMetaXTextAttachToxMark() throws Exception - { - doMetaXTextAttach( new AttachHelper() - { - @Override - boolean isAttribute() { return true; } - @Override - TreeNode mkTreeNode() { - return new DocumentIndexMarkNode( mkName("toxmark") ); - } - @Override - XTextContent mkTextContent(Inserter inserter, TreeNode node) - throws Exception { - return inserter.makeDocumentIndexMark( - ((DocumentIndexMarkNode)node).getName()); - } - }); - } - - @Test public void testMetaXTextAttachRefMark() throws Exception - { - doMetaXTextAttach( new AttachHelper() - { - @Override - boolean isAttribute() { return true; } - @Override - TreeNode mkTreeNode() { - return new ReferenceMarkNode( mkName("refmark") ); - } - @Override - XTextContent mkTextContent(Inserter inserter, TreeNode node) - throws Exception { - return inserter.makeReferenceMark( - ((ReferenceMarkNode)node).getName()); - } - }); - } - - @Test public void testMetaXTextAttachTextField() throws Exception - { - doMetaXTextAttach( new AttachHelper() - { - @Override - boolean isAttribute() { return false; } - @Override - TreeNode mkTreeNode() { - return new TextFieldNode( mkName("field") ); - } - @Override - XTextContent mkTextContent(Inserter inserter, TreeNode node) - throws Exception { - return inserter.makeTextField( - ((TextFieldNode)node).getContent()); - } - }); - } - - @Test public void testMetaXTextAttachFootnote() throws Exception - { - doMetaXTextAttach( new AttachHelper() - { - @Override - boolean isAttribute() { return false; } - @Override - TreeNode mkTreeNode() { - return new FootnoteNode( mkName("ftn") ); - } - @Override - XTextContent mkTextContent(Inserter inserter, TreeNode node) - throws Exception { - return inserter.makeFootnote( - ((FootnoteNode)node).getLabel()); - } - }); - } - - @Test public void testMetaXTextAttachMeta() throws Exception - { - doMetaXTextAttach( new AttachHelper() - { - @Override - boolean isAttribute() { return true; } - @Override - TreeNode mkTreeNode() { - return new MetaNode( mkId("id") ); - } - @Override - XTextContent mkTextContent(Inserter inserter, TreeNode node) - throws Exception { - return inserter.makeMeta(); - } - @Override - void postInserted(TreeNode node, XTextContent xContent) - throws Exception { - XMetadatable xMetadatable = UnoRuntime.queryInterface(XMetadatable.class, xContent); - xMetadatable.setMetadataReference( - ((MetaNode)node).getXmlId()); - } - }); - } - - void doMetaXTextAttach(AttachHelper helper) throws Exception - { - RangeInserter inserter = new RangeInserter(m_xDoc); - TreeNode text = new TextNode("12AB6789"); - inserter.insertRange( new Range(0, 0, text) ); - MetaNode met1 = new MetaNode( mkId("id") ); - XTextContent xMeta = inserter.makeMeta(); - - XText xDocText = m_xDoc.getText(); - XTextCursor xDocTextCursor = xDocText.createTextCursor(); - xDocTextCursor.goRight((short)3, false); - xDocTextCursor.goRight((short)2, true); - xDocText.insertTextContent(xDocTextCursor, xMeta, true); - - XMetadatable xMetadatable = UnoRuntime.queryInterface(XMetadatable.class, xMeta); - xMetadatable.setMetadataReference(met1.getXmlId()); - XText xText = UnoRuntime.queryInterface(XText.class, xMeta); - XTextRange xStart = null; - XTextRange xEnd = null; - - { - xStart = xText.getStart(); - xEnd = xText.getEnd(); - - TreeNode nod1 = helper.mkTreeNode(); - TreeNode nod2 = helper.mkTreeNode(); - XTextContent xContent1 = helper.mkTextContent(inserter, nod1); - XTextContent xContent2 = helper.mkTextContent(inserter, nod2); - - // insertTextContent with meta getStart()/getEnd() - xText.insertTextContent(xStart, xContent1, false); - xText.insertTextContent(xEnd , xContent2, false); - - helper.postInserted(nod1, xContent1); - helper.postInserted(nod2, xContent2); - - TreeNode root = new TreeNode() - .appendChild( new TextNode("12") ) - .appendChild( met1.dup() - .appendChild( nod1.dup() ) - .appendChild( new TextNode("AB") ) - .appendChild( nod2.dup() ) ) - .appendChild( new TextNode("6789") ); - doTest(root, false); - } - { - xText.setString("AB"); - xStart = xText.getStart(); - xEnd = xText.getEnd(); - - TreeNode nod1 = helper.mkTreeNode(); - TreeNode nod2 = helper.mkTreeNode(); - XTextContent xContent1 = helper.mkTextContent(inserter, nod1); - XTextContent xContent2 = helper.mkTextContent(inserter, nod2); - - XTextCursor xTextCursor = xText.createTextCursor(); - xTextCursor.gotoStart(false); - - // insertTextContent with meta cursor - xText.insertTextContent(xTextCursor, xContent1, false); - xTextCursor.gotoEnd(false); - xText.insertTextContent(xTextCursor, xContent2, false); - - helper.postInserted(nod1, xContent1); - helper.postInserted(nod2, xContent2); - - TreeNode root = new TreeNode() - .appendChild( new TextNode("12") ) - .appendChild( met1.dup() - .appendChild( nod1.dup() ) - .appendChild( new TextNode("AB") ) - .appendChild( nod2.dup() ) ) - .appendChild( new TextNode("6789") ); - doTest(root, false); - } - if (!helper.isAttribute()) - { -// xText.setString("AB"); - xStart = xText.getStart(); - xEnd = xText.getEnd(); - - TreeNode nod1 = helper.mkTreeNode(); - TreeNode nod2 = helper.mkTreeNode(); - XTextContent xContent1 = helper.mkTextContent(inserter, nod1); - XTextContent xContent2 = helper.mkTextContent(inserter, nod2); - - XTextCursor xTextCursor = xText.createTextCursor(); - xTextCursor.gotoStart(false); - xTextCursor.goRight((short)1, true); - - // insertTextContent with meta cursor and absorb - xText.insertTextContent(xTextCursor, xContent1, true); - xTextCursor.gotoEnd(false); - xTextCursor.goLeft((short)1, true); - xText.insertTextContent(xTextCursor, xContent2, true); - - helper.postInserted(nod1, xContent1); - helper.postInserted(nod2, xContent2); - - TreeNode root = new TreeNode() - .appendChild( new TextNode("12") ) - .appendChild( met1.dup() - .appendChild( nod1.dup() ) - .appendChild( new TextNode("AB") ) - .appendChild( nod2.dup() ) ) - .appendChild( new TextNode("6789") ); - doTest(root, false); - } - { - xText.setString("AB"); - xStart = xText.getStart(); - xEnd = xText.getEnd(); - - TreeNode nod1 = helper.mkTreeNode(); - TreeNode nod2 = helper.mkTreeNode(); - XTextContent xContent1 = helper.mkTextContent(inserter, nod1); - XTextContent xContent2 = helper.mkTextContent(inserter, nod2); - - xDocTextCursor.gotoRange(xStart, false); - - // insertTextContent with document cursor - xText.insertTextContent(xDocTextCursor, xContent1, false); - xDocTextCursor.gotoRange(xEnd, false); - xText.insertTextContent(xDocTextCursor, xContent2, false); - - helper.postInserted(nod1, xContent1); - helper.postInserted(nod2, xContent2); - - TreeNode root = new TreeNode() - .appendChild( new TextNode("12") ) - .appendChild( met1.dup() - .appendChild( nod1.dup() ) - .appendChild( new TextNode("AB") ) - .appendChild( nod2.dup() ) ) - .appendChild( new TextNode("6789") ); - doTest(root, false); - } - if (!helper.isAttribute()) - { - xStart = xText.getStart(); - xEnd = xText.getEnd(); - - TreeNode nod1 = helper.mkTreeNode(); - TreeNode nod2 = helper.mkTreeNode(); - XTextContent xContent1 = helper.mkTextContent(inserter, nod1); - XTextContent xContent2 = helper.mkTextContent(inserter, nod2); - - xDocTextCursor.gotoRange(xStart, false); - xDocTextCursor.goRight((short)1, true); - - // insertTextContent with document cursor and absorb - xText.insertTextContent(xDocTextCursor, xContent1, true); - xDocTextCursor.gotoRange(xEnd, false); - xDocTextCursor.goLeft((short)1, true); - xText.insertTextContent(xDocTextCursor, xContent2, true); - - helper.postInserted(nod1, xContent1); - helper.postInserted(nod2, xContent2); - - TreeNode root = new TreeNode() - .appendChild( new TextNode("12") ) - .appendChild( met1.dup() - .appendChild( nod1.dup() ) - .appendChild( new TextNode("AB") ) - .appendChild( nod2.dup() ) ) - .appendChild( new TextNode("6789") ); - doTest(root, false); - } - { - xText.setString("AB"); - xStart = xText.getStart(); - xEnd = xText.getEnd(); - - TreeNode nod1 = helper.mkTreeNode(); - TreeNode nod2 = helper.mkTreeNode(); - XTextContent xContent1 = helper.mkTextContent(inserter, nod1); - XTextContent xContent2 = helper.mkTextContent(inserter, nod2); - - // attach to range from meta getStart()/getEnd() - xContent1.attach(xStart); - xContent2.attach(xEnd); - - helper.postInserted(nod1, xContent1); - helper.postInserted(nod2, xContent2); - - TreeNode root = new TreeNode() - .appendChild( new TextNode("12") ) - .appendChild( met1.dup() - .appendChild( nod1.dup() ) - .appendChild( new TextNode("AB") ) - .appendChild( nod2.dup() ) ) - .appendChild( new TextNode("6789") ); - doTest(root, false); - } - { - xText.setString("AB"); - xStart = xText.getStart(); - xEnd = xText.getEnd(); - - TreeNode nod1 = helper.mkTreeNode(); - TreeNode nod2 = helper.mkTreeNode(); - XTextContent xContent1 = helper.mkTextContent(inserter, nod1); - XTextContent xContent2 = helper.mkTextContent(inserter, nod2); - - XTextCursor xTextCursor = xText.createTextCursor(); - xTextCursor.gotoStart(false); - - // attach to cursor from meta XText - xContent1.attach(xTextCursor); - xTextCursor.gotoEnd(false); - xContent2.attach(xTextCursor); - - helper.postInserted(nod1, xContent1); - helper.postInserted(nod2, xContent2); - - TreeNode root = new TreeNode() - .appendChild( new TextNode("12") ) - .appendChild( met1.dup() - .appendChild( nod1.dup() ) - .appendChild( new TextNode("AB") ) - .appendChild( nod2.dup() ) ) - .appendChild( new TextNode("6789") ); - doTest(root, false); - } - } - - @Test public void testMetaFieldXTextField() throws Exception - { - com.sun.star.rdf.XRepositorySupplier xModel = - UnoRuntime.queryInterface( - com.sun.star.rdf.XRepositorySupplier.class, m_xDoc); - com.sun.star.rdf.XRepository xRepo = xModel.getRDFRepository(); - // for testing just add it to the first graph - com.sun.star.rdf.XURI[] Graphs = xRepo.getGraphNames(); - com.sun.star.rdf.XNamedGraph xGraph = xRepo.getGraph(Graphs[0]); - com.sun.star.rdf.XURI xOdfPrefix = - com.sun.star.rdf.URI.createKnown(m_xContext, - com.sun.star.rdf.URIs.ODF_PREFIX); - com.sun.star.rdf.XURI xOdfSuffix = - com.sun.star.rdf.URI.createKnown(m_xContext, - com.sun.star.rdf.URIs.ODF_SUFFIX); - com.sun.star.rdf.XNode xPrefix = - com.sun.star.rdf.Literal.create(m_xContext, "foo"); - com.sun.star.rdf.XNode xSuffix = - com.sun.star.rdf.Literal.create(m_xContext, "bar"); - - RangeInserter inserter = new RangeInserter(m_xDoc); - TreeNode text = new TextNode("abc"); - inserter.insertRange( new Range(0, 0, text) ); - XText xDocText = m_xDoc.getText(); - XTextCursor xDocTextCursor = xDocText.createTextCursor(); - xDocTextCursor.goRight((short)1, false); - xDocTextCursor.goRight((short)3, true); - - XTextField xMetaField = inserter.makeMetaField(); - - xDocText.insertTextContent(xDocTextCursor, xMetaField, true); - - XMetadatable xMetadatable = UnoRuntime.queryInterface(XMetadatable.class, xMetaField); - xMetadatable.ensureMetadataReference(); - - xGraph.addStatement(xMetadatable, xOdfPrefix, xPrefix); - xGraph.addStatement(xMetadatable, xOdfSuffix, xSuffix); - assertEquals("getPresentation(): wrong", - "fooabcbar", xMetaField.getPresentation(false)); - inserter.insertRange( new Range(0, 0, text) ); - } - - @Test public void testMetaFieldXPropertySet() throws Exception - { - RangeInserter inserter = new RangeInserter(m_xDoc); - TreeNode text = new TextNode("123"); - inserter.insertRange( new Range(0, 0, text) ); - XText xDocText = m_xDoc.getText(); - XTextCursor xDocTextCursor = xDocText.createTextCursor(); - xDocTextCursor.goRight((short)1, false); - xDocTextCursor.goRight((short)3, true); - - XTextField xMetaField = inserter.makeMetaField(); - - xDocText.insertTextContent(xDocTextCursor, xMetaField, true); - - XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xMetaField); - assertNotNull("PropertySet: not supported?", xPropertySet); - XPropertySetInfo xPropertySetInfo = xPropertySet.getPropertySetInfo(); - assertTrue("hasPropertyByName(\"NumberFormat\"):", - xPropertySetInfo.hasPropertyByName("NumberFormat")); - assertTrue("hasPropertyByName(\"IsFixedLanguage\"):", - xPropertySetInfo.hasPropertyByName("IsFixedLanguage")); - - int def = (Integer) xPropertySet.getPropertyValue("NumberFormat"); - System.out.println("NumberFormat: default is " + def); - short INT = com.sun.star.i18n.NumberFormatIndex.NUMBER_INT; - xPropertySet.setPropertyValue("NumberFormat", INT); - xPropertySet.setPropertyValue("IsFixedLanguage", true); - int format = (Integer) xPropertySet.getPropertyValue("NumberFormat"); - assertEquals("NumberFormat: failed", INT, format); - boolean isFixed = (Boolean) - xPropertySet.getPropertyValue("IsFixedLanguage"); - assertTrue("IsFixedLanguage: failed", isFixed); - } - - @Test public void testLoadStore() throws Exception - { - XTextDocument xComp = null; - String filename = "TESTMETA.odt"; - String file; - try { - file = TestDocument.getUrl(filename); - xComp = doLoad(file); - if (xComp != null) - { - checkLoadMeta(xComp); - file = m_TmpDir + filename; - doStore(xComp, file); - close(xComp); - xComp = doLoad(file); - checkLoadMeta(xComp); - } - } finally { - close(xComp); - } - } - - private void doStore(XComponent xComp, String file) throws Exception - { - System.out.println("Storing test document..."); - - XStorable xStor = UnoRuntime.queryInterface( - XStorable.class, xComp); - - xStor.storeToURL(file, new PropertyValue[0]); - - System.out.println("...done"); - } - - public XTextDocument doLoad(String file) throws Exception - { - XComponent xComp = null; - - System.out.println("Loading test document..."); - - PropertyValue[] loadProps = new PropertyValue[1]; - loadProps[0] = new PropertyValue(); - loadProps[0].Name = "Hidden"; - loadProps[0].Value = Boolean.TRUE; - - xComp = util.DesktopTools.loadDoc(m_xMSF, file, loadProps); - - XTextDocument xTextDoc = UnoRuntime.queryInterface(XTextDocument.class, xComp); - - assertNotNull("cannot load: " + file, xTextDoc); - - System.out.println("...done"); - - return xTextDoc; - } - - public void checkLoadMeta(XTextDocument xTextDoc) throws Exception - { - XText xText = xTextDoc.getText(); - - System.out.println("Checking meta(-field)s in loaded test document..."); - - TreeNode root = new TreeNode() - .appendChild( new RubyNode("ruby1") - .appendChild( new TextNode("1") ) ) - .appendChild( new MetaNode(mkId_("id1")) - .appendChild( new TextNode("2") ) ) - .appendChild( new MetaFieldNode(mkId_("id2")) - .appendChild( new TextNode("3") ) ) - .appendChild( new RubyNode("ruby2") - .appendChild( new MetaNode(mkId_("id3")) - .appendChild( new TextNode("4") ) ) ) - .appendChild( new RubyNode("ruby3") - .appendChild( new MetaFieldNode(mkId_("id4")) - .appendChild( new TextNode("5") ) ) ) - .appendChild( new MetaNode(mkId_("id5")) - .appendChild( new RubyNode("ruby4") - .appendChild( new TextNode("6") ) ) ) - .appendChild( new MetaFieldNode(mkId_("id6")) - .appendChild( new RubyNode("ruby5") - .appendChild( new TextNode("7") ) ) ) - .appendChild( new MetaNode(mkId_("id7")) - .appendChild( new MetaNode(mkId_("id8")) - .appendChild( new TextNode("8") ) ) ) - .appendChild( new MetaNode(mkId_("id9")) - .appendChild( new MetaFieldNode(mkId_("id10")) - .appendChild( new TextNode("9") ) ) ) - .appendChild( new MetaFieldNode(mkId_("id11")) - .appendChild( new MetaNode(mkId_("id12")) - .appendChild( new TextNode("10") ) ) ) - .appendChild( new MetaFieldNode(mkId_("id13")) - .appendChild( new MetaFieldNode(mkId_("id14")) - .appendChild( new TextNode("11") ) ) ) - .appendChild( new MetaNode(mkId_("id15")) - .appendChild( new RubyNode("ruby6") - .appendChild( new MetaFieldNode(mkId_("id16")) - .appendChild( new TextNode("12") ) ) ) ) - .appendChild( new MetaNode(mkId_("")) { - @Override - public boolean equals(Object other) { - return (other instanceof MetaNode); - } } - .appendChild( new TextNode("13") ) ) - .appendChild( new TextNode(" X X ") ); - doTest(xTextDoc, root, false); - - System.out.println("...done"); - } - - @Test public void testLoadStoreXmlid() throws Exception - { - XTextDocument xComp = null; - String filename = "TESTXMLID.odt"; - String file; - try { - file = TestDocument.getUrl(filename); - xComp = doLoad(file); - if (xComp != null) - { - checkLoadXmlId(xComp); - file = m_TmpDir + filename; - doStore(xComp, file); - close(xComp); - xComp = doLoad(file); - checkLoadXmlId(xComp); - } - } finally { - close(xComp); - } - } - - public void checkLoadXmlId(XTextDocument xTextDoc) throws Exception - { - XText xText = xTextDoc.getText(); - - System.out.println("Checking bookmarks in loaded test document..."); - - XRepositorySupplier xRS = UnoRuntime.queryInterface(XRepositorySupplier.class, xTextDoc); - XDocumentRepository xRepo = UnoRuntime.queryInterface(XDocumentRepository.class, - xRS.getRDFRepository()); - XBookmarksSupplier xBMS = UnoRuntime.queryInterface(XBookmarksSupplier.class, xTextDoc); - XNameAccess xBookmarks = xBMS.getBookmarks(); - XMetadatable xMark1 = UnoRuntime.queryInterface( - XMetadatable.class, xBookmarks.getByName("mk1")); - assertTrue("mark1", - eq(xMark1.getMetadataReference(), - new StringPair("content.xml", "id90"))); - - XMetadatable xMark2 = UnoRuntime.queryInterface( - XMetadatable.class, xBookmarks.getByName("mk2")); - Pair result = xRepo.getStatementRDFa(xMark2); - assertTrue("mark2", (result.First.length == 1) - && result.First[0].Subject.getStringValue().equals("uri:foo") - && result.First[0].Predicate.getStringValue().equals("uri:bar") - && result.First[0].Object.getStringValue().contains("a fooish bar") - ); - - XMetadatable xMark3 = UnoRuntime.queryInterface( - XMetadatable.class, xBookmarks.getByName("mk3")); - assertTrue("mark3", - eq(xMark3.getMetadataReference(), - new StringPair("content.xml", "id91"))); - - System.out.println("...done"); - - System.out.println("Checking sections in loaded test document..."); - - XTextSectionsSupplier xTSS = UnoRuntime.queryInterface(XTextSectionsSupplier.class, xTextDoc); - - XNameAccess xSections = xTSS.getTextSections(); - - XMetadatable xSection1 = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("Section 1")); - assertTrue("idsection1", eq(xSection1.getMetadataReference(), - new StringPair("content.xml", "idSection1"))); - - XMetadatable xSection2 = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("Section 2")); - assertTrue("idSection2", eq(xSection2.getMetadataReference(), - new StringPair("content.xml", "idSection2"))); - - XMetadatable xSection3 = UnoRuntime.queryInterface( - XMetadatable.class, - xSections.getByName("Table of Contents1_Head")); - assertTrue("idTOCTitle", eq(xSection3.getMetadataReference(), - new StringPair("content.xml", "idTOCTitle"))); - - XMetadatable xSection4 = UnoRuntime.queryInterface( - XMetadatable.class, - xSections.getByName("Alphabetical Index1_Head")); - assertTrue("idAITitle", eq(xSection4.getMetadataReference(), - new StringPair("content.xml", "idAITitle"))); - - XMetadatable xSection5 = UnoRuntime.queryInterface( - XMetadatable.class, - xSections.getByName("Illustration Index1_Head")); - assertTrue("idIITitle", eq(xSection5.getMetadataReference(), - new StringPair("content.xml", "idIITitle"))); - - XMetadatable xSection6 = UnoRuntime.queryInterface( - XMetadatable.class, - xSections.getByName("Index of Tables1_Head")); - assertTrue("idIOTTitle", eq(xSection6.getMetadataReference(), - new StringPair("content.xml", "idIOTTitle"))); - - XMetadatable xSection7 = UnoRuntime.queryInterface( - XMetadatable.class, - xSections.getByName("User-Defined1_Head")); - assertTrue("idUDTitle", eq(xSection7.getMetadataReference(), - new StringPair("content.xml", "idUDTitle"))); - - XMetadatable xSection8 = UnoRuntime.queryInterface( - XMetadatable.class, - xSections.getByName("Table of Objects1_Head")); - assertTrue("idTOOTitle", eq(xSection8.getMetadataReference(), - new StringPair("content.xml", "idTOOTitle"))); - - XMetadatable xSection9 = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("Bibliography1_Head")); - assertTrue("idBibTitle", eq(xSection9.getMetadataReference(), - new StringPair("content.xml", "idBibTitle"))); - - System.out.println("...done"); - - System.out.println("Checking indexes in loaded test document..."); - - XDocumentIndexesSupplier xDIS = UnoRuntime.queryInterface(XDocumentIndexesSupplier.class, xTextDoc); - XIndexAccess xIndexesIA = xDIS.getDocumentIndexes(); - XNameAccess xIndexes = - UnoRuntime.queryInterface(XNameAccess.class, xIndexesIA); - - XMetadatable xIndex1 = UnoRuntime.queryInterface( - XMetadatable.class, xIndexes.getByName("Table of Contents1")); - assertTrue("idTOC", eq(xIndex1.getMetadataReference(), - new StringPair("content.xml", "idTOC"))); - XMetadatable xIndex1s = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("Table of Contents1")); - assertTrue("idTOC", eq(xIndex1s.getMetadataReference(), - new StringPair("content.xml", "idTOC"))); - - XMetadatable xIndex2 = UnoRuntime.queryInterface( - XMetadatable.class, xIndexes.getByName("Alphabetical Index1")); - assertTrue("idAI", eq(xIndex2.getMetadataReference(), - new StringPair("content.xml", "idAI"))); - XMetadatable xIndex2s = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("Alphabetical Index1")); - assertTrue("idAI", eq(xIndex2s.getMetadataReference(), - new StringPair("content.xml", "idAI"))); - - XMetadatable xIndex3 = UnoRuntime.queryInterface( - XMetadatable.class, xIndexes.getByName("Illustration Index1")); - assertTrue("idII", eq(xIndex3.getMetadataReference(), - new StringPair("content.xml", "idII"))); - XMetadatable xIndex3s = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("Illustration Index1")); - assertTrue("idII", eq(xIndex3s.getMetadataReference(), - new StringPair("content.xml", "idII"))); - - XMetadatable xIndex4 = UnoRuntime.queryInterface( - XMetadatable.class, xIndexes.getByName("Index of Tables1")); - assertTrue("idIOT", eq(xIndex4.getMetadataReference(), - new StringPair("content.xml", "idIOT"))); - XMetadatable xIndex4s = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("Index of Tables1")); - assertTrue("idIOT", eq(xIndex4s.getMetadataReference(), - new StringPair("content.xml", "idIOT"))); - - XMetadatable xIndex5 = UnoRuntime.queryInterface( - XMetadatable.class, xIndexes.getByName("User-Defined1")); - assertTrue("idUD", eq(xIndex5.getMetadataReference(), - new StringPair("content.xml", "idUD"))); - XMetadatable xIndex5s = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("User-Defined1")); - assertTrue("idUD", eq(xIndex5s.getMetadataReference(), - new StringPair("content.xml", "idUD"))); - - XMetadatable xIndex6 = UnoRuntime.queryInterface( - XMetadatable.class, xIndexes.getByName("Table of Objects1")); - assertTrue("idTOO", eq(xIndex6.getMetadataReference(), - new StringPair("content.xml", "idTOO"))); - XMetadatable xIndex6s = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("Table of Objects1")); - assertTrue("idTOO", eq(xIndex6s.getMetadataReference(), - new StringPair("content.xml", "idTOO"))); - - XMetadatable xIndex7 = UnoRuntime.queryInterface( - XMetadatable.class, xIndexes.getByName("Bibliography1")); - assertTrue("idBib", eq(xIndex7.getMetadataReference(), - new StringPair("content.xml", "idBib"))); - XMetadatable xIndex7s = UnoRuntime.queryInterface( - XMetadatable.class, xSections.getByName("Bibliography1")); - assertTrue("idBib", eq(xIndex7s.getMetadataReference(), - new StringPair("content.xml", "idBib"))); - - System.out.println("...done"); - } - - static void close(XComponent i_comp) - { - try { - XCloseable xClos = UnoRuntime.queryInterface( - XCloseable.class, i_comp); - if (xClos != null) xClos.close(true); - } catch (Exception e) { - } - } - - private void doTest(TreeNode intree) throws Exception - { - doTest(m_xDoc, intree, true); - } - - private void doTest(TreeNode intree, boolean insert) throws Exception - { - doTest(m_xDoc, intree, insert); - } - - private void doTest(XTextDocument xDoc, TreeNode intree, - boolean insert) throws Exception - { - dumpTree(intree, "I: "); - - if (insert) { - new TreeInserter(xDoc).insertTree(intree); - } - - XText xText = xDoc.getText(); - XEnumerationAccess xTextEA = UnoRuntime.queryInterface(XEnumerationAccess.class, xText); - XEnumeration xTextEnum = xTextEA.createEnumeration(); - // skip to right paragraph - xTextEnum.nextElement(); // skip first -- always empty! - Object xElement = xTextEnum.nextElement(); // second contains test case - XEnumerationAccess xEA = UnoRuntime.queryInterface(XEnumerationAccess.class, xElement); - XEnumeration xEnum = xEA.createEnumeration(); - TreeNode outtree = new EnumConverter().convert(xEnum); - - dumpTree(outtree, "O: "); - - new FuzzyTester().doTest(intree, outtree); - } - - private void dumpTree(TreeNode tree, String prefix) - { - System.out.println(prefix + tree.toString()); - TreeNodeEnum children = tree.createEnumeration(); - while (children.hasNext()) { - TreeNode node = children.next(); - dumpTree(node, prefix + " "); - } - } - - private String mkName(String prefix) - { - return prefix + (m_Count++); - } - - private StringPair mkId(String prefix) - { - return new StringPair("content.xml", mkName(prefix)); - } - - private StringPair mkId_(String id) - { - return new StringPair("content.xml", id); - } - - static boolean eq(StringPair i_Left, StringPair i_Right) - { - return (i_Left.First.equals(i_Right.First)) && - (i_Left.Second.equals(i_Right.Second)); - } - - @BeforeClass public static void setUpConnection() throws Exception { - connection.setUp(); - } - - @AfterClass public static void tearDownConnection() - throws InterruptedException, com.sun.star.uno.Exception - { - connection.tearDown(); - } - - private static final OfficeConnection connection = new OfficeConnection(); -} - diff --git a/sw/qa/python/text_portion_enumeration_test.py b/sw/qa/python/text_portion_enumeration_test.py index 857eeee78b68..cc7a28e785ea 100644 --- a/sw/qa/python/text_portion_enumeration_test.py +++ b/sw/qa/python/text_portion_enumeration_test.py @@ -480,7 +480,6 @@ class Inserter(): def insertruby(self, xCursor, rubytext): xCursor.RubyText = rubytext - def insertmeta(self, xCursor, xmlid): xContent = self.makemeta() xContent.attach(xCursor) @@ -2903,6 +2902,547 @@ class TextPortionEnumerationTest(unittest.TestCase): self.assertEqual("Text", string, "gotoEndOfWord(): wrong string") self.assertNotEqual("a","b") + class AttachHelper(): + def isattribute(self): pass + def mktreenode(self): pass + def mktextcontent(self, inserter, node): pass + def postinserted(self, node, xContent): pass + + def test_meta_xtextattach_toxmark(self): + class Helper(self.AttachHelper): + def isattribute(_): + return True + def mktreenode(_): + return DocumentIndexMarkNode(self.mkname("toxmark")) + def mktextcontent(_, inserter, node): + return inserter.makedocumentindexmark(node.name) + self.do_meta_xtextattach(Helper()) + + def test_meta_xtextattach_refmark(self): + class Helper(self.AttachHelper): + def isattribute(_): + return True + def mktreenode(_): + return ReferenceMarkNode(self.mkname("refmark")) + def mktextcontent(_, inserter, node): + return inserter.makereferencemark(node.name) + self.do_meta_xtextattach(Helper()) + + def test_meta_xtextattach_textfield(self): + class Helper(self.AttachHelper): + def isattribute(_): + return False + def mktreenode(_): + return TextFieldNode(self.mkname("field")) + def mktextcontent(_, inserter, node): + return inserter.maketextfield(node.content) + self.do_meta_xtextattach(Helper()) + + def test_meta_xtextattach_footnote(self): + class Helper(self.AttachHelper): + def isattribute(_): + return False + def mktreenode(_): + return FootnoteNode(self.mkname("ftn")) + def mktextcontent(_, inserter, node): + return inserter.makefootnote(node.label) + self.do_meta_xtextattach(Helper()) + + def test_meta_xtextattach_meta(self): + class Helper(self.AttachHelper): + def isattribute(_): + return True + def mktreenode(_): + return MetaNode(self.mkid("id")) + def mktextcontent(_, inserter, node): + return inserter.makemeta() + def postinserted(_, node, xContent): + xContent.MetadataReference = node.xmlid + self.do_meta_xtextattach(Helper()) + + def do_meta_xtextattach(self, helper): + xDoc = self.__class__.xDoc + inserter = RangeInserter(xDoc) + text = TextNode("12AB6789") + inserter.insertrange(Range(0, 0, text)) + met1 = MetaNode(self.mkid("id")) + xMeta = inserter.makemeta() + + xDocText = xDoc.getText() + xDocTextCursor = xDocText.createTextCursor() + xDocTextCursor.goRight(3, False) + xDocTextCursor.goRight(2, True) + xDocText.insertTextContent(xDocTextCursor, xMeta, True) + + xMeta.MetadataReference = met1.xmlid + xStart = None + xEnd = None + + xStart = xMeta.getStart() + xEnd = xMeta.getEnd() + + nod1 = helper.mktreenode() + nod2 = helper.mktreenode() + xContent1 = helper.mktextcontent(inserter, nod1) + xContent2 = helper.mktextcontent(inserter, nod2) + + ## insertTextContent with meta getStart()/getEnd() + xMeta.insertTextContent(xStart, xContent1, False) + xMeta.insertTextContent(xEnd, xContent2, False) + + helper.postinserted(nod1, xContent1) + helper.postinserted(nod2, xContent2) + + root = TreeNode() + root.appendchild(TextNode("12")) + root.appendchild(met1.dup() + .appendchild(nod1.dup()) + .appendchild(TextNode("AB")) + .appendchild(nod2.dup())) + root.appendchild(TextNode("6789")) + self.dotest(root, False) + + xMeta.setString("AB") + xStart = xMeta.getStart() + xEnd = xMeta.getEnd() + + nod1 = helper.mktreenode() + nod2 = helper.mktreenode() + xContent1 = helper.mktextcontent(inserter, nod1) + xContent2 = helper.mktextcontent(inserter, nod2) + + xTextCursor = xMeta.createTextCursor() + xTextCursor.gotoStart(False) + + ## insertTextContent with meta cursor + xMeta.insertTextContent(xTextCursor, xContent1, False) + xTextCursor.gotoEnd(False) + xMeta.insertTextContent(xTextCursor, xContent2, False) + + helper.postinserted(nod1, xContent1) + helper.postinserted(nod2, xContent2) + + root = TreeNode() + root.appendchild(TextNode("12")) + root.appendchild(met1.dup() + .appendchild(nod1.dup()) + .appendchild(TextNode("AB")) + .appendchild(nod2.dup())) + root.appendchild(TextNode("6789")) + self.dotest(root, False) + + if not helper.isattribute(): + # xMeta.setString("AB") + xStart = xMeta.getStart() + xEnd = xMeta.getEnd() + + nod1 = helper.mktreenode() + nod2 = helper.mktreenode() + xContent1 = helper.mktextcontent(inserter, nod1) + xContent2 = helper.mktextcontent(inserter, nod2) + + xTextCursor = xMeta.createTextCursor() + xTextCursor.gotoStart(False) + xTextCursor.goRight(1, True) + + ## insertTextContent with meta cursor and absorb + xMeta.insertTextContent(xTextCursor, xContent1, True) + xTextCursor.gotoEnd(False) + xTextCursor.goLeft(1, True) + xMeta.insertTextContent(xTextCursor, xContent2, True) + + helper.postinserted(nod1, xContent1) + helper.postinserted(nod2, xContent2) + + root = TreeNode() + root.appendchild(TextNode("12")) + root.appendchild(met1.dup() + .appendchild(nod1.dup()) + .appendchild(TextNode("AB")) + .appendchild(nod2.dup())) + root.appendchild(TextNode("6789")) + self.dotest(root, False) + + xMeta.setString("AB") + xStart = xMeta.getStart() + xEnd = xMeta.getEnd() + + nod1 = helper.mktreenode() + nod2 = helper.mktreenode() + xContent1 = helper.mktextcontent(inserter, nod1) + xContent2 = helper.mktextcontent(inserter, nod2) + + xDocTextCursor.gotoRange(xStart, False) + + ## insertTextContent with document cursor + xMeta.insertTextContent(xDocTextCursor, xContent1, False) + xDocTextCursor.gotoRange(xEnd, False) + xMeta.insertTextContent(xDocTextCursor, xContent2, False) + + helper.postinserted(nod1, xContent1) + helper.postinserted(nod2, xContent2) + + root = TreeNode() + root.appendchild(TextNode("12")) + root.appendchild(met1.dup() + .appendchild(nod1.dup()) + .appendchild(TextNode("AB")) + .appendchild(nod2.dup())) + root.appendchild(TextNode("6789")) + self.dotest(root, False) + + if not helper.isattribute(): + xStart = xMeta.getStart() + xEnd = xMeta.getEnd() + + nod1 = helper.mktreenode() + nod2 = helper.mktreenode() + xContent1 = helper.mktextcontent(inserter, nod1) + xContent2 = helper.mktextcontent(inserter, nod2) + + xDocTextCursor.gotoRange(xStart, False) + xDocTextCursor.goRight(1, True) + + ## insertTextContent with document cursor and absorb + xMeta.insertTextContent(xDocTextCursor, xContent1, True) + xDocTextCursor.gotoRange(xEnd, False) + xDocTextCursor.goLeft(1, True) + xMeta.insertTextContent(xDocTextCursor, xContent2, True) + + helper.postinserted(nod1, xContent1) + helper.postinserted(nod2, xContent2) + + root = TreeNode() + root.appendchild(TextNode("12")) + root.appendchild(met1.dup() + .appendchild(nod1.dup()) + .appendchild(TextNode("AB")) + .appendchild(nod2.dup())) + root.appendchild(TextNode("6789")) + self.dotest(root, False) + + xMeta.setString("AB") + xStart = xMeta.getStart() + xEnd = xMeta.getEnd() + + nod1 = helper.mktreenode() + nod2 = helper.mktreenode() + xContent1 = helper.mktextcontent(inserter, nod1) + xContent2 = helper.mktextcontent(inserter, nod2) + + ## attach to range from meta getStart()/getEnd() + xContent1.attach(xStart) + xContent2.attach(xEnd) + + helper.postinserted(nod1, xContent1) + helper.postinserted(nod2, xContent2) + + root = TreeNode() + root.appendchild(TextNode("12")) + root.appendchild(met1.dup() + .appendchild(nod1.dup()) + .appendchild(TextNode("AB")) + .appendchild(nod2.dup())) + root.appendchild(TextNode("6789")) + self.dotest(root, False) + + xMeta.setString("AB") + xStart = xMeta.getStart() + xEnd = xMeta.getEnd() + + nod1 = helper.mktreenode() + nod2 = helper.mktreenode() + xContent1 = helper.mktextcontent(inserter, nod1) + xContent2 = helper.mktextcontent(inserter, nod2) + + xTextCursor = xMeta.createTextCursor() + xTextCursor.gotoStart(False) + + ## attach to cursor from meta XText + xContent1.attach(xTextCursor) + xTextCursor.gotoEnd(False) + xContent2.attach(xTextCursor) + + helper.postinserted(nod1, xContent1) + helper.postinserted(nod2, xContent2) + + root = TreeNode() + root.appendchild(TextNode("12")) + root.appendchild(met1.dup() + .appendchild(nod1.dup()) + .appendchild(TextNode("AB")) + .appendchild(nod2.dup())) + root.appendchild(TextNode("6789")) + self.dotest(root, False) + + def test_metafield_xtextfield(self): + xDoc = self.__class__.xDoc + smgr = self.__class__._uno.xContext.ServiceManager + xRepo = xDoc.getRDFRepository() + ## for testing just add it to the first graph + Graphs = xRepo.getGraphNames() + xGraph = xRepo.getGraph(Graphs[0]) + xOdfPrefix = smgr.createInstance("com.sun.star.rdf.URI") + xOdfPrefix.initialize((ODF_PREFIX,)) + xOdfSuffix = smgr.createInstance("com.sun.star.rdf.URI") + xOdfSuffix.initialize((ODF_SUFFIX,)) + + xPrefix = smgr.createInstance("com.sun.star.rdf.Literal") + xPrefix.initialize(("foo",)) + xSuffix = smgr.createInstance("com.sun.star.rdf.Literal") + xSuffix.initialize(("bar",)) + + inserter = RangeInserter(xDoc) + text = TextNode("abc") + inserter.insertrange(Range(0, 0, text)) + xDocText = xDoc.getText() + xDocTextCursor = xDocText.createTextCursor() + xDocTextCursor.goRight(1, False) + xDocTextCursor.goRight(3, True) + + xMetaField = inserter.makemetafield() + + xDocText.insertTextContent(xDocTextCursor, xMetaField, True) + + xMetaField.ensureMetadataReference + + xGraph.addStatement(xMetaField, xOdfPrefix, xPrefix) + xGraph.addStatement(xMetaField, xOdfSuffix, xSuffix) + self.assertEqual("fooabcbar", xMetaField.getPresentation(False), + "getPresentation(): wrong") + inserter.insertrange(Range(0, 0, text)) + + def test_metafield_xpropertyset(self): + xDoc = self.__class__.xDoc + inserter = RangeInserter(xDoc) + text = TextNode("123") + inserter.insertrange(Range(0, 0, text)) + xDocText = xDoc.getText() + xDocTextCursor = xDocText.createTextCursor() + xDocTextCursor.goRight(1, False) + xDocTextCursor.goRight(3, True) + + xMetaField = inserter.makemetafield() + + xDocText.insertTextContent(xDocTextCursor, xMetaField, True) + + self.assertIsNotNone(xMetaField, "PropertySet: not supported?") + xPropertySetInfo = xMetaField.getPropertySetInfo() + self.assertTrue(xPropertySetInfo.hasPropertyByName("NumberFormat"), + 'hasPropertyByName("NumberFormat"):') + self.assertTrue(xPropertySetInfo.hasPropertyByName("IsFixedLanguage"), + 'hasPropertyByName("IsFixedLanguage"):') + + def_ = xMetaField.NumberFormat + print("NumberFormat: default is {}".format(def_)) + xMetaField.NumberFormat = NUMBER_INT + xMetaField.IsFixedLanguage = True + format = xMetaField.NumberFormat + self.assertEqual(NUMBER_INT, format, "NumberFormat: failed") + isfixed = xMetaField.IsFixedLanguage + self.assertTrue(isfixed, "IsFixedLanguage: failed") + + def dostore(self, xComp, file): + print("Storing test document...") + file = uno.systemPathToFileUrl(file) + xComp.storeToURL(file, ()) + print("...done") + + def doload(self, file): + xComp = None + print("Loading test document...") + xComp = self.__class__._uno.openDoc(file) + self.assertIsNotNone(xComp, "cannot load: {}".format(file)) + print("...done") + return xComp + + def close(self, i_comp): + try: + if i_comp: + i_comp.close(True) + except Exception as e: + pass + + def test_load_store(self): + xComp = None + filename = "TESTMETA.odt" + try: + xComp = self.__class__._uno.openWriterTemplateDoc(filename) + if xComp: + self.checkloadmeta(xComp) + with TemporaryDirectory() as tempdir: + file = os.path.join(tempdir, filename) + self.dostore(xComp, file) + self.close(xComp) + xComp = self.doload(file) + self.checkloadmeta(xComp) + finally: + self.close(xComp) + + def checkloadmeta(self, xTextDoc): + xText = xTextDoc.getText() + print("Checking meta(-field)s in loaded test document...") + root = TreeNode() + root.appendchild(RubyNode("ruby1") + .appendchild(TextNode("1"))) + root.appendchild(MetaNode(self.mkid_("id1")) + .appendchild(TextNode("2"))) + root.appendchild(MetaFieldNode(self.mkid_("id2")) + .appendchild(TextNode("3"))) + root.appendchild(RubyNode("ruby2") + .appendchild(MetaNode(self.mkid_("id3")) + .appendchild(TextNode("4")))) + root.appendchild(RubyNode("ruby3") + .appendchild(MetaFieldNode(self.mkid_("id4")) + .appendchild(TextNode("5")))) + root.appendchild(MetaNode(self.mkid_("id5")) + .appendchild(RubyNode("ruby4") + .appendchild(TextNode("6")))) + root.appendchild(MetaFieldNode(self.mkid_("id6")) + .appendchild(RubyNode("ruby5") + .appendchild(TextNode("7")))) + root.appendchild(MetaNode(self.mkid_("id7")) + .appendchild(MetaNode(self.mkid_("id8")) + .appendchild(TextNode("8")))) + root.appendchild(MetaNode(self.mkid_("id9")) + .appendchild(MetaFieldNode(self.mkid_("id10")) + .appendchild(TextNode("9")))) + root.appendchild(MetaFieldNode(self.mkid_("id11")) + .appendchild(MetaNode(self.mkid_("id12")) + .appendchild(TextNode("10")))) + root.appendchild(MetaFieldNode(self.mkid_("id13")) + .appendchild(MetaFieldNode(self.mkid_("id14")) + .appendchild(TextNode("11")))) + root.appendchild(MetaNode(self.mkid_("id15")) + .appendchild(RubyNode("ruby6") + .appendchild(MetaFieldNode(self.mkid_("id16")) + .appendchild(TextNode("12"))))) + + class MetaNode_(MetaNode): + def __init__(self, id): + super().__init__(id) + def __eq__(self, other): + return isinstance(other, MetaNode) + root.appendchild(MetaNode_(self.mkid_("")) + .appendchild(TextNode("13"))) + root.appendchild(TextNode(" X X ")) + self._dotest(xTextDoc, root, False) + print("...done") + + def test_load_store_xmlid(self): + xComp = None + filename = "TESTXMLID.odt" + try: + xComp = self.__class__._uno.openWriterTemplateDoc(filename) + if xComp: + self.checkloadxmlid(xComp) + with TemporaryDirectory() as tempdir: + file = os.path.join(tempdir, filename) + self.dostore(xComp, file) + self.close(xComp) + xComp = self.doload(file) + self.checkloadxmlid(xComp) + finally: + self.close(xComp) + + def checkloadxmlid(self, xTextDoc): + xText = xTextDoc.getText() + xRepo = xTextDoc.getRDFRepository() + + print("Checking bookmarks in loaded test document...") + xBookmarks = xTextDoc.getBookmarks() + xMark1 = xBookmarks["mk1"] + self.assertTrue(self.eq(xMark1.MetadataReference, + StringPair("content.xml", "id90")), "mark1") + xMark2 = xBookmarks["mk2"] + result = xRepo.getStatementRDFa(xMark2) + self.assertTrue(len(result.First) == 1 and + result.First[0].Subject.StringValue == "uri:foo" and + result.First[0].Predicate.StringValue == "uri:bar" and + result.First[0].Object.Value == "a fooish bar", + "mark2") + xMark3 = xBookmarks["mk3"] + self.assertTrue(self.eq(xMark3.MetadataReference, + StringPair("content.xml", "id91")), "mark3") + print("...done") + + print("Checking sections in loaded test document...") + xSections = xTextDoc.getTextSections() + xSection1 = xSections["Section 1"] + self.assertTrue(self.eq(xSection1.MetadataReference, + StringPair("content.xml", "idSection1")), "idsection1") + xSection2 = xSections["Section 2"] + self.assertTrue(self.eq(xSection2.MetadataReference, + StringPair("content.xml", "idSection2")),"idSection2") + xSection3 = xSections["Table of Contents1_Head"] + self.assertTrue(self.eq(xSection3.MetadataReference, + StringPair("content.xml", "idTOCTitle")), "idTOCTitle") + xSection4 = xSections["Alphabetical Index1_Head"] + self.assertTrue(self.eq(xSection4.MetadataReference, + StringPair("content.xml", "idAITitle")), "idAITitle") + xSection5 = xSections["Illustration Index1_Head"] + self.assertTrue(self.eq(xSection5.MetadataReference, + StringPair("content.xml", "idIITitle")), "idIITitle") + xSection6 = xSections["Index of Tables1_Head"] + self.assertTrue(self.eq(xSection6.MetadataReference, + StringPair("content.xml", "idIOTTitle")), "idIOTTitle") + xSection7 = xSections["User-Defined1_Head"] + self.assertTrue(self.eq(xSection7.MetadataReference, + StringPair("content.xml", "idUDTitle")), "idUDTitle") + xSection8 = xSections["Table of Objects1_Head"] + self.assertTrue(self.eq(xSection8.MetadataReference, + StringPair("content.xml", "idTOOTitle")), "idTOOTitle") + xSection9 = xSections["Bibliography1_Head"] + self.assertTrue(self.eq(xSection9.MetadataReference, + StringPair("content.xml", "idBibTitle")), "idBibTitle") + print("...done") + + print("Checking indexes in loaded test document...") + xIndexes = xTextDoc.getDocumentIndexes() + xIndex1 = xIndexes["Table of Contents1"] + self.assertTrue(self.eq(xIndex1.MetadataReference, + StringPair("content.xml", "idTOC")), "idTOC") + xIndex1s = xSections["Table of Contents1"] + self.assertTrue(self.eq(xIndex1s.MetadataReference, + StringPair("content.xml", "idTOC")), "idTOC") + xIndex2 = xIndexes["Alphabetical Index1"] + self.assertTrue(self.eq(xIndex2.MetadataReference, + StringPair("content.xml", "idAI")), "idAI") + xIndex2s = xSections["Alphabetical Index1"] + self.assertTrue(self.eq(xIndex2s.MetadataReference, + StringPair("content.xml", "idAI")), "idAI") + xIndex3 = xIndexes["Illustration Index1"] + self.assertTrue(self.eq(xIndex3.MetadataReference, + StringPair("content.xml", "idII")), "idII") + xIndex3s = xSections["Illustration Index1"] + self.assertTrue(self.eq(xIndex3s.MetadataReference, + StringPair("content.xml", "idII")), "idII") + xIndex4 = xIndexes["Index of Tables1"] + self.assertTrue(self.eq(xIndex4.MetadataReference, + StringPair("content.xml", "idIOT")), "idIOT") + xIndex4s = xSections["Index of Tables1"] + self.assertTrue(self.eq(xIndex4s.MetadataReference, + StringPair("content.xml", "idIOT")), "idIOT") + xIndex5 = xIndexes["User-Defined1"] + self.assertTrue(self.eq(xIndex5.MetadataReference, + StringPair("content.xml", "idUD")), "idUD") + xIndex5s = xSections["User-Defined1"] + self.assertTrue(self.eq(xIndex5s.MetadataReference, + StringPair("content.xml", "idUD")), "idUD") + xIndex6 = xIndexes["Table of Objects1"] + self.assertTrue(self.eq(xIndex6.MetadataReference, + StringPair("content.xml", "idTOO")), "idTOO") + xIndex6s = xSections["Table of Objects1"] + self.assertTrue(self.eq(xIndex6s.MetadataReference, + StringPair("content.xml", "idTOO")), "idTOO") + xIndex7 = xIndexes["Bibliography1"] + self.assertTrue(self.eq(xIndex7.MetadataReference, + StringPair("content.xml", "idBib")), "idBib") + xIndex7s = xSections["Bibliography1"] + self.assertTrue(self.eq(xIndex7s.MetadataReference, + StringPair("content.xml", "idBib")), "idBib") + print("...done") + def dotest(self, intree, insert=True): xDoc = self.__class__.xDoc self._dotest(xDoc, intree, insert) -- cgit