View Javadoc

1   package org.cateproject.controller.upload;
2   
3   import java.io.File;
4   
5   import org.junit.Test;
6   import org.odftoolkit.odfdom.doc.OdfTextDocument;
7   import org.odftoolkit.odfdom.doc.office.OdfOfficeStyles;
8   import org.odftoolkit.odfdom.doc.office.OdfOfficeText;
9   import org.odftoolkit.odfdom.doc.style.OdfDefaultStyle;
10  import org.odftoolkit.odfdom.doc.style.OdfStyle;
11  import org.odftoolkit.odfdom.doc.text.OdfTextHeading;
12  import org.odftoolkit.odfdom.doc.text.OdfTextParagraph;
13  import org.odftoolkit.odfdom.doc.text.OdfTextSection;
14  import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
15  import org.w3c.dom.Node;
16  
17  
18  public class OdfUploadTest {
19  	
20  	@Test
21  	public void testCreateOdfDocument() throws Exception {
22  		OdfTextDocument document = OdfTextDocument.newTextDocument(); 
23  		
24  		OdfOfficeText root = document.getContentRoot();
25  		OdfOfficeStyles styles = document.getOrCreateDocumentStyles();
26  		OdfStyle sectionStyle = styles.newStyle("8db4259e-3fed-4935-a2a4-9ba2bbb87fbb", OdfStyleFamily.Paragraph);
27  		sectionStyle.setStyleDisplayNameAttribute("Description");
28  		
29  		OdfTextSection section = new OdfTextSection(document.getContentDom());
30  
31  		OdfTextHeading heading = new OdfTextHeading(document.getContentDom());
32  		heading.addContent("Description");
33  		
34  		section.appendChild(heading);
35  		
36  		OdfTextParagraph paragraph = new OdfTextParagraph(document.getContentDom());
37  		paragraph.addContent("Insert Content Here");
38  		paragraph.setStyleName("8db4259e-3fed-4935-a2a4-9ba2bbb87fbb");
39  		section.appendChild(paragraph);
40  		
41  		root.appendChild(section);
42  		System.out.println(section);
43  		
44  		document.save(new File("./target/test.odt"));
45  	}
46  	
47  	@Test
48  	public void testReadOdfDocument() throws Exception {
49  		OdfTextDocument document = (OdfTextDocument) OdfTextDocument.loadDocument(new File("./src/test/resources/org/cateproject/controller/upload/edited.odt"));
50  		OdfOfficeText root = document.getContentRoot();
51  		for(int i = 0; i < root.getChildNodes().getLength(); i++){
52  		  Node node = root.getChildNodes().item(i);
53  		  System.out.println(i + " " + node);
54  		  if(node instanceof OdfTextSection) {
55  			  OdfTextSection section = (OdfTextSection)node;
56  		      System.out.println(section.getStyleName() + " " + section.toString());
57  		  }
58  		}
59  	}
60  
61  }