incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liud...@apache.org
Subject svn commit: r1406045 - in /incubator/odf/trunk/simple/src: main/java/org/odftoolkit/simple/TextDocument.java test/java/org/odftoolkit/simple/TextDocumentTest.java
Date Tue, 06 Nov 2012 05:49:40 GMT
Author: liudali
Date: Tue Nov  6 05:49:40 2012
New Revision: 1406045

URL: http://svn.apache.org/viewvc?rev=1406045&view=rev
Log:
Fix for ODFTOOLKIT-339 [Simpe]Add new methods in TextDocument to suport insert a d ocument
into an open document 
https://issues.apache.org/jira/browse/ODFTOOLKIT-339

Modified:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/TextDocumentTest.java

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=1406045&r1=1406044&r2=1406045&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 Tue Nov
 6 05:49:40 2012
@@ -1364,4 +1364,86 @@ public class TextDocument extends Docume
 		}
 		return null;
 	}
+	
+	private OdfElement insertOdfElementwithoutstyle(OdfElement referenceOdfElement,Document
sourceDocument ,OdfElement sourceOdfElement,boolean before) {
+		try {			 
+			OdfElement newOdfElement = (OdfElement) sourceOdfElement.cloneNode(true);
+			newOdfElement = (OdfElement)  cloneForeignElement(newOdfElement, getContentDom(), true);
+			if (before) {
+				referenceOdfElement.getParentNode().insertBefore(newOdfElement, referenceOdfElement);
+			} else {
+				// Insert newOdfElement after the referenceOdfElement
+				Node refNextNode = referenceOdfElement.getNextSibling();
+				if (refNextNode == null) {
+					referenceOdfElement.getParentNode().appendChild(newOdfElement);
+				} else {
+					referenceOdfElement.getParentNode().insertBefore(newOdfElement, refNextNode);
+				}
+			}
+			return newOdfElement;
+		} catch (Exception e) {
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE, null, e);
+		}
+		return null;
+	}
+	
+	/**
+	 * Copy text content of the source TextDocument and insert it to the current TextDocument
+	 * after the reference Paragraph, with Styles or without Styles.
+	 * 
+	 * @param sourceDocument
+	 *            the source TextDocument
+	 * @param referenceParagraph
+	 *  		  where the text content of the source TextDocument be inserted 
+	 * @param isCopyStyle
+	 *            true:copy the styles in source document to current TextDocment. 
+	 *            false:don't copy the styles in source document to current TextDocment. 
+	 */
+	public void insertContentFromDocumentAfter(TextDocument sourceDocument, Paragraph referenceParagraph,
boolean isCopyStyle){
+		try {
+			OfficeTextElement sroot = sourceDocument.getContentRoot();
+			NodeList clist = sroot.getChildNodes();
+			for (int i=(clist.getLength()-1); i>=0; i--) {
+				OdfElement node = (OdfElement) clist.item(i);
+				if(isCopyStyle){
+					insertOdfElement(referenceParagraph.getOdfElement(), sourceDocument, node, false);
+				}
+				else {
+					insertOdfElementwithoutstyle(referenceParagraph.getOdfElement(), sourceDocument, node,
false);
+				}
+			}
+			
+		} catch (Exception e) {
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE, null, e);
+		}
+	}
+	/**
+	 * Copy text content of the source TextDocument and insert it to the current TextDocument
+	 * before the reference Paragraph, with Styles or without Styles.
+	 * 
+	 * @param srcDoc
+	 *            the source TextDocument
+	 * @param referenceParagraph
+	 *  		  where the text content of the source TextDocument be inserted 
+	 * @param isCopyStyle
+	 *            true:copy the styles in source document to current TextDocment. 
+	 *            false:don't copy the styles in source document to current TextDocment. 
+	 */
+	public void insertContentFromDocumentBefore(TextDocument sourceDocument, Paragraph referenceParagraph,
boolean isCopyStyle){
+		try {
+			OfficeTextElement sroot = sourceDocument.getContentRoot();
+			NodeList clist = sroot.getChildNodes();
+			for (int i = 0; i < clist.getLength(); i++) {
+				OdfElement node = (OdfElement) clist.item(i);
+				if(isCopyStyle){
+					insertOdfElement(referenceParagraph.getOdfElement(), sourceDocument, node, true);
+				}
+				else {
+					insertOdfElementwithoutstyle(referenceParagraph.getOdfElement(), sourceDocument, node,
true);
+				}
+			}
+		} catch (Exception e) {
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE, null, e);
+		}
+	}
 }

