Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER Copyright 2011 IBM. All rights reserved. Use is subject to license terms. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also obtain a copy of the License at http://odftoolkit.org/docs/license.txt Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package org.odftoolkit.simple.text;
 
 import java.util.List;
 
This class presents paragraph in ODF document. It provides methods to manipulate text content, and other child component under the paragraph.

Since:
0.5
 
 public class Paragraph extends Component implements TextboxContainer {
 
 
 	private Paragraph(TextPElement pElement) {
 		 = pElement;
 		 = new DefaultStyleHandler(pElement);
 		 = null;
 	}

Get a paragraph instance by an instance of TextPElement.

Parameters:
pElement - the instance of TextPElement
Returns:
an instance of paragraph
 
 	public static Paragraph getInstanceof(TextPElement pElement) {
 		if (pElement == null)
 			return null;
 
 		Paragraph para = null;
 		para = (Paragraph) Component.getComponentByElement(pElement);
 		if (para != null)
 			return para;
 
 		para = new Paragraph(pElement);
 		Component.registerComponent(parapElement);
 		return para;
 	}

Create an instance of paragraph

The paragrah will be added at the end of this container.

Parameters:
container - the paragraph container that contains this paragraph.
 
 	public static Paragraph newParagraph(ParagraphContainer container) {
 		Paragraph para = null;
 		OdfElement parent = container.getParagraphContainerElement();
 		OdfFileDom ownerDom = (OdfFileDomparent.getOwnerDocument();
 		TextPElement pEle = ownerDom.newOdfElement(TextPElement.class);
		parent.appendChild(pEle);
		para = new Paragraph(pEle);
		para.mContainer = container;
		Component.registerComponent(parapEle);
		return para;
	}

Set the text content of this paragraph.

All the existing text content of this paragraph would be removed, and then new text content would be set.

The white space characters in the content would be collapsed by default. For example, tab character would be replaced with <text:tab>, break line character would be replaced with <text:line-break>.

Parameters:
content - the text content
See also:
setTextContentNotCollapsed(java.lang.String)
	public void setTextContent(String content) {
		if (content != null && !content.equals(""))
			appendTextElements(contenttrue);
	}

Remove the text content of this paragraph.

The other child elements except text content will not be removed.

	public void removeTextContent() {
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node;
			node = nodeList.item(i);
			if (node.getNodeType() == .){
				//element removed need reset index.
				i--;
			}else if (node.getNodeType() == .) {
				String nodename = node.getNodeName();
				if (nodename.equals("text:s") || nodename.equals("text:tab") || nodename.equals("text:line-break")){
					//element removed need reset index.
					i--;
				}
			}
		}
	}

Return the text content of this paragraph.

The other child elements except text content will not be returned.

Returns:
- the text content of this paragraph
	public String getTextContent() {
		StringBuffer buffer = new StringBuffer();
		int i;
		for (i = 0; i < nodeList.getLength(); i++) {
			Node node;
			node = nodeList.item(i);
			if (node.getNodeType() == .)
				buffer.append(node.getNodeValue());
			else if (node.getNodeType() == .) {
				if (node.getNodeName().equals("text:s")) {
					int count = ((TextSElementnode).getTextCAttribute();
					for (int j = 0; j < countj++)
						buffer.append(' ');
else if (node.getNodeName().equals("text:tab"))
					buffer.append('\t');
				else if (node.getNodeName().equals("text:line-break")) {
					String lineseperator = System.getProperty("line.separator");
					buffer.append(lineseperator);
				}
			}
		}
		return buffer.toString();
	}

Set the text content of this paragraph.

All the existing text content of this paragraph would be removed, and then new text content would be set.

The white space characters in the content would not be collapsed.

Parameters:
content - the text content
See also:
setTextContent(java.lang.String)
	public void setTextContentNotCollapsed(String content) {
		if (content != null && !content.equals(""))
			appendTextElements(contentfalse);
	}
	private void appendTextElements(String contentboolean isWhitespaceCollapsed) {
		if (isWhitespaceCollapsed) {
			int i = 0, length = content.length();
			String str = "";
			while (i < length) {
				char ch = content.charAt(i);
				if (ch == ' ') {
					int j = 1;
					i++;
					while ((i < length) && (content.charAt(i) == ' ')) {
						j++;
						i++;
					}
					if (j == 1) {
						str += ' ';
else {
						str += ' ';
						str = "";
						spaceElement.setTextCAttribute(j - 1);
					}
else if (ch == '\n') {
					if (str.length() > 0) {
						str = "";
					}
					i++;
else if (ch == '\t') {
					if (str.length() > 0) {
						str = "";
					}
					i++;
else if (ch == '\r') {
					i++;
else {
					str += ch;
					i++;
				}
			}
			if (str.length() > 0) {
			}
else {
		}
	}

Append the text content at the end of this paragraph.

The white space characters in the content would be collapsed by default. For example, tab character would be replaced with <text:tab>, break line character would be replaced with <text:line-break>.

Parameters:
content - the text content
See also:
appendTextContentNotCollapsed(java.lang.String)
	public void appendTextContent(String content) {
		if (content != null && !content.equals(""))
			appendTextElements(contenttrue);
	}

Append the text content at the end of this paragraph.

The white space characters in the content would not be collapsed.

Parameters:
content - the text content
See also:
appendTextContent(java.lang.String)
	public void appendTextContentNotCollapsed(String content) {
		if (content != null && !content.equals(""))
			appendTextElements(contentfalse);
	}

Set the style name of this paragraph

Parameters:
styleName - the style name
	public void setStyleName(String styleName) {
	}

Get the style name of this paragraph

Returns:
- the style name
	public String getStyleName() {
		if (style == null) {
			return "";
		}
		if (style instanceof OdfStyle)
			return ((OdfStylestyle).getStyleNameAttribute();
		else
			return "";
	}

Get the owner document of this paragraph

Returns:
the document who owns this paragraph
	}

Get the style handler of this paragraph.

The style handler is an instance of DefaultStyleHandler

Returns:
an instance of DefaultStyleHandler
See also:
org.odftoolkit.simple.style.DefaultStyleHandler
		if ( != null)
		else {
		}
	}

Return the instance of "text:p" element

Returns:
the instance of "text:p" element
	}
	public Textbox addTextbox() {
	}
	}
	public boolean removeTextbox(Textbox box) {
	}
	}
	public Textbox addTextbox(FrameRectangle position) {
	}
	public Textbox getTextboxByName(String name) {
	}
		throw new UnsupportedOperationException("this method is not supported by paragraph.");
	}
		}
	}
		if ( == null)
	}
New to GrepCode? Check out our FAQ X