incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liud...@apache.org
Subject svn commit: r1443861 - in /incubator/odf/trunk/simple/src: main/java/org/odftoolkit/simple/ main/java/org/odftoolkit/simple/common/navigation/ main/java/org/odftoolkit/simple/style/ main/java/org/odftoolkit/simple/table/ test/java/org/odftoolkit/simple...
Date Fri, 08 Feb 2013 07:44:25 GMT
Author: liudali
Date: Fri Feb  8 07:44:25 2013
New Revision: 1443861

URL: http://svn.apache.org/r1443861
Log:
fix for ODFToolkit-351, patched by KeJiaYe

Added:
    incubator/odf/trunk/simple/src/test/resources/TestReplaceWithBasicCases.odt   (with props)
    incubator/odf/trunk/simple/src/test/resources/TestTextSelectionCopySource.odt   (with props)
Modified:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/CellSelection.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ImageSelection.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ParagraphSelection.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/Selection.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TableSelection.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextDocumentSelection.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextNavigation.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextSelection.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextStyleNavigation.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/DefaultStyleHandler.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/ParagraphProperties.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/common/navigation/TextSelectionTest.java

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java Fri Feb  8 07:44:25 2013
@@ -63,6 +63,7 @@ import org.odftoolkit.odfdom.dom.element
 import org.odftoolkit.odfdom.dom.element.draw.DrawPageElement;
 import org.odftoolkit.odfdom.dom.element.office.OfficeBodyElement;
 import org.odftoolkit.odfdom.dom.element.office.OfficeMasterStylesElement;
+import org.odftoolkit.odfdom.dom.element.style.StyleFontFaceElement;
 import org.odftoolkit.odfdom.dom.element.table.TableTableCellElement;
 import org.odftoolkit.odfdom.dom.element.table.TableTableCellElementBase;
 import org.odftoolkit.odfdom.dom.element.table.TableTableTemplateElement;
@@ -2161,12 +2162,14 @@ public abstract class Document extends O
 								}
 							}
 							boolean hasLoopStyleDef = true;