Modified: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/TextDocumentTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/TextDocumentTest.java?rev=1406045&r1=1406044&r2=1406045&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/TextDocumentTest.java (original)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/TextDocumentTest.java Tue
Nov  6 05:49:40 2012
@@ -412,11 +412,260 @@ public class TextDocumentTest {
 		}
 		Assert.assertEquals(expected, value);
 	}
+	
+	@Test
+	public void testAddPagebreakWithMasterPage() {
+		try {
+
+			TextDocument newDoc = TextDocument.newTextDocument();
+
+			// create a new master page
+			Paragraph paragraph = newDoc
+					.addParagraph("before page break - original Landscape");
+			MasterPage master1 = MasterPage.getOrCreateMasterPage(newDoc,
+					"Landscape");
+			master1.setPageWidth(279.4);
+			master1.setPageHeight(215.9);
+			master1.setNumberFormat(NumberFormat.HINDU_ARABIC_NUMBER.toString());
+			master1.setPrintOrientation(PrintOrientation.LANDSCAPE);
+			master1.setFootnoteMaxHeight(0);
+			master1.setWritingMode(StyleTypeDefinitions.WritingMode.LRTB);
+			master1.setMargins(20, 20, 20, 20);
+			master1.setFootnoteSepProperties(AdjustmentStyle.LEFT, Color
+					.valueOf("#000000"), 1, 1, null, Percent.valueOf("25%"),
+					0.18);
+			newDoc.addPageBreak(paragraph, master1);
+			newDoc.addParagraph("after page break - original Landscape");
+			validMasterPageApplied(newDoc, paragraph, master1);
+			newDoc
+					.save(ResourceUtilities
+							.newTestOutputFile("AddPageBreakWithMasterPageOutput1.odt"));
+
+			// modify the master page
+			paragraph = newDoc
+					.addParagraph("before page break - modified Landscape");
+			MasterPage master2 = MasterPage.getOrCreateMasterPage(newDoc,
+					"Landscape");
+			master2.setPageWidth(100);
+			master2.setPageHeight(300);
+			master2.setNumberFormat(null);
+			master2.setPrintOrientation(null);
+			master2.setMargins(20, 20, 0, 0);
+			newDoc.addPageBreak(paragraph, master2);
+			newDoc.addParagraph("after page break - modified Landscape");
+			validMasterPageApplied(newDoc, paragraph, master2);
+
+			// apply existing master page
+			paragraph = newDoc.addParagraph("before page break - Standard");
+			MasterPage master3 = MasterPage.getOrCreateMasterPage(newDoc,
+					"Standard");
+			newDoc.addPageBreak(paragraph, master3);
+			newDoc.addParagraph("after page break - Standard");
+			newDoc.addParagraph("end page");
+			newDoc
+					.save(ResourceUtilities
+							.newTestOutputFile("AddPageBreakWithMasterPageOutput2.odt"));
+		} catch (Exception e) {
+			Logger.getLogger(TextDocumentTest.class.getName()).log(
+					Level.SEVERE, null, e);
+			Assert.fail();
+		}
+	}
+
+	private void validMasterPageApplied(TextDocument newDoc,
+			Paragraph paragraph, MasterPage masterPage) throws Exception {
+		Node paragraphNode = paragraph.getOdfElement().getNextSibling();
+		Assert.assertTrue(paragraphNode instanceof TextPElement);
+
+		// check paragraph style
+		OdfOfficeAutomaticStyles styles = newDoc.getContentDom()
+				.getAutomaticStyles();
+		OdfStyle style = styles.getStyle(((TextPElement) paragraphNode)
+				.getStyleName(), OdfStyleFamily.Paragraph);
+		Assert.assertNotNull(style);
+
+		// check master page style
+		String masterName = style.getStyleMasterPageNameAttribute();
+		StyleMasterPageElement master = newDoc.getOfficeMasterStyles()
+				.getMasterPage(masterName);
+		Assert.assertEquals(masterPage.getName(), masterName);
+		Assert.assertNotNull(master);
+
+		// check page layout style
+		String pageLayoutName = master.getStylePageLayoutNameAttribute();
+		OdfStylePageLayout pageLayout = master.getAutomaticStyles()
+				.getPageLayout(pageLayoutName);
+		Assert.assertNotNull(pageLayout);
+
+		// check page layout properties
+		StylePageLayoutPropertiesElement properties = (StylePageLayoutPropertiesElement) pageLayout
+				.getPropertiesElement(OdfStylePropertiesSet.PageLayoutProperties);
+		Assert.assertNotNull(properties);
+		// page width
+		checkDoubleValue(masterPage.getPageWidth(), properties
+				.getFoPageWidthAttribute());
+		// page height
+		checkDoubleValue(masterPage.getPageHeight(), properties
+				.getFoPageHeightAttribute());
+		// footnote max height
+		checkDoubleValue(masterPage.getFootnoteMaxHeight(), properties
+				.getStyleFootnoteMaxHeightAttribute());
+		// margins
+		checkDoubleValue(masterPage.getMarginTop(), properties
+				.getFoMarginTopAttribute());
+		checkDoubleValue(masterPage.getMarginBottom(), properties
+				.getFoMarginBottomAttribute());
+		checkDoubleValue(masterPage.getMarginLeft(), properties
+				.getFoMarginLeftAttribute());
+		checkDoubleValue(masterPage.getMarginRight(), properties
+				.getFoMarginRightAttribute());
+		// writing mode
+		checkStringValue(masterPage.getWritingMode(), properties
+				.getStyleWritingModeAttribute());
+		// number format
+		checkStringValue(masterPage.getNumberFormat(), properties
+				.getStyleNumFormatAttribute());
+		// print orientation
+		checkStringValue(masterPage.getPrintOrientation(), properties
+				.getStylePrintOrientationAttribute());
+
+		// check footnote separator line
+		StyleFootnoteSepElement footnoteSep = (StyleFootnoteSepElement) properties
+				.getElementsByTagName("style:footnote-sep").item(0);
+		Assert.assertNotNull(footnoteSep);
+		checkStringValue(masterPage.getFootnoteSepAdjustment(), footnoteSep
+				.getStyleAdjustmentAttribute());
+		checkStringValue(masterPage.getFootnoteSepColor(), footnoteSep
+				.getStyleColorAttribute());
+		checkStringValue(masterPage.getFootnoteSepLineStyle(), footnoteSep
+				.getStyleLineStyleAttribute());
+		checkDoubleValue(masterPage.getFootnoteSepDistanceAfterSep(),
+				footnoteSep.getStyleDistanceAfterSepAttribute());
+		checkDoubleValue(masterPage.getFootnoteSepDistanceBeforeSep(),
+				footnoteSep.getStyleDistanceBeforeSepAttribute());
+		checkDoubleValue(masterPage.getFootnoteSepThickness(), footnoteSep
+				.getStyleWidthAttribute());
+		checkDoubleValue(masterPage.getFootnoteSepWidth(), footnoteSep
+				.getStyleRelWidthAttribute());
+	}
+
+	private void checkStringValue(String expected, String actual) {
+		if (expected == null) {
+			Assert.assertNull(actual);
+		} else if (expected.equals("page")) {
+			Assert.assertTrue(actual == null || actual.equals(expected));
+		} else {
+			Assert.assertEquals(expected, actual);
+		}
+	}
+
+	private void checkDoubleValue(double expected, String actual) {
+		double value = 0;
+		if (actual != null) {
+			if (Percent.isValid(actual)) {
+				value = Percent.valueOf(actual).doubleValue();
+			} else {
+				value = Length.parseDouble(actual, Unit.MILLIMETER);
+			}
+		}
+		Assert.assertEquals(expected, value);
+	}
 
 	@Test
 	public void testAddComment() {
 		try {
 			// test new creation document.
+	}
+	
+	@Test
+	public void testinsertContentFromDocumentBefore() throws Exception {
+		TextDocument src1 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		TextDocument src2 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		TextDocument target1 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		TextDocument target2 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		Paragraph p1 = target1.getParagraphByIndex(2, true);
+		target1.insertContentFromDocumentBefore(src1, p1, true);
+		Iterable<OdfStyle> pstyles1 = target1.getStylesDom().getOfficeStyles().getStylesForFamily(OdfStyleFamily.Paragraph);
+		Iterator<OdfStyle> ite = pstyles1.iterator();
+		int i=0;
+		System.out.println("Custom Styles in TestInsertDocumentWithStyleBefore.odt");
+		while(ite.hasNext()){
+			OdfStyle odfStyle = ite.next();
+			String sname = odfStyle.getAttribute("style:name");
+			if(sname.startsWith("style"))
+			{
+				i++;
+				System.out.println(sname);
+			}
+		}	
+		Assert.assertEquals(6, i);
+		target1.save(ResourceUtilities.newTestOutputFile("TestInsertDocumentWithStyleBefore.odt"));
+		
+		target2 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		Paragraph p2 = target2.getParagraphByIndex(2, true);
+		target2.insertContentFromDocumentBefore(src2, p2, false);
+		
+		Iterable<OdfStyle> pstyles2 = target2.getStylesDom().getOfficeStyles().getStylesForFamily(OdfStyleFamily.Paragraph);
+		Iterator<OdfStyle> ite2 = pstyles2.iterator();
+		i=0;
+		System.out.println("Custom Styles in TestInsertDocumentWithoutStyleBefore.odt");
+		while(ite2.hasNext()){
+			OdfStyle odfStyle = ite2.next();
+			String sname = odfStyle.getAttribute("style:name");
+			if(sname.startsWith("style"))
+			{
+				i++;
+				System.out.println(sname);
+			}
+		}	
+		Assert.assertEquals(3, i);
+		target2.save(ResourceUtilities.newTestOutputFile("TestInsertDocumentWithoutStyleBefore.odt"));
+	}
+	@Test
+	public void testinsertContentFromDocumentAfter() throws Exception {
+		TextDocument src1 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		TextDocument src2 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		TextDocument target1 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		TextDocument target2 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		Paragraph p1 = target1.getParagraphByIndex(2, true);
+		target1.insertContentFromDocumentAfter(src1, p1, true);
+		Iterable<OdfStyle> pstyles1 = target1.getStylesDom().getOfficeStyles().getStylesForFamily(OdfStyleFamily.Paragraph);
+		Iterator<OdfStyle> ite = pstyles1.iterator();
+		int i=0;
+		System.out.println("Custom Styles in TestInsertDocumentWithStyleAfter.odt:");
+		while(ite.hasNext()){
+			OdfStyle odfStyle = ite.next();
+			String sname = odfStyle.getAttribute("style:name");
+			if(sname.startsWith("style"))
+			{
+				i++;
+				System.out.println(sname);
+			}
+		}	
+		Assert.assertEquals(6, i);
+		target1.save(ResourceUtilities.newTestOutputFile("TestInsertDocumentWithStyleAfter.odt"));
+		
+		
+		
+		target2 = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("TestInsertDocument.odt"));
+		Paragraph p2 = target2.getParagraphByIndex(2, true);
+		target2.insertContentFromDocumentAfter(src2, p2, false);
+		
+		Iterable<OdfStyle> pstyles2 = target2.getStylesDom().getOfficeStyles().getStylesForFamily(OdfStyleFamily.Paragraph);
+		Iterator<OdfStyle> ite2 = pstyles2.iterator();
+		i=0;
+		System.out.println("Custom Styles in TestInsertDocumentWithoutStyleAfter.odt:");
+		while(ite2.hasNext()){
+			OdfStyle odfStyle = ite2.next();
+			String sname = odfStyle.getAttribute("style:name");
+			if(sname.startsWith("style"))
+			{
+				i++;
+				System.out.println(sname);
+			}
+		}	
+		Assert.assertEquals(3, i);
+		target2.save(ResourceUtilities.newTestOutputFile("TestInsertDocumentWithoutStyleAfter.odt"));
 			TextDocument newDoc = TextDocument.newTextDocument();
 			
 			Paragraph paragraph = newDoc.addParagraph("Paragraph1");



Mime
View raw message