poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject svn commit: r819469 - in /poi/trunk: src/ooxml/java/org/apache/poi/xwpf/usermodel/ src/ooxml/testcases/org/apache/poi/xwpf/usermodel/ test-data/document/
Date Mon, 28 Sep 2009 07:42:51 GMT
Author: paolo
Date: Mon Sep 28 07:42:50 2009
New Revision: 819469

URL: http://svn.apache.org/viewvc?rev=819469&view=rev
Log:
Table of Contents first implementation

Added:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeadings.java
    poi/trunk/test-data/document/heading123.docx   (with props)
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java

Added: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java?rev=819469&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java (added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java Mon Sep 28 07:42:50 2009
@@ -0,0 +1,106 @@
+package org.apache.poi.xwpf.usermodel;
+
+import java.math.BigInteger;
+
+import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute.Space;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentBlock;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtEndPr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtPr;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabs;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFldCharType;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabTlc;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTheme;
+
+public class TOC {
+	
+	CTSdtBlock block;
+	
+	public TOC() {
+		this(CTSdtBlock.Factory.newInstance());
+	}
+	
+	public TOC(CTSdtBlock block) {
+		this.block = block;
+		CTSdtPr sdtPr = block.addNewSdtPr();
+		CTDecimalNumber id = sdtPr.addNewId();
+		id.setVal(new BigInteger("4844945"));
+		sdtPr.addNewDocPartObj().addNewDocPartGallery().setVal("Table of contents");
+		CTSdtEndPr sdtEndPr = block.addNewSdtEndPr();
+		CTRPr rPr = sdtEndPr.addNewRPr();
+		CTFonts fonts = rPr.addNewRFonts();
+		fonts.setAsciiTheme(STTheme.MINOR_H_ANSI);
+		fonts.setEastAsiaTheme(STTheme.MINOR_H_ANSI);
+		fonts.setHAnsiTheme(STTheme.MINOR_H_ANSI);
+		fonts.setCstheme(STTheme.MINOR_BIDI);
+		rPr.addNewB().setVal(STOnOff.OFF);
+		rPr.addNewBCs().setVal(STOnOff.OFF);
+		rPr.addNewColor().setVal("auto");
+		rPr.addNewSz().setVal(new BigInteger("24"));
+		rPr.addNewSzCs().setVal(new BigInteger("24"));
+		CTSdtContentBlock content = block.addNewSdtContent();
+		CTP p = content.addNewP();
+		p.setRsidR("00EF7E24".getBytes());
+		p.setRsidRDefault("00EF7E24".getBytes());
+		p.addNewPPr().addNewPStyle().setVal("TOCHeading");
+		p.addNewR().addNewT().set("Table of Contents");
+	}
+	
+	public CTSdtBlock getBlock() {
+		return this.block;
+	}
+	
+	public void addRow(int level, String title, int page, String bookmarkRef) {
+		CTSdtContentBlock contentBlock = this.block.getSdtContent();
+		CTP p = contentBlock.addNewP();
+		p.setRsidR("00EF7E24".getBytes());
+		p.setRsidRDefault("00EF7E24".getBytes());
+		CTPPr pPr = p.addNewPPr();
+		pPr.addNewPStyle().setVal("TOC" + level);
+		CTTabs tabs = pPr.addNewTabs();
+		CTTabStop tab = tabs.addNewTab();
+		tab.setVal(STTabJc.RIGHT);
+		tab.setLeader(STTabTlc.DOT);
+		tab.setPos(new BigInteger("8290"));
+		pPr.addNewRPr().addNewNoProof();
+		CTR run = p.addNewR();
+		run.addNewRPr().addNewNoProof();
+		run.addNewT().set(title);
+		run = p.addNewR();
+		run.addNewRPr().addNewNoProof();
+		run.addNewTab();
+		run = p.addNewR();
+		run.addNewRPr().addNewNoProof();
+		run.addNewFldChar().setFldCharType(STFldCharType.BEGIN);
+		// pageref run
+		run = p.addNewR();
+		run.addNewRPr().addNewNoProof();
+		CTText text = run.addNewInstrText();
+		text.setSpace(Space.PRESERVE);
+		// bookmark reference
+		text.set(" PAGEREF _Toc" + bookmarkRef + " \\h ");
+		p.addNewR().addNewRPr().addNewNoProof();
+		run = p.addNewR();
+		run.addNewRPr().addNewNoProof();
+		run.addNewFldChar().setFldCharType(STFldCharType.SEPARATE);
+		// page number run
+		run = p.addNewR();
+		run.addNewRPr().addNewNoProof();
+		run.addNewT().set(new Integer(page).toString());
+		run = p.addNewR();
+		run.addNewRPr().addNewNoProof();
+		run.addNewFldChar().setFldCharType(STFldCharType.END);
+		
+	}
+
+}

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java?rev=819469&r1=819468&r2=819469&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java Mon Sep 28 07:42:50
2009
@@ -371,5 +371,24 @@
     public XWPFTable createTable(int rows, int cols) {
 	return new XWPFTable(this, ctDocument.getBody().addNewTbl(), rows, cols);
     }
+    
+    public void createTOC() {
+    	CTSdtBlock block = this.getDocument().getBody().addNewSdt();
+    	TOC toc = new TOC(block);
+    	int i = 1;
+    	for (Iterator<XWPFParagraph> iterator = getParagraphsIterator() ; iterator.hasNext()
; ) {
+    		XWPFParagraph par = iterator.next();
+    		String parStyle = par.getStyle();
+    		if (parStyle != null && parStyle.substring(0, 7).equals("Heading")) {
+    			try {
+    				int level = new Integer(parStyle.substring("Heading".length()));
+        			toc.addRow(level, par.getText(), 1, "112723803");
+    			}
+    			catch (NumberFormatException e) {
+    				e.printStackTrace();
+    			}
+    		}
+    	}
+    }
 }
 

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java?rev=819469&r1=819468&r2=819469&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java Mon Sep 28 07:42:50
2009
@@ -939,6 +939,27 @@
     }
 
     /**
+     * This method provides a style to the paragraph
+     * This is useful when, e.g. an Heading style has to be assigned
+     * @param newStyle
+     */
+    public void setStyle(String newStyle) {
+    	CTPPr pr = getCTPPr();
+    	CTString style = pr.getPStyle() != null ? pr.getPStyle() : pr.addNewPStyle();
+    	style.setVal(newStyle);
+    }
+    
+    /**
+     * Gets the style of the paragraph
+     * @return
+     */
+    public String getStyle() {
+    	CTPPr pr = getCTPPr();
+    	CTString style = pr.isSetPStyle() ? pr.getPStyle() : null;
+    	return style != null ? style.getVal() : null;
+    }
+
+    /**
      * Get a <b>copy</b> of the currently used CTPBrd, if none is used, return
      * a new instance.
      */