+							if (!(styleElement instanceof StyleFontFaceElement)) {
 							if (copyStyleEleList.get(styleElement) == null) {
 								List<OdfElement> styleRefEleList = new ArrayList<OdfElement>();
 								copyStyleEleList.put(styleElement, styleRefEleList);
 								hasLoopStyleDef = false;
 							}
 							copyStyleEleList.get(styleElement).add(styleRefElement);
+							}
 
 							OdfElement cloneStyleElement = appendStyleList.get(styleElement);
 							if (cloneStyleElement == null) {
@@ -2192,10 +2195,12 @@ public abstract class Document extends O
 	private void appendForeignStyleElement(OdfElement cloneStyleEle, OdfFileDom dom, String styleElePath) {
 		StringTokenizer token = new StringTokenizer(styleElePath, "/");
 		boolean isExist = true;
+		boolean found = false;
 		Node iterNode = dom.getFirstChild();
 		Node parentNode = dom;
 		while (token.hasMoreTokens()) {
 			String onePath = token.nextToken();
+			found = false;
 
 			while ((iterNode != null) && isExist) {
 				String path = iterNode.getNamespaceURI();
@@ -2213,12 +2218,13 @@ public abstract class Document extends O
 					// found, then get the child nodes to find the next path
 					// node
 					parentNode = iterNode;
+					found = true;
 					iterNode = iterNode.getFirstChild();
 					break;
 				}
 			}
 
-			if (iterNode == null) {
+			if (!found) {
 				// should new the element since the current path node
 				if (isExist) {
 					isExist = false;

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java Fri Feb  8 07:44:25 2013
@@ -1391,10 +1391,12 @@ public class TextDocument extends Docume
 			 
 			OdfElement newOdfElement = (OdfElement) sourceOdfElement.cloneNode(true);
 			
-			if (isForeignNode)
-				copyForeignStyleRef(sourceOdfElement,sourceDocument);
-			if (isForeignNode) // not in a same document
-				newOdfElement = (OdfElement)  cloneForeignElement(newOdfElement, getContentDom(), true);
+			if (isForeignNode) {
+				copyForeignStyleRef(newOdfElement, sourceDocument);
+				copyLinkedRef(newOdfElement);
+				newOdfElement = (OdfElement) cloneForeignElement(newOdfElement,
+						getContentDom(), true);
+			}
 			
 			if (before) {
 				referenceOdfElement.getParentNode().insertBefore(newOdfElement, referenceOdfElement);

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/CellSelection.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/CellSelection.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/CellSelection.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/CellSelection.java Fri Feb  8 07:44:25 2013
@@ -51,8 +51,9 @@ public class CellSelection extends TextS
 	 * @param cell
 	 *            the table cell which is selected
 	 */
-	CellSelection(String text, OdfElement containerElement, int index, Cell cell) {
-		super(text, containerElement, index);
+	CellSelection(Navigation search, String text, OdfElement containerElement,
+			int index, Cell cell) {
+		super(search, text, containerElement, index);
 		mCell = cell;
 	}
 

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ImageSelection.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ImageSelection.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ImageSelection.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ImageSelection.java Fri Feb  8 07:44:25 2013
@@ -60,12 +60,18 @@ public class ImageSelection extends Sele
 	 */
 	public Image replaceWithImage(Image image) {
 		Image mImage =null;
+		if(search instanceof TextNavigation){
+			TextNavigation textSearch = (TextNavigation) search;
 		int leftLength = textSelection.getText().length();
 		int index = textSelection.getIndex();
 		mIsInserted = false;
 		OdfElement parentElement = textSelection.getContainerElement();
 		OdfFileDom ownerDom = (OdfFileDom) parentElement.getOwnerDocument();
 		
+			int nodeLength = TextExtractor.getText(
+					this.textSelection.getContainerElement()).length();
+			if (this.textSelection.getText().length() != nodeLength)
+				this.textSelection.setSelectionReplaced(true);
 		try {
 			if (imageContainer == null) {
 				delete(index, leftLength, parentElement);
@@ -100,6 +106,7 @@ public class ImageSelection extends Sele
 		} catch (Exception e) {
 			Logger.getLogger(ImageSelection.class.getName()).log(Level.SEVERE, e.getMessage(), e);
 		}
+		}
 		return mImage;
 	}	
 	/**
@@ -112,11 +119,17 @@ public class ImageSelection extends Sele
 	 */
 	public Image replaceWithImage(URI imageUri) {
 		Image mImage =null;
+		if(search instanceof TextNavigation){
+			TextNavigation textSearch = (TextNavigation) search;
 		int leftLength = textSelection.getText().length();
 		int index = textSelection.getIndex();
 		mIsInserted = false;
 		OdfElement parentElement = textSelection.getContainerElement();
 		OdfFileDom ownerDom = (OdfFileDom) parentElement.getOwnerDocument();
+			int nodeLength = TextExtractor.getText(
+					this.textSelection.getContainerElement()).length();
+			if (this.textSelection.getText().length() != nodeLength)
+				this.textSelection.setSelectionReplaced(true);
 		try{
 			if (imageContainer == null) {
 				delete(index, leftLength, parentElement);
@@ -149,6 +162,7 @@ public class ImageSelection extends Sele
 			
 		} catch (Exception e) {
 			Logger.getLogger(ImageSelection.class.getName()).log(Level.SEVERE, e.getMessage(), e);
+			}
 		}
 		return mImage;
 	}	
@@ -237,7 +251,7 @@ public class ImageSelection extends Sele
 	 */
 	public ImageSelection(TextSelection selection) {
 		textSelection = selection;
-		
+		search = textSelection.getTextNavigation();
 		imageContainer = null;
 	}
 

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ParagraphSelection.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ParagraphSelection.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ParagraphSelection.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/ParagraphSelection.java Fri Feb  8 07:44:25 2013
@@ -19,14 +19,29 @@ under the License.
 
 package org.odftoolkit.simple.common.navigation;
 
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;
+import org.odftoolkit.odfdom.dom.attribute.style.StyleMasterPageNameAttribute;
+import org.odftoolkit.odfdom.dom.element.OdfStyleBase;
+import org.odftoolkit.odfdom.dom.element.style.StyleMasterPageElement;
+import org.odftoolkit.odfdom.dom.element.style.StyleParagraphPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.text.TextParagraphElementBase;
 import org.odftoolkit.odfdom.dom.element.text.TextSElement;
+import org.odftoolkit.odfdom.dom.style.props.OdfStylePropertiesSet;
+import org.odftoolkit.odfdom.dom.style.props.OdfStyleProperty;
+import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
 import org.odftoolkit.odfdom.pkg.OdfElement;
+import org.odftoolkit.odfdom.pkg.OdfName;
 import org.odftoolkit.simple.TextDocument;
 import org.odftoolkit.simple.common.TextExtractor;
+import org.odftoolkit.simple.common.navigation.Selection;
+import org.odftoolkit.simple.common.navigation.TextNavigation;
+import org.odftoolkit.simple.common.navigation.TextSelection;
+import org.odftoolkit.simple.style.ParagraphProperties;
 import org.odftoolkit.simple.text.Paragraph;
 import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
@@ -48,9 +63,32 @@ public class ParagraphSelection extends 
 	 */
 	public Paragraph replaceWithParagraph(Paragraph paragraph) {
 		this.sourceParagraph=paragraph;
+		if (search instanceof TextNavigation) {
+			TextNavigation textSearch = (TextNavigation) search;
 		int leftLength = textSelection.getText().length();
 		int index = textSelection.getIndex();
-		prepareParagraphContainer(leftLength, index);
+			boolean continued = false;
+			if (textSearch != null
+					&& textSearch.getReplacedItem() != null
+					&& textSearch.getReplacedItem().getElement() == this.textSelection
+							.getElement()) {
+				continued = true;
+			} else {
+				textSearch.setHandlePageBreak(false);
+			}
+			prepareParagraphContainer(leftLength, index, continued);
+			Selection.SelectionManager.unregisterItem(this.textSelection);
+			if (textSearch != null) {
+				textSearch.setReplacedItem(this.textSelection);
+				OdfElement containerElement = paragraphContainer
+						.getOdfElement();
+				String content = TextExtractor.getText(containerElement);
+				TextSelection selected = TextSelection.newTextSelection(
+						textSearch, this.textSelection.getText(),
+						containerElement, content.length() - 1);
+				textSearch.setSelectedItem(selected);
+			}
+		}
 		return paragraphContainer;
 	}
 
@@ -63,6 +101,7 @@ public class ParagraphSelection extends 
 	 */
 	public ParagraphSelection(TextSelection selection) {
 		textSelection = selection;
+		search = textSelection.getTextNavigation();
 		paragraphContainer = null;
 	}
 
@@ -189,50 +228,228 @@ public class ParagraphSelection extends 
 		textSelection.refreshAfterFrontalInsert(insertedItem);
 	}
 
-	private void prepareParagraphContainer(int leftLength, int index) {
+	private ParagraphProperties getParagraphPropertiesForWrite() {
+		OdfStyleBase style = paragraphContainer.getStyleHandler()
+				.getStyleElementForRead();
+		if (style == null || style.getLocalName().equals("default-style")) {
+			OdfStyle element = paragraphContainer.getStyleHandler()
+					.getStyleElementForWrite();
+			NodeList nodes = element.getChildNodes();
+			int size = nodes.getLength();
+			for (int i = 0; i < size; i++) {
+				element.removeChild(nodes.item(0));
+			}
+		}
+		ParagraphProperties properties = paragraphContainer.getStyleHandler()
+				.getParagraphPropertiesForWrite();
+		return properties;
+	}
+	private OdfStyleBase getParagraphStyleElementForWrite() {
+		OdfStyleBase style = paragraphContainer.getStyleHandler()
+				.getStyleElementForRead();
+		OdfStyle element = paragraphContainer.getStyleHandler()
+				.getStyleElementForWrite();
+		if (style == null || style.getLocalName().equals("default-style")) {
+			NodeList nodes = element.getChildNodes();
+			int size = nodes.getLength();
+			for (int i = 0; i < size; i++) {
+				element.removeChild(nodes.item(0));
+			}
+		}
+		return element;
+	}
+	private void handlePageBreak(Paragraph origParagraph, String pos,
+			boolean continued) {
+		if (continued
+				&& this.textSelection.getTextNavigation().isHandlePageBreak())
+			return;
+		ParagraphProperties orgParaPty = origParagraph.getStyleHandler()
+				.getParagraphPropertiesForRead();
+		boolean handleBreak = false;
+		String posInPara = "middle";
+		if (continued && pos.equals("whole")) {
+			posInPara = "end";
+		} else if (continued && pos.endsWith("head")) {
+			posInPara = "middle";
+		} else if (continued && pos.endsWith("end")) {
+			posInPara = "end";
+		} else if (!continued && pos.endsWith("whole")) {
+			posInPara = "whole";
+		} else if (!continued && pos.endsWith("head")) {
+			posInPara = "head";
+		} else if (!continued && pos.endsWith("end")) {
+			posInPara = "end";
+		}
+		if (orgParaPty != null) {
+			String breakAttribute = orgParaPty.getBreakBefore();
+			if (breakAttribute != null) {
+				if (posInPara.equals("head") || posInPara.equals("whole")) {
+					getParagraphPropertiesForWrite().setBreak("before",
+							breakAttribute);
+					handleBreak = true;
+				}
+			}
+			breakAttribute = orgParaPty.getBreakAfter();
+			if (breakAttribute != null) {
+				if (posInPara.equals("end") || posInPara.equals("whole")) {
+					getParagraphPropertiesForWrite().setBreak("after",
+							breakAttribute);
+					handleBreak = true;
+				}
+			}
+		}
+		String masterStyle = origParagraph
+				.getStyleHandler()
+				.getStyleElementForRead()
+				.getOdfAttributeValue(
+						OdfName.newName(OdfDocumentNamespace.STYLE,
+								"master-page-name"));
+		if (masterStyle != null && !masterStyle.isEmpty()) {
+			if (posInPara.equals("head") || posInPara.equals("whole")) {
+				getParagraphStyleElementForWrite().setOdfAttributeValue(
+						OdfName.newName(OdfDocumentNamespace.STYLE,
+								"master-page-name"), masterStyle);
+				handleBreak = true;
+				try {
+					int pageNumber = orgParaPty.getPageNumber();
+					if (pos.equals("head")) {
+						paragraphContainer.getStyleHandler()
+								.getParagraphPropertiesForWrite()
+								.setPageNumber(pageNumber);
+					}
+				} catch (NumberFormatException e) {
+					Logger.getLogger(ParagraphSelection.class.getName()).log(
+							Level.SEVERE, e.getMessage(), "NumberFormatException");
+				}
+			}
+		}
+		if (handleBreak && !posInPara.equals("whole"))
+			cleanBreakProperty(origParagraph);
+	}
+	private void cleanBreakProperty(Paragraph paragraph) {
+		TextNavigation search = this.textSelection.getTextNavigation();
+		if (search == null)
+			throw new IllegalStateException("Navigation is null");
+		OdfStyleBase styleElement = paragraph.getStyleHandler()
+				.getStyleElementForRead();
+		String name = styleElement.getAttribute("style:name");
+		String newName = null;
+		OdfElement modifiedStyleElement = search
+				.getModifiedStyleElement(styleElement);
+		if (modifiedStyleElement == null) {
+			modifiedStyleElement = (OdfElement) styleElement.cloneNode(true);
+			search.addModifiedStyleElement(styleElement, modifiedStyleElement);
+			NodeList paragraphProperties = modifiedStyleElement
+					.getElementsByTagName("style:paragraph-properties");
+			if (paragraphProperties != null
+					&& paragraphProperties.getLength() > 0) {
+				StyleParagraphPropertiesElement property = (StyleParagraphPropertiesElement) paragraphProperties
+						.item(0);
+				property.removeAttribute("fo:break-before");
+				property.removeAttribute("fo:break-after");
+				property.removeAttribute("style:page-number");
+			}
+			modifiedStyleElement.removeAttribute("style:master-page-name");
+			newName = name + "-" + makeUniqueName();
+			NamedNodeMap attributes = modifiedStyleElement.getAttributes();
+			if (attributes != null) {
+				for (int i = 0; i < attributes.getLength(); i++) {
+					Node item = attributes.item(i);
+					String value = item.getNodeValue();
+					if (name.equals(value)) {
+						item.setNodeValue(newName);
+						break;
+					}
+				}
+			}
+			styleElement.getParentNode().appendChild(modifiedStyleElement);
+		} else {
+			newName = modifiedStyleElement.getAttribute("style:name");
+		}
+		NamedNodeMap attributes = paragraph.getOdfElement().getAttributes();
+		if (attributes != null) {
+			for (int i = 0; i < attributes.getLength(); i++) {
+				Node item = attributes.item(i);
+				String value = item.getNodeValue();
+				if (name.equals(value)) {
+					item.setNodeValue(newName);
+					break;
+				}
+			}
+		}
+		this.textSelection.getTextNavigation().setHandlePageBreak(true);
+	}
+	String makeUniqueName() {
+		return String.format("p%06x", (int) (Math.random() * 0xffffff));
+	}
+	private void prepareParagraphContainer(int leftLength, int index,
+			boolean continued) {
 		if (paragraphContainer == null) {
+			String pos = "middle";
 			OdfElement rightparentElement = textSelection.getContainerElement();
 			int nodeLength = TextExtractor.getText(rightparentElement).length();
 			if(index==0){
 				if(leftLength==nodeLength){
 					//Replace whole Paragraph
-					Paragraph orgparagraph = Paragraph.getInstanceof((TextParagraphElementBase)rightparentElement);
-					TextDocument document = (TextDocument)orgparagraph.getOwnerDocument();
-					paragraphContainer = document.insertParagraph(orgparagraph, sourceParagraph, false);
+					Paragraph orgparagraph = Paragraph
+							.getInstanceof((TextParagraphElementBase) rightparentElement);
+					TextDocument document = (TextDocument) orgparagraph
+							.getOwnerDocument();
+					paragraphContainer = document.insertParagraph(orgparagraph,
+							sourceParagraph, false);
 					NodeList cnl = rightparentElement.getChildNodes();
-					for(int i=0;i<cnl.getLength();i++){
-						rightparentElement.removeChild(cnl.item(i));
-					}
+					pos = "whole";
+					handlePageBreak(orgparagraph, pos, continued);
+
+					rightparentElement.getParentNode().removeChild(
+							rightparentElement);
 				}else{
 					//at the start of original Paragraph, insert before original Paragraph
 					delete(index, leftLength, rightparentElement);
-					Paragraph orgparagraph = Paragraph.getInstanceof((TextParagraphElementBase)rightparentElement);
-					TextDocument document = (TextDocument)orgparagraph.getOwnerDocument();
-					paragraphContainer = document.insertParagraph(orgparagraph, sourceParagraph, true);					
-				}
+					Paragraph orgparagraph = Paragraph
+							.getInstanceof((TextParagraphElementBase) rightparentElement);
+					TextDocument document = (TextDocument) orgparagraph
+							.getOwnerDocument();
+					paragraphContainer = document.insertParagraph(orgparagraph,
+							sourceParagraph, true);
+					pos = "head";
+					handlePageBreak(orgparagraph, pos, continued);
 			}
-			else if(nodeLength==(index+leftLength)){
+			} else if (nodeLength == (index + leftLength)) {
 				//at the end of original Paragraph, insert after original Paragraph
 				delete(index, leftLength, rightparentElement);
-				Paragraph orgparagraph = Paragraph.getInstanceof((TextParagraphElementBase)rightparentElement);
-				TextDocument document = (TextDocument)orgparagraph.getOwnerDocument();
-				paragraphContainer = document.insertParagraph(orgparagraph, sourceParagraph, false);
+				Paragraph orgparagraph = Paragraph
+						.getInstanceof((TextParagraphElementBase) rightparentElement);
+				TextDocument document = (TextDocument) orgparagraph
+						.getOwnerDocument();
+				paragraphContainer = document.insertParagraph(orgparagraph,
+						sourceParagraph, false);
+				handlePageBreak(orgparagraph, pos, continued);
 			}else{
 				//at the middle of original Paragraph, split original Paragraph, insert before the second Paragraph.
 				delete(index, leftLength, rightparentElement);
 				Node leftparentElement = rightparentElement.cloneNode(true);
-				rightparentElement.getParentNode().insertBefore(leftparentElement,rightparentElement);
-				nodeLength = TextExtractor.getText((OdfElement) leftparentElement).length();
+				rightparentElement.getParentNode().insertBefore(
+						leftparentElement, rightparentElement);
+				nodeLength = TextExtractor.getText(
+						(OdfElement) leftparentElement).length();
 				delete(index, nodeLength-index, leftparentElement);
 				delete(0, index, rightparentElement);
-				Paragraph orgparagraph = Paragraph.getInstanceof((TextParagraphElementBase)rightparentElement);
-				TextDocument document = (TextDocument)orgparagraph.getOwnerDocument();
-				paragraphContainer = document.insertParagraph(orgparagraph, sourceParagraph, true);
+				Paragraph orgparagraph = Paragraph
+						.getInstanceof((TextParagraphElementBase) rightparentElement);
+				TextDocument document = (TextDocument) orgparagraph
+						.getOwnerDocument();
+				paragraphContainer = document.insertParagraph(orgparagraph,
+						sourceParagraph, true);
+				if (!continued)
+					cleanBreakProperty(orgparagraph);
 				
 			}
 		} else{
-			TextDocument document = (TextDocument)paragraphContainer.getOwnerDocument();
-			Paragraph tmp = document.insertParagraph(paragraphContainer, sourceParagraph, true);
+			TextDocument document = (TextDocument) paragraphContainer
+					.getOwnerDocument();
+			Paragraph tmp = document.insertParagraph(paragraphContainer,
+					sourceParagraph, true);
 			paragraphContainer.remove();
 			paragraphContainer=tmp;
 		}

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/Selection.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/Selection.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/Selection.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/Selection.java Fri Feb  8 07:44:25 2013
@@ -33,6 +33,10 @@ public abstract class Selection {
 
 	private OdfElement mElement;
 	private int mIndex;
+	protected Navigation search;
+	public Navigation getNavigation() {
+		return search;
+	}
 
 	/**
 	 * Get the container element of this <code>Selection</code>.

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TableSelection.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TableSelection.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TableSelection.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TableSelection.java Fri Feb  8 07:44:25 2013
@@ -19,13 +19,27 @@ under the License.
 
 package org.odftoolkit.simple.common.navigation;
 
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;
+import org.odftoolkit.odfdom.dom.element.OdfStyleBase;
 import org.odftoolkit.odfdom.dom.element.table.TableTableElement;
 import org.odftoolkit.odfdom.dom.element.text.TextParagraphElementBase;
 import org.odftoolkit.odfdom.dom.element.text.TextSElement;
+import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
 import org.odftoolkit.odfdom.pkg.OdfElement;
+import org.odftoolkit.odfdom.pkg.OdfName;
 import org.odftoolkit.simple.TextDocument;
 import org.odftoolkit.simple.common.TextExtractor;
+import org.odftoolkit.simple.common.navigation.Navigation;
+import org.odftoolkit.simple.common.navigation.Selection;
+import org.odftoolkit.simple.common.navigation.TextNavigation;
+import org.odftoolkit.simple.common.navigation.TextSelection;
+import org.odftoolkit.simple.style.DefaultStyleHandler;
+import org.odftoolkit.simple.style.ParagraphProperties;
+import org.odftoolkit.simple.style.TableProperties;
+import org.odftoolkit.simple.table.Cell;
+import org.odftoolkit.simple.table.Row;
 import org.odftoolkit.simple.table.Table;
 import org.odftoolkit.simple.text.Paragraph;
 import org.w3c.dom.Element;
@@ -51,9 +65,37 @@ public class TableSelection extends Sele
 	 */
 	public Table replaceWithTable(Table table) {
 		this.sourceTable=table;
+		if (search instanceof TextNavigation) {
 		int leftLength = textSelection.getText().length();
 		int index = textSelection.getIndex();
-		preparetableContainer(leftLength, index);
+			boolean continued = false;
+			TextNavigation textSearch = (TextNavigation) search;
+			if (textSearch != null
+					&& textSearch.getReplacedItem() != null
+					&& textSearch.getReplacedItem().getElement() == this.textSelection
+							.getElement()) {
+				continued = true;
+			} else {
+				textSearch.setHandlePageBreak(false);
+			}
+			preparetableContainer(leftLength, index, continued);
+			Selection.SelectionManager.unregisterItem(this.textSelection);
+			if (textSearch != null) {
+				textSearch.setReplacedItem(this.textSelection);
+				Paragraph lastParagraph = getLastParagraphInTable(tableContainer);
+				OdfElement newStartPoint;
+				if (lastParagraph != null) {
+					newStartPoint = lastParagraph.getOdfElement();
+				} else {
+					newStartPoint = tableContainer.getOdfElement();
+				}
+				String content = TextExtractor.getText(newStartPoint);
+				TextSelection selected = newTextSelection(textSearch,
+						this.textSelection.getText(), newStartPoint,
+						content.length() - 1);
+				textSearch.setSelectedItem(selected);
+			}
+		}
 		return tableContainer;
 	}
 
@@ -66,6 +108,7 @@ public class TableSelection extends Sele
 	 */
 	public TableSelection(TextSelection selection) {
 		textSelection = selection;
+		search = textSelection.getTextNavigation();
 		tableContainer = null;
 	}
 
@@ -192,54 +235,216 @@ public class TableSelection extends Sele
 		textSelection.refreshAfterFrontalInsert(insertedItem);
 	}
 
-	private void preparetableContainer(int leftLength, int index) {
+	private void preparetableContainer(int leftLength, int index,
+			boolean continued) {
 		if (tableContainer == null) {
+			String pos = "middle";
 			OdfElement rightparentElement = textSelection.getContainerElement();
 			int nodeLength = TextExtractor.getText(rightparentElement).length();
 			if(index==0){
 				
 				if(leftLength==nodeLength){
 					//Replace whole Paragraph
-					Paragraph orgparagraph = Paragraph.getInstanceof((TextParagraphElementBase)rightparentElement);
-					TextDocument document = (TextDocument)orgparagraph.getOwnerDocument();
-					tableContainer = document.insertTable(orgparagraph, sourceTable, false);
-					NodeList cnl = rightparentElement.getChildNodes();
-					for(int i=0;i<cnl.getLength();i++){
-						rightparentElement.removeChild(cnl.item(i));
-					}
-					
+					Paragraph orgparagraph = Paragraph
+							.getInstanceof((TextParagraphElementBase) rightparentElement);
+					TextDocument document = (TextDocument) orgparagraph
+							.getOwnerDocument();
+					tableContainer = document.insertTable(orgparagraph,
+							sourceTable, false);
+					pos = "whole";
+					handlePageBreak(orgparagraph, pos, continued);
+
+					rightparentElement.getParentNode().removeChild(
+							rightparentElement);
 				}else{
 					//at the start of original Paragraph, insert before original Paragraph
 					delete(index, leftLength, rightparentElement);
-					Paragraph orgparagraph = Paragraph.getInstanceof((TextParagraphElementBase)rightparentElement);
-					TextDocument document = (TextDocument)orgparagraph.getOwnerDocument();
-					tableContainer = document.insertTable(orgparagraph, sourceTable, true);					
-				}
+					Paragraph orgparagraph = Paragraph
+							.getInstanceof((TextParagraphElementBase) rightparentElement);
+					TextDocument document = (TextDocument) orgparagraph
+							.getOwnerDocument();
+					tableContainer = document.insertTable(orgparagraph,
+							sourceTable, true);
+					pos = "head";
+					handlePageBreak(orgparagraph, pos, continued);
 			}
-			else if(nodeLength==(index+leftLength)){
+			} else if (nodeLength == (index + leftLength)) {
 				//at the end of original Paragraph, insert after original Paragraph
 				delete(index, leftLength, rightparentElement);
-				Paragraph orgparagraph = Paragraph.getInstanceof((TextParagraphElementBase)rightparentElement);
-				TextDocument document = (TextDocument)orgparagraph.getOwnerDocument();
-				tableContainer =document.insertTable(orgparagraph, sourceTable,false);
+				Paragraph orgparagraph = Paragraph
+						.getInstanceof((TextParagraphElementBase) rightparentElement);
+				TextDocument document = (TextDocument) orgparagraph
+						.getOwnerDocument();
+				tableContainer = document.insertTable(orgparagraph,
+						sourceTable, false);
+				handlePageBreak(orgparagraph, pos, continued);
 			}else{
 				//at the middle of original Paragraph, split original Paragraph, insert before the second Paragraph.
 				delete(index, leftLength, rightparentElement);
 				Node leftparentElement = rightparentElement.cloneNode(true);
-				rightparentElement.getParentNode().insertBefore(leftparentElement,rightparentElement);
-				nodeLength = TextExtractor.getText((OdfElement) leftparentElement).length();
+				rightparentElement.getParentNode().insertBefore(
+						leftparentElement, rightparentElement);
+				nodeLength = TextExtractor.getText(
+						(OdfElement) leftparentElement).length();
 				delete(index, nodeLength-index, leftparentElement);
 				delete(0, index, rightparentElement);
-				Paragraph orgparagraph = Paragraph.getInstanceof((TextParagraphElementBase)rightparentElement);
-				TextDocument document = (TextDocument)orgparagraph.getOwnerDocument();
-				tableContainer =  document.insertTable(orgparagraph, sourceTable, true);
+				Paragraph orgparagraph = Paragraph
+						.getInstanceof((TextParagraphElementBase) rightparentElement);
+				TextDocument document = (TextDocument) orgparagraph
+						.getOwnerDocument();
+				tableContainer = document.insertTable(orgparagraph,
+						sourceTable, true);
+				if (!continued)
+					textSelection.cleanBreakProperty(orgparagraph);
 			}
 		} else{
-			TextDocument document = (TextDocument)tableContainer.getOwnerDocument();
-			TableTableElement newTEle =(TableTableElement) document.insertOdfElement(tableContainer.getOdfElement(),tableContainer.getOwnerDocument(), sourceTable.getOdfElement(), true);
-			tableContainer.getOdfElement().getParentNode().removeChild(tableContainer.getOdfElement());
+			TextDocument document = (TextDocument) tableContainer
+					.getOwnerDocument();
+			TableTableElement newTEle = (TableTableElement) document
+					.insertOdfElement(tableContainer.getOdfElement(),
+							tableContainer.getOwnerDocument(),
+							sourceTable.getOdfElement(), true);
+			tableContainer.getOdfElement().getParentNode()
+					.removeChild(tableContainer.getOdfElement());
 			Table table = Table.getInstance(newTEle);
 			tableContainer=table;
 		}
 	}
+	private Paragraph getLastParagraphInTable(Table table) {
+		Paragraph paragraph = null;
+		int rowCount = table.getRowCount();
+		for (int i = rowCount - 1; i >= 0; i--) {
+			Row row = table.getRowByIndex(i);
+			int cellCount = row.getCellCount();
+			for (int j = cellCount - 1; j >= 0; j--) {
+				Cell cell = row.getCellByIndex(j);
+				paragraph = cell.getParagraphByReverseIndex(0, false);
+				if (paragraph != null)
+					return paragraph;
+			}
+		}
+		return paragraph;
+	}
+	private TableProperties getTablePropertiesForWrite() {
+		OdfStyleBase style = tableContainer.getStyleHandler()
+				.getStyleElementForRead();
+		if (style == null || style.getLocalName().equals("default-style")) {
+			OdfStyle element = tableContainer.getStyleHandler()
+					.getStyleElementForWrite();
+			NodeList nodes = element.getChildNodes();
+			int size = nodes.getLength();
+			for (int i = 0; i < size; i++) {
+				element.removeChild(nodes.item(0));
+			}
+		}
+		TableProperties properties = tableContainer.getStyleHandler()
+				.getTablePropertiesForWrite();
+		return properties;
+	}
+	private OdfStyleBase getParagraphStyleElementForWrite() {
+		OdfStyleBase style = tableContainer.getStyleHandler()
+				.getStyleElementForRead();
+		OdfStyle element = tableContainer.getStyleHandler()
+				.getStyleElementForWrite();
+		if (style == null || style.getLocalName().equals("default-style")) {
+			NodeList nodes = element.getChildNodes();
+			int size = nodes.getLength();
+			for (int i = 0; i < size; i++) {
+				element.removeChild(nodes.item(0));
+			}
+		}
+		return element;
+	}
+	private void handlePageBreak(Paragraph origParagraph, String pos,
+			boolean continued) {
+		if (continued
+				&& this.textSelection.getTextNavigation().isHandlePageBreak())
+			return;
+		ParagraphProperties orgParaPty = origParagraph.getStyleHandler()
+				.getParagraphPropertiesForRead();
+		boolean handleBreak = false;
+		String posInPara = "middle";
+		if (continued && pos.equals("whole")) {
+			posInPara = "end";
+		} else if (continued && pos.endsWith("head")) {
+			posInPara = "middle";
+		} else if (continued && pos.endsWith("end")) {
+			posInPara = "end";
+		} else if (!continued && pos.endsWith("whole")) {
+			posInPara = "whole";
+		} else if (!continued && pos.endsWith("head")) {
+			posInPara = "head";
+		} else if (!continued && pos.endsWith("end")) {
+			posInPara = "end";
+		}
+		if (orgParaPty != null) {
+			String breakAttribute = orgParaPty.getBreakBefore();
+			if (breakAttribute != null) {
+				if (posInPara.equals("head") || posInPara.equals("whole")) {
+					getTablePropertiesForWrite().setBreak("before",
+							breakAttribute);
+					handleBreak = true;
+				}
+			}
+			breakAttribute = orgParaPty.getBreakAfter();
+			if (breakAttribute != null) {
+				if (posInPara.equals("end") || posInPara.equals("whole")) {
+					getTablePropertiesForWrite().setBreak("after",
+							breakAttribute);
+					handleBreak = true;
+				}
+			}
+		}
+		String masterStyle = origParagraph
+				.getStyleHandler()
+				.getStyleElementForRead()
+				.getOdfAttributeValue(
+						OdfName.newName(OdfDocumentNamespace.STYLE,
+								"master-page-name"));
+		if (masterStyle != null && !masterStyle.isEmpty()) {
+			if (posInPara.equals("head") || posInPara.equals("whole")) {
+				getParagraphStyleElementForWrite().setOdfAttributeValue(
+						OdfName.newName(OdfDocumentNamespace.STYLE,
+								"master-page-name"), masterStyle);
+				handleBreak = true;
+				try {
+					int pageNumber = orgParaPty.getPageNumber();
+					if (pos.equals("head")) {
+						tableContainer.getStyleHandler()
+								.getParagraphPropertiesForWrite()
+								.setPageNumber(pageNumber);
+					}
+				} catch (NumberFormatException e) {
+					Logger.getLogger(ParagraphSelection.class.getName()).log(
+							Level.SEVERE, e.getMessage(), "NumberFormatException");
+				}
+			}
+		}
+		if (handleBreak && !posInPara.equals("whole"))
+			textSelection.cleanBreakProperty(origParagraph);
+	}
+	private class TextSelectionForTableReplacement extends TextSelection {
+		private OdfElement mContainer;
+		TextSelectionForTableReplacement(Navigation search, String text,
+				OdfElement containerElement, int index) {
+			super(search, text, containerElement, index);
+			if (containerElement instanceof TableTableElement)
+				mContainer = containerElement;
+		}
+		@Override
+		public OdfElement getContainerElement() {
+			OdfElement element = super.getContainerElement();
+			if (element == null) {
+				element = mContainer;
+			}
+			return element;
+		}
+	}
+	TextSelection newTextSelection(Navigation search, String text,
+			OdfElement containerElement, int index) {
+		TextSelection selection = new TextSelectionForTableReplacement(search,
+				text, containerElement, index);
+		Selection.SelectionManager.registerItem(selection);
+		return selection;
+	}
 }

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextDocumentSelection.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextDocumentSelection.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextDocumentSelection.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextDocumentSelection.java Fri Feb  8 07:44:25 2013
@@ -250,16 +250,14 @@ public class TextDocumentSelection exten
 		if (index == 0) {
 			if (leftLength == nodeLength) {
 				// Replace whole Paragraph
-				
+				OdfElement refElement = orgparagraph.getOdfElement();
 				for (int i = 0; i < clist.getLength(); i++) {
 					OdfElement node = (OdfElement) clist.item(i);
-					document.insertOdfElement(orgparagraph.getOdfElement(), sourceDocument, node, false);
-				}
-				
-				NodeList cnl = rightparentElement.getChildNodes();
-				for (int i = 0; i < cnl.getLength(); i++) {
-					rightparentElement.removeChild(cnl.item(i));
+					refElement = document.insertOdfElement(refElement,
+							sourceDocument, node, false);
 				}
+				rightparentElement.getParentNode().removeChild(
+						rightparentElement);
 			} else {
 				// at the start of original Paragraph, insert before original
 				// Paragraph

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextNavigation.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextNavigation.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextNavigation.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextNavigation.java Fri Feb  8 07:44:25 2013
@@ -19,6 +19,7 @@ under the License.
 
 package org.odftoolkit.simple.common.navigation;
 
+import java.util.IdentityHashMap;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.regex.Matcher;
@@ -26,6 +27,7 @@ import java.util.regex.Pattern;
 
 import org.odftoolkit.odfdom.dom.element.table.TableTableCellElementBase;
 import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeMasterStyles;
+import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
 import org.odftoolkit.odfdom.pkg.OdfElement;
 import org.odftoolkit.odfdom.pkg.OdfFileDom;
 import org.odftoolkit.odfdom.pkg.OdfName;
@@ -48,9 +50,12 @@ public class TextNavigation extends Navi
 	private OdfElement mElement;
 	private TextSelection mNextSelectedItem;
 	private TextSelection mTempSelectedItem;
+	private TextSelection mReplacedItem;
+	private boolean handlePageBreak;
 	private String mNextText;
 	private int mNextIndex;
 	private boolean mbFinishFindInHeaderFooter;
+	private IdentityHashMap<OdfElement, OdfElement> modifiedStyleList;
 
 	/**
 	 * Construct <code>TextNavigation</code> with matched condition and
@@ -68,6 +73,7 @@ public class TextNavigation extends Navi
 		mNextSelectedItem = null;
 		mTempSelectedItem = null;
 		mbFinishFindInHeaderFooter = false;
+		setHandlePageBreak(false);
 	}
 
 	/**
@@ -100,6 +106,12 @@ public class TextNavigation extends Navi
 		mTempSelectedItem = findNext(mNextSelectedItem);
 		return (mTempSelectedItem != null);
 	}
+	void setSelectedItem(TextSelection nextSelectedItem) {
+		this.mNextSelectedItem = nextSelectedItem;
+	}
+	TextSelection getSelectedItem() {
+		return this.mNextSelectedItem;
+	}
 
 	/**
 	 * Get next <code>TextSelection</code>.
@@ -271,8 +283,15 @@ public class TextNavigation extends Navi
 		int nextIndex = -1;
 		Matcher matcher = mPattern.matcher(content);
 		// start from the end index of the selected item
+		if (!selected.isSelectionReplaced()) {
 		if (((content.length() > index + selected.getText().length()))
 				&& (matcher.find(index + selected.getText().length()))) {
+				nextIndex = matcher.start();
+				int eIndex = matcher.end();
+				mNextText = content.substring(nextIndex, eIndex);
+			}
+		} else if (((content.length() >= index + selected.getText().length()))
+				&& (matcher.find(index))) {
 			// here just consider \n\r\t occupy one char
 			nextIndex = matcher.start();
 			int eIndex = matcher.end();
@@ -301,7 +320,8 @@ public class TextNavigation extends Navi
 			if (TableTableCellElementBase.class.isInstance(parent)) {
 				TableTableCellElementBase cellElement = (TableTableCellElementBase) parent;
 				Cell cell = Cell.getInstance(cellElement);
-				item = new CellSelection(mNextText, containerElement, nextIndex, cell);
+				item = new CellSelection(this, mNextText, containerElement,
+						nextIndex, cell);
 				break;
 			} else {
 				OdfName odfName = ((OdfElement) parent).getOdfName();
@@ -314,8 +334,35 @@ public class TextNavigation extends Navi
 			}
 		}
 		if (item == null) {
-			item = new TextSelection(mNextText, containerElement, nextIndex);
+			item = new TextSelection(this, mNextText, containerElement,
+					nextIndex);
 		}
 		return item;
 	}
+	OdfElement getModifiedStyleElement(OdfElement styleElement) {
+		if (modifiedStyleList == null)
+			return null;
+		return modifiedStyleList.get(styleElement);
+	}
+	void addModifiedStyleElement(OdfElement styleElment,
+			OdfElement modifiedStyleElement) {
+		if (modifiedStyleElement != null) {
+			if (modifiedStyleList == null) {
+				modifiedStyleList = new IdentityHashMap<OdfElement, OdfElement>();
+			}
+			modifiedStyleList.put(styleElment, modifiedStyleElement);
+		}
+	}
+	boolean isHandlePageBreak() {
+		return handlePageBreak;
+	}
+	void setHandlePageBreak(boolean handlePageBreak) {
+		this.handlePageBreak = handlePageBreak;
+	}
+	void setReplacedItem(TextSelection replacedItem) {
+		this.mReplacedItem = replacedItem;
+	}
+	TextSelection getReplacedItem() {
+		return this.mReplacedItem;
+	}
 }

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextSelection.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextSelection.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextSelection.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextSelection.java Fri Feb  8 07:44:25 2013
@@ -34,6 +34,7 @@ import org.odftoolkit.odfdom.dom.element
 import org.odftoolkit.odfdom.dom.element.dc.DcCreatorElement;
 import org.odftoolkit.odfdom.dom.element.dc.DcDateElement;
 import org.odftoolkit.odfdom.dom.element.office.OfficeAnnotationElement;
+import org.odftoolkit.odfdom.dom.element.style.StyleParagraphPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleTextPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.text.TextAElement;
 import org.odftoolkit.odfdom.dom.element.text.TextConditionalTextElement;
@@ -65,7 +66,9 @@ import org.odftoolkit.simple.draw.Image;
 import org.odftoolkit.simple.table.Table;
 import org.odftoolkit.simple.text.Paragraph;
 import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 /**
  * <code>TextSelection</code> describes a sub element in a paragraph element or
@@ -83,6 +86,7 @@ public class TextSelection extends Selec
 	private OdfTextHeading mHeading;
 	private int mIndexInContainer;
 	private boolean mIsInserted;
+	private boolean isSelectionReplaced = false;
 
 	/**
 	 * Constructor of <code>TextSelection</code>.
@@ -96,7 +100,9 @@ public class TextSelection extends Selec
 	 *            the start index of the text content in container element
 	 * 
 	 */
-	TextSelection(String text, OdfElement containerElement, int index) {
+	TextSelection(Navigation search, String text, OdfElement containerElement,
+			int index) {
+		this.search = search;
 		mMatchedText = text;
 		if (containerElement instanceof OdfTextParagraph) {
 			mParagraph = (OdfTextParagraph) containerElement;
@@ -106,6 +112,12 @@ public class TextSelection extends Selec
 		mIndexInContainer = index;
 	}
 
+	public TextNavigation getTextNavigation() {
+		if (search instanceof TextNavigation) {
+			return (TextNavigation) search;
+		}
+		return null;
+	}
 	/**
 	 * Create a new <code>TextSelection</code>.
 	 * 
@@ -119,8 +131,10 @@ public class TextSelection extends Selec
 	 * 
 	 * @since 0.5.5
 	 */
-	public static TextSelection newTextSelection(String text, OdfElement containerElement, int index) {
-		TextSelection selection = new TextSelection(text, containerElement, index);
+	public static TextSelection newTextSelection(Navigation search,
+			String text, OdfElement containerElement, int index) {
+		TextSelection selection = new TextSelection(search, text,
+				containerElement, index);
 		Selection.SelectionManager.registerItem(selection);
 		return selection;
 	}
@@ -539,6 +553,12 @@ public class TextSelection extends Selec
 		adjustStyle(newElement, textSpan, null);
 		SelectionManager.refreshAfterPasteAtEndOf(this, positionItem);
 	}
+	public void setSelectionReplaced(boolean b) {
+		this.isSelectionReplaced = b;
+	}
+	public boolean isSelectionReplaced() {
+		return this.isSelectionReplaced;
+	}
 
 	/**
 	 * Add a hypertext reference to the selection.
@@ -650,6 +670,62 @@ public class TextSelection extends Selec
 		}
 	}
 
+	void cleanBreakProperty(Paragraph paragraph) {
+		TextNavigation search = this.getTextNavigation();
+		if (search == null)
+			throw new IllegalStateException("Navigation is null");
+		OdfStyleBase styleElement = paragraph.getStyleHandler()
+				.getStyleElementForRead();
+		String name = styleElement.getAttribute("style:name");
+		String newName = null;
+		OdfElement modifiedStyleElement = search
+				.getModifiedStyleElement(styleElement);
+		if (modifiedStyleElement == null) {
+			modifiedStyleElement = (OdfElement) styleElement.cloneNode(true);
+			search.addModifiedStyleElement(styleElement, modifiedStyleElement);
+			NodeList paragraphProperties = modifiedStyleElement
+					.getElementsByTagName("style:paragraph-properties");
+			if (paragraphProperties != null
+					&& paragraphProperties.getLength() > 0) {
+				StyleParagraphPropertiesElement property = (StyleParagraphPropertiesElement) paragraphProperties
+						.item(0);
+				property.removeAttribute("fo:break-before");
+				property.removeAttribute("fo:break-after");
+				property.removeAttribute("style:page-number");
+			}
+			modifiedStyleElement.removeAttribute("style:master-page-name");
+			newName = name + "-" + makeUniqueName();
+			NamedNodeMap attributes = modifiedStyleElement.getAttributes();
+			if (attributes != null) {
+				for (int i = 0; i < attributes.getLength(); i++) {
+					Node item = attributes.item(i);
+					String value = item.getNodeValue();
+					if (name.equals(value)) {
+						item.setNodeValue(newName);
+						break;
+					}
+				}
+			}
+			styleElement.getParentNode().appendChild(modifiedStyleElement);
+		} else {
+			newName = modifiedStyleElement.getAttribute("style:name");
+		}
+		NamedNodeMap attributes = paragraph.getOdfElement().getAttributes();
+		if (attributes != null) {
+			for (int i = 0; i < attributes.getLength(); i++) {
+				Node item = attributes.item(i);
+				String value = item.getNodeValue();
+				if (name.equals(value)) {
+					item.setNodeValue(newName);
+					break;
+				}
+			}
+		}
+		this.getTextNavigation().setHandlePageBreak(true);
+	}
+	String makeUniqueName() {
+		return String.format("p%06x", (int) (Math.random() * 0xffffff));
+	}
 	/*
 	 * Return a new span that cover this selection and keep the original style
 	 * of this <code>Selection</code>.

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextStyleNavigation.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextStyleNavigation.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextStyleNavigation.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/common/navigation/TextStyleNavigation.java Fri Feb  8 07:44:25 2013
@@ -151,7 +151,8 @@ public class TextStyleNavigation extends
 		}
 		if (mNode != null) {
 			element = (OdfElement) getPHElement(mNode);
-			TextSelection item = new TextSelection(mText, element, mCurrentIndex);
+			TextSelection item = new TextSelection(this, mText, element,
+					mCurrentIndex);
 			return item;
 		}
 		return null;

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/DefaultStyleHandler.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/DefaultStyleHandler.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/DefaultStyleHandler.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/DefaultStyleHandler.java Fri Feb  8 07:44:25 2013
@@ -107,8 +107,16 @@ public class DefaultStyleHandler {
 	 * The writable graphics properties element.
 	 */
 	protected GraphicProperties mWritableGraphicProperties;
+	/**
+	 * The readable table properties element.
+	 */
+	protected TableProperties mTableProperties;
 
 	/**
+	 * The writable table properties element.
+	 */
+	protected TableProperties mWritableTableProperties;
+	/**
 	 * Constructor of DefaultStyleHandler
 	 * 
 	 * @param element
@@ -436,6 +444,78 @@ public class DefaultStyleHandler {
 	}
 
 	/**
+	 * Return the table style properties definition for this component, only for
+	 * read function.
+	 * <p>
+	 * Null will be returned if there is no style definition.
+	 * Note if you try to write style properties to the returned object, errors
+	 * will be met with.
+	 * 
+	 * @return the table style properties definition for this component, only
+	 *         for read function
+	 */
+	public TableProperties getTablePropertiesForRead() {
+		if (!mFamilyProperties.get(mOdfElement.getStyleFamily()).contains(
+				OdfStylePropertiesSet.TableProperties)) {
+			Logger.getLogger(DefaultStyleHandler.class.getName()).log(
+					Level.FINE,
+					"Table properties are not supported by style family: "
+							+ mOdfElement.getStyleFamily() + "!", "");
+			return null;
+		}
+
+		if (mWritableTableProperties != null)
+			return mWritableTableProperties;
+		else if (mTableProperties != null)
+			return mTableProperties;
+
+		OdfStyleBase style = getStyleElementForRead();
+		if (style == null) {
+			Logger.getLogger(DefaultStyleHandler.class.getName()).log(
+					Level.FINE, "No style definition is found!", "");
+			return null;
+		}
+		mTableProperties = TableProperties.getTableProperties(style);
+		if (mTableProperties != null)
+			return mTableProperties;
+		else {
+			Logger.getLogger(DefaultStyleHandler.class.getName()).log(
+					Level.FINE,
+					"No explicit table properties definition is found!", "");
+			return null;
+		}
+	}
+
+	/**
+	 * Return the table style properties definition for this component, for read
+	 * and write function.
+	 * <p>
+	 * An empty style definition will be created if there is no style
+	 * An empty table style properties definition will be created if there is no
+	 * explicit table style properties definition.
+	 * 
+	 * @return the table style properties definition for this component, for
+	 *         read and write function
+	 */
+	public TableProperties getTablePropertiesForWrite() {
+		if (!mFamilyProperties.get(mOdfElement.getStyleFamily()).contains(
+				OdfStylePropertiesSet.TableProperties)) {
+			Logger.getLogger(DefaultStyleHandler.class.getName()).log(
+					Level.FINE,
+					"Table properties are not supported by style family: "
+							+ mOdfElement.getStyleFamily() + "!", "");
+			return null;
+		}
+
+		if (mWritableTableProperties != null)
+			return mWritableTableProperties;
+		OdfStyle style = getStyleElementForWrite();
+		mWritableTableProperties = TableProperties
+				.getOrCreateTableProperties(style);
+		return mWritableTableProperties;
+	}
+
+	/**
 	 * Return the used style name of this component.
 	 * <p>
 	 * This method can be override by sub classes.

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/ParagraphProperties.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/ParagraphProperties.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/ParagraphProperties.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/ParagraphProperties.java Fri Feb  8 07:44:25 2013
@@ -19,6 +19,8 @@ under the License.
 
 package org.odftoolkit.simple.style;
 
+import org.odftoolkit.odfdom.dom.attribute.fo.FoBreakAfterAttribute;
+import org.odftoolkit.odfdom.dom.attribute.fo.FoBreakBeforeAttribute;
 import org.odftoolkit.odfdom.dom.attribute.fo.FoMarginBottomAttribute;
 import org.odftoolkit.odfdom.dom.attribute.fo.FoMarginLeftAttribute;
 import org.odftoolkit.odfdom.dom.attribute.fo.FoMarginRightAttribute;
@@ -337,4 +339,36 @@ public class ParagraphProperties {
 		// return the value
 		return inchValueString;
 	}
+	public void setBreak(String breakPosition, String breakAttribute) {
+		if (breakPosition == null) {
+			return;
+		}
+		if (breakAttribute == null) {
+			if (breakPosition.equals("before")) {
+				mElement.removeAttribute(FoBreakBeforeAttribute.ATTRIBUTE_NAME
+						.getQName());
+			} else if (breakPosition.equals("after"))
+				mElement.removeAttribute(FoBreakAfterAttribute.ATTRIBUTE_NAME
+						.getQName());
+		} else if (breakPosition.equals("before")) {
+			mElement.setFoBreakBeforeAttribute(breakAttribute);
+		} else if (breakPosition.equals("after"))
+			mElement.setFoBreakAfterAttribute(breakAttribute);
+	}
+	public String getBreakBefore() {
+		return mElement.getFoBreakBeforeAttribute();
+	}
+	public String getBreakAfter() {
+		return mElement.getFoBreakAfterAttribute();
+	}
+	public int getPageNumber() {
+		return mElement.getStylePageNumberAttribute();
+	}
+	public void setPageNumber(int pageNumber) {
+		if (pageNumber > 0) {
+			mElement.setStylePageNumberAttribute(pageNumber);
+		} else {
+			mElement.removeAttribute("style:page-number");
+		}
+	}
 }

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java Fri Feb  8 07:44:25 2013
@@ -73,6 +73,7 @@ import org.odftoolkit.simple.Component;
 import org.odftoolkit.simple.Document;
 import org.odftoolkit.simple.SpreadsheetDocument;
 import org.odftoolkit.simple.TextDocument;
+import org.odftoolkit.simple.style.DefaultStyleHandler;
 import org.odftoolkit.simple.text.Paragraph;
 import org.w3c.dom.DOMException;
 import org.w3c.dom.Node;
@@ -101,6 +102,7 @@ public class Table extends Component {
 	IdentityHashMap<TableTableCellElementBase, Vector<Cell>> mCellRepository = new IdentityHashMap<TableTableCellElementBase, Vector<Cell>>();
 	IdentityHashMap<TableTableRowElement, Vector<Row>> mRowRepository = new IdentityHashMap<TableTableRowElement, Vector<Row>>();
 	IdentityHashMap<TableTableColumnElement, Vector<Column>> mColumnRepository = new IdentityHashMap<TableTableColumnElement, Vector<Column>>();
+	private DefaultStyleHandler mStyleHandler;
 	static {
 		IN_FORMAT.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
 	}
@@ -3268,6 +3270,11 @@ public class Table extends Component {
 		}
 	}
 	
+	public DefaultStyleHandler getStyleHandler() {
+		if (mStyleHandler == null)
+			mStyleHandler = new DefaultStyleHandler(this.getOdfElement());
+		return mStyleHandler;
+	}
 	// default iterator to iterate column item.
 	private class SimpleColumnIterator implements Iterator<Column> {
 		private Table ownerTable;

Modified: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/common/navigation/TextSelectionTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/common/navigation/TextSelectionTest.java?rev=1443861&r1=1443860&r2=1443861&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/common/navigation/TextSelectionTest.java (original)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/common/navigation/TextSelectionTest.java Fri Feb  8 07:44:25 2013
@@ -32,8 +32,10 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.odftoolkit.odfdom.dom.element.OdfStyleBase;
+import org.odftoolkit.odfdom.dom.element.draw.DrawFrameElement;
 import org.odftoolkit.odfdom.dom.element.draw.DrawImageElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleTextPropertiesElement;
+import org.odftoolkit.odfdom.dom.element.table.TableTableElement;
 import org.odftoolkit.odfdom.dom.element.text.TextAElement;
 import org.odftoolkit.odfdom.dom.element.text.TextParagraphElementBase;
 import org.odftoolkit.odfdom.dom.element.text.TextSpanElement;
@@ -44,13 +46,19 @@ import org.odftoolkit.odfdom.pkg.OdfElem
 import org.odftoolkit.odfdom.pkg.OdfFileDom;
 import org.odftoolkit.simple.Document;
 import org.odftoolkit.simple.TextDocument;
+import org.odftoolkit.simple.common.TextExtractor;
 import org.odftoolkit.simple.common.field.Field;
 import org.odftoolkit.simple.common.field.Fields;
 import org.odftoolkit.simple.common.field.VariableField;
+import org.odftoolkit.simple.common.navigation.TextNavigation;
+import org.odftoolkit.simple.common.navigation.TextSelection;
+import org.odftoolkit.simple.common.navigation.TextSelectionTest;
 import org.odftoolkit.simple.draw.Image;
+import org.odftoolkit.simple.style.StyleTypeDefinitions.FontStyle;
 import org.odftoolkit.simple.table.Cell;
 import org.odftoolkit.simple.table.Table;
 import org.odftoolkit.simple.text.Paragraph;
+import org.odftoolkit.simple.text.Span;
 import org.odftoolkit.simple.utils.ResourceUtilities;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -62,6 +70,7 @@ import org.w3c.dom.NodeList;
 public class TextSelectionTest {
 
 	private static final String TEXT_FILE = "TestTextSelection.odt";
+	private static final String TEST_FILE = "TestReplaceWithBasicCases.odt";
 	private static final String TEXT_COMMENT_FILE = "TestTextSelectionComment.odt";
 	private static final String SAVE_FILE_DELETE = "TextSelectionResultDelete.odt";
 	private static final String SAVE_FILE_STYLE = "TextSelectionResultStyle.odt";
@@ -548,6 +557,33 @@ public class TextSelectionTest {
 			Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
 			Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
 		}
+		doc = (TextDocument) Document.loadDocument(ResourceUtilities
+				.getAbsolutePath(TEST_FILE));
+		search = new TextNavigation("<<target>>", doc);
+		int i = 0;
+		try {
+			while (search.hasNext()) {
+				i++;
+				TextSelection item = (TextSelection) search.nextSelection();
+				table = sourcedoc.getTableByName("Table1");
+				Table newtable = item.replaceWith(table);
+				Assert.assertNotNull(newtable);
+				Assert.assertEquals(1, newtable.getHeaderColumnCount());
+				Assert.assertEquals(1, newtable.getHeaderRowCount());
+				Assert.assertEquals(7 + 1, newtable.getRowCount());
+				Assert.assertEquals(5 + 1, newtable.getColumnCount());
+				Cell cell = newtable.getCellByPosition(1, 1);
+				Assert.assertEquals("string", cell.getValueType());
+			}
+			doc.save(ResourceUtilities
+					.newTestOutputFile("TextSelectionReplacewithTableResult-BasicCases.odt"));
+			verifyReplaceWithTableByBasicCases("TextSelectionReplacewithTableResult-BasicCases.odt");
+		} catch (Exception e) {
+			Logger.getLogger(TextSelectionTest.class.getName()).log(
+					Level.SEVERE, e.getMessage(), e);
+			Assert.fail("Failed with " + e.getClass().getName() + ": '"
+					+ e.getMessage() + "'");
+		}
 	}
 	
 	/**
@@ -556,9 +592,8 @@ public class TextSelectionTest {
 	 */
 	@Test
 	public void testReplacewithImage() throws Exception {
-		doc = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE));
+		// create source image
 		TextDocument sourcedoc = TextDocument.newTextDocument();
-		sourcedoc = TextDocument.newTextDocument();
 		Paragraph para = sourcedoc.addParagraph("helloImage");
 		Image image = Image.newImage(para, ResourceUtilities.getURI("image_list_item.png"));
 		image.setName("this image 1");
@@ -570,6 +605,7 @@ public class TextSelectionTest {
 		search = null;
 		// 6 Simple, at the middle of original Paragraph, split original
 		// Paragraph, insert before the second Paragraph.
+		doc = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE));
 		search = new TextNavigation("SIMPLE", doc);
 		int i = 0;
 		TextSelection item = null;
@@ -599,7 +635,6 @@ public class TextSelectionTest {
 			Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
 		}
 		doc = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE));
-		search = null;
 		// 6 Simple, at the middle of original Paragraph, split original
 		// Paragraph, insert before the second Paragraph.
 		search = new TextNavigation("SIMPLE", doc);
@@ -624,6 +659,59 @@ public class TextSelectionTest {
 			Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
 			Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
 		}
+		doc = (TextDocument) Document.loadDocument(ResourceUtilities
+				.getAbsolutePath(TEST_FILE));
+		search = new TextNavigation("<<target>>", doc);
+		i = 0;
+		try {
+			while (search.hasNext()) {
+				i++;
+				item = (TextSelection) search.nextSelection();
+				URI imageuri = ResourceUtilities.getURI("image_list_item.png");
+				image = item.replaceWith(imageuri);
+				Assert.assertNotNull(image);
+				if (image.getName().startsWith("replace")) {
+					Assert.assertTrue(true);
+				} else {
+					Assert.fail();
+				}
+			}
+			Assert.assertEquals(11, i);
+			doc.save(ResourceUtilities
+					.newTestOutputFile("TextSelectionReplacewithImageResult-URI-BasicCases.odt"));
+			verifyReplaceWithImageByBasicCases("TextSelectionReplacewithImageResult-URI-BasicCases.odt");
+		} catch (Exception e) {
+			Logger.getLogger(TextSelectionTest.class.getName()).log(
+					Level.SEVERE, e.getMessage(), e);
+			Assert.fail("Failed with " + e.getClass().getName() + ": '"
+					+ e.getMessage() + "'");
+		}
+		doc = (TextDocument) Document.loadDocument(ResourceUtilities
+				.getAbsolutePath(TEST_FILE));
+		search = new TextNavigation("<<target>>", doc);
+		i = 0;
+		try {
+			while (search.hasNext()) {
+				i++;
+				item = (TextSelection) search.nextSelection();
+				image = item.replaceWith(image);
+				Assert.assertNotNull(image);
+				if (image.getName().startsWith("replace")) {
+					Assert.assertTrue(true);
+				} else {
+					Assert.fail();
+				}
+			}
+			Assert.assertEquals(11, i);
+			doc.save(ResourceUtilities
+					.newTestOutputFile("TextSelectionReplacewithImageResult-BasicCases.odt"));
+			verifyReplaceWithImageByBasicCases("TextSelectionReplacewithImageResult-BasicCases.odt");
+		} catch (Exception e) {
+			Logger.getLogger(TextSelectionTest.class.getName()).log(
+					Level.SEVERE, e.getMessage(), e);
+			Assert.fail("Failed with " + e.getClass().getName() + ": '"
+					+ e.getMessage() + "'");
+		}
 	}
 	
 	/**
@@ -699,6 +787,30 @@ public class TextSelectionTest {
 			Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
 			Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
 		}
+		doc = (TextDocument) Document.loadDocument(ResourceUtilities
+				.getAbsolutePath(TEST_FILE));
+		search = new TextNavigation("<<target>>", doc);
+		i = 0;
+		try {
+			while (search.hasNext()) {
+				i++;
+				item = (TextSelection) search.nextSelection();
+				Paragraph paragraph = sourcedoc.getParagraphByIndex(1, true);
+				paragraph = item.replaceWith(paragraph);
+				Assert.assertNotNull(paragraph);
+				Assert.assertEquals(
+						TextExtractor.getText(paragraph.getOdfElement()),
+						"Hello2 from source document!");
+			}
+			doc.save(ResourceUtilities
+					.newTestOutputFile("TextSelectionReplacewithPararaphResult-BasicCases.odt"));
+			verifyReplaceWithParagraphByBasicCases("TextSelectionReplacewithPararaphResult-BasicCases.odt");
+		} catch (Exception e) {
+			Logger.getLogger(TextSelectionTest.class.getName()).log(
+					Level.SEVERE, e.getMessage(), e);
+			Assert.fail("Failed with " + e.getClass().getName() + ": '"
+					+ e.getMessage() + "'");
+		}
 	}
 	/**
 	 * Test replaceWith method of
@@ -799,4 +911,208 @@ public class TextSelectionTest {
 			Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
 		}
 	}
+	private void verifyReplaceWithTableByBasicCases(String fileName) {
+		try {
+			doc = (TextDocument) Document.loadDocument(ResourceUtilities
+					.getAbsolutePath(fileName));
+			search = new TextNavigation("#1", doc);
+			while (search.hasNext()) {
+				OdfElement description = search.nextSelection().getElement();
+				OdfElement replacement = (OdfElement) description
+						.getNextSibling();
+				Assert.assertNotNull(replacement);
+				Assert.assertTrue(replacement instanceof TableTableElement);
+			}
+			search = new TextNavigation("#2", doc);
+			while (search.hasNext()) {
+				OdfElement description = search.nextSelection().getElement();
+				String contents;
+				do {
+					OdfElement replacement = (OdfElement) description
+							.getNextSibling();
+					Assert.assertNotNull(replacement);
+					contents = TextExtractor.getText(replacement);
+					if (contents.contains("<<target>>")) {
+						Assert.fail();
+					}
+				} while (contents.startsWith("#"));
+			}
+			search = new TextNavigation("#3", doc);
+			OdfElement description = search.nextSelection().getElement();
+			Node replacement = description.getNextSibling();
+			Assert.assertTrue(replacement instanceof TableTableElement);
+			Table table = Table.getInstance((TableTableElement) replacement);
+			String breakBefore = table.getStyleHandler()
+					.getTablePropertiesForRead().getBreakBefore();
+			Assert.assertNotNull(breakBefore);
+			description = search.nextSelection().getElement();
+			replacement = description.getNextSibling();
+			Assert.assertEquals("prefix", TextExtractor
+					.getText((TextParagraphElementBase) replacement));
+			Paragraph para = Paragraph
+					.getInstanceof((TextParagraphElementBase) replacement);
+			breakBefore = para.getStyleHandler()
+					.getParagraphPropertiesForRead().getBreakBefore();
+			Assert.assertNotNull(breakBefore);
+			replacement = replacement.getNextSibling();
+			Assert.assertTrue(replacement instanceof TableTableElement);
+			table = Table.getInstance((TableTableElement) replacement);
+			breakBefore = table.getStyleHandler().getTablePropertiesForRead()
+					.getBreakBefore();
+			Assert.assertNull(breakBefore);
+			replacement = replacement.getNextSibling();
+			Assert.assertEquals("suffix", TextExtractor
+					.getText((TextParagraphElementBase) replacement));
+			para = Paragraph
+					.getInstanceof((TextParagraphElementBase) replacement);
+			breakBefore = para.getStyleHandler()
+					.getParagraphPropertiesForRead().getBreakBefore();
+			Assert.assertNull(breakBefore);
+			description = search.nextSelection().getElement();
+			replacement = description.getNextSibling();
+			Assert.assertTrue(replacement instanceof TableTableElement);
+			table = Table.getInstance((TableTableElement) replacement);
+			String masterStyle = table.getStyleHandler()
+					.getStyleElementForRead()
+					.getAttribute("style:master-page-name");
+			Assert.assertNotNull(masterStyle);
+		} catch (Exception e) {
+			Logger.getLogger(TextSelectionTest.class.getName()).log(
+					Level.SEVERE, e.getMessage(), e);
+			Assert.fail("Failed with " + e.getClass().getName() + ": '"
+					+ e.getMessage() + "'");
+		}
+	}
+	private void verifyReplaceWithImageByBasicCases(String fileName) {
+		try {
+			doc = (TextDocument) Document.loadDocument(ResourceUtilities
+					.getAbsolutePath(fileName));
+			search = new TextNavigation("#2", doc);
+			while (search.hasNext()) {
+				OdfElement description = search.nextSelection().getElement();
+				Node replacement = description.getNextSibling();
+				NodeList children = replacement.getChildNodes();
+				Assert.assertNotNull(children);
+				int count = 0;
+				for (int m = 0; m < children.getLength(); m++) {
+					if (children.item(m) instanceof DrawFrameElement) {
+						count++;
+					}
+				}
+				Assert.assertEquals(3, count);
+			}
+		} catch (Exception e) {
+			Logger.getLogger(TextSelectionTest.class.getName()).log(
+					Level.SEVERE, e.getMessage(), e);
+			Assert.fail("Failed with " + e.getClass().getName() + ": '"
+					+ e.getMessage() + "'");
+		}
+	}
+	private void verifyReplaceWithParagraphByBasicCases(String fileName) {
+		try {
+			doc = (TextDocument) Document.loadDocument(ResourceUtilities
+					.getAbsolutePath(fileName));
+			search = new TextNavigation("#1", doc);
+			while (search.hasNext()) {
+				OdfElement description = search.nextSelection().getElement();
+				OdfElement replacement = (OdfElement) description
+						.getNextSibling();
+				Assert.assertNotNull(replacement);
+				Assert.assertEquals("Hello2 from source document!",
+						TextExtractor.getText(replacement));
+			}
+			search = new TextNavigation("#2", doc);
+			while (search.hasNext()) {
+				OdfElement description = search.nextSelection().getElement();
+				String contents;
+				do {
+					OdfElement replacement = (OdfElement) description
+							.getNextSibling();
+					Assert.assertNotNull(replacement);
+					contents = TextExtractor.getText(replacement);
+					if (contents.contains("<<target>>")) {
+						Assert.fail();
+					}
+				} while (contents.startsWith("#"));
+			}
+			search = new TextNavigation("#3", doc);
+			OdfElement description = search.nextSelection().getElement();
+			TextParagraphElementBase replacement = (TextParagraphElementBase) description
+					.getNextSibling();
+			Assert.assertEquals("Hello2 from source document!",
+					TextExtractor.getText(replacement));
+			Paragraph para = Paragraph.getInstanceof(replacement);
+			String breakBefore = para.getStyleHandler()
+					.getParagraphPropertiesForRead().getBreakBefore();
+			Assert.assertNotNull(breakBefore);
+			description = search.nextSelection().getElement();
+			replacement = (TextParagraphElementBase) description
+					.getNextSibling();
+			Assert.assertEquals("prefix", TextExtractor.getText(replacement));
+			para = Paragraph.getInstanceof(replacement);
+			breakBefore = para.getStyleHandler()
+					.getParagraphPropertiesForRead().getBreakBefore();
+			Assert.assertNotNull(breakBefore);
+			replacement = (TextParagraphElementBase) replacement
+					.getNextSibling();
+			Assert.assertEquals("Hello2 from source document!",
+					TextExtractor.getText(replacement));
+			para = Paragraph.getInstanceof(replacement);
+			breakBefore = para.getStyleHandler()
+					.getParagraphPropertiesForRead().getBreakBefore();
+			Assert.assertNull(breakBefore);
+			replacement = (TextParagraphElementBase) replacement
+					.getNextSibling();
+			Assert.assertEquals("suffix", TextExtractor.getText(replacement));
+			para = Paragraph.getInstanceof(replacement);
+			breakBefore = para.getStyleHandler()
+					.getParagraphPropertiesForRead().getBreakBefore();
+			Assert.assertNull(breakBefore);
+			description = search.nextSelection().getElement();
+			replacement = (TextParagraphElementBase) description
+					.getNextSibling();
+			Assert.assertEquals("Hello2 from source document!",
+					TextExtractor.getText(replacement));
+			para = Paragraph.getInstanceof(replacement);
+			String masterStyle = para.getStyleHandler()
+					.getStyleElementForRead()
+					.getAttribute("style:master-page-name");
+			Assert.assertNotNull(masterStyle);
+		} catch (Exception e) {
+			Logger.getLogger(TextSelectionTest.class.getName()).log(
+					Level.SEVERE, e.getMessage(), e);
+			Assert.fail("Failed with " + e.getClass().getName() + ": '"
+					+ e.getMessage() + "'");
+		}
+	}
+	private void verifyReplaceWithTextDocumentByBasicCases(String fileName) {
+		try {
+			doc = (TextDocument) Document.loadDocument(ResourceUtilities
+					.getAbsolutePath(fileName));
+			search = new TextNavigation("This is the first paragraph", doc);
+			Assert.assertNotNull(search.hasNext());
+			TextParagraphElementBase pElement = (TextParagraphElementBase) search
+					.nextSelection().getElement();
+			Node nextPElement = pElement.getNextSibling();
+			Assert.assertTrue(nextPElement instanceof TextParagraphElementBase);
+			String contents = TextExtractor
+					.getText((TextParagraphElementBase) nextPElement);
+			if (!contents
+					.startsWith("This is a picture from the source document:"))
+				Assert.fail();
+			TextSpanElement spanEle = (TextSpanElement) pElement
+					.getElementsByTagName("text:span").item(0);
+			Span span = Span.getInstanceof(spanEle);
+			FontStyle fontStyle = span.getStyleHandler()
+					.getTextPropertiesForRead().getFontStyle();
+			Assert.assertEquals(FontStyle.BOLD, fontStyle);
+			Assert.assertTrue(doc.getContentDom().getDocument().getPackage()
+					.contains("Pictures/20000007000010550000107047D3E406.svm"));
+		} catch (Exception e) {
+			Logger.getLogger(TextSelectionTest.class.getName()).log(
+					Level.SEVERE, e.getMessage(), e);
+			Assert.fail("Failed with " + e.getClass().getName() + ": '"
+					+ e.getMessage() + "'");
+		}
+	}
 }

Added: incubator/odf/trunk/simple/src/test/resources/TestReplaceWithBasicCases.odt
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/resources/TestReplaceWithBasicCases.odt?rev=1443861&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/odf/trunk/simple/src/test/resources/TestReplaceWithBasicCases.odt
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/odf/trunk/simple/src/test/resources/TestTextSelectionCopySource.odt
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/resources/TestTextSelectionCopySource.odt?rev=1443861&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/odf/trunk/simple/src/test/resources/TestTextSelectionCopySource.odt
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message