Added: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeadings.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeadings.java?rev=819469&view=auto
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeadings.java (added)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeadings.java Mon
Sep 28 07:42:50 2009
@@ -0,0 +1,44 @@
+package org.apache.poi.xwpf.usermodel;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.xwpf.XWPFTestDataSamples;
+import org.apache.xmlbeans.XmlException;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
+
+public class TestXWPFHeadings extends TestCase{
+	private static final String HEADING1 = "Heading1";
+	
+	public void testSetParagraphStyle() throws IOException, XmlException, InvalidFormatException
{
+        //new clean instance of paragraph
+		XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("heading123.docx");
+        XWPFParagraph p = doc.createParagraph();
+        XWPFRun run = p.createRun();
+        run.setText("Heading 1");
+        
+        CTSdtBlock block = doc.getDocument().getBody().addNewSdt();
+        
+        assertNull(p.getStyle());
+        p.setStyle(HEADING1);
+        assertEquals(HEADING1, p.getCTP().getPPr().getPStyle().getVal());
+        
+        doc.createTOC();
+        
+//        CTStyles styles = doc.getStyle();
+//        CTStyle style = styles.addNewStyle();
+//        style.setType(STStyleType.PARAGRAPH);
+//        style.setStyleId("Heading1");
+        
+        File file = new File("/Users/paolomoz/Desktop/testHeaders.docx");
+        OutputStream out = new FileOutputStream(file);
+        doc.write(out);
+        out.close();
+    }
+
+}

Added: poi/trunk/test-data/document/heading123.docx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/document/heading123.docx?rev=819469&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/document/heading123.docx
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message