Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER Copyright 2009, 2010 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.common.navigation;
 
 
A derived Navigation class used to navigate the text content, which can search the document and find the matched text and return TextSelection instance.
 
 public class TextNavigation extends Navigation {
 
 	private String mMatchedElementName = "text:p,text:h";
 	private Pattern mPattern;
 	private Document mDocument;
 	private String mNextText;
 	private int mNextIndex;
 	private boolean mbFinishFindInHeaderFooter;

Construct TextNavigation with matched condition and navigation scope.

Parameters:
pattern the matched pattern String
doc the navigation scope
 
 	public TextNavigation(String patternDocument doc) {
 		 = Pattern.compile(pattern);
 		 = doc;
 		 = null;
 		 = null;
 	}

Check if has next TextSelection with satisfied content pattern.

 
 	public boolean hasNext() {
 		return ( != null);
 	}

Get next TextSelection.

 
 	public Selection nextSelection() {
 		if ( != null) {
 			 = null;
 		} else {
 		}
 		if ( == null) {
			return null;
else {
			Selection.SelectionManager.registerItem();
		}
	}

Check if the text content of element match the specified matched condition, which is stated when the TextNavigation created.

Parameters:
element navigate this element
Returns:
true if the text content of this element match this pattern; false if not match
	public boolean match(Node element) {
		if (element instanceof OdfElement) {
			WhitespaceProcessor textProcessor = new WhitespaceProcessor();
			String content = textProcessor.getText(element);
			Matcher matcher = .matcher(content);
			if (matcher.find()) {
				// check whether this container is minimum
				Node childNode = element.getFirstChild();
				while (childNode != null) {
					WhitespaceProcessor childTextProcessor = new WhitespaceProcessor();
					String childContent = childTextProcessor.getText(childNode);
					Matcher childMatcher = .matcher(childContent);
					if (childMatcher.find()) {
						if (childNode.getNodeType() == .
								|| "text:span".equalsIgnoreCase(childNode.getNodeName())
								|| "text:a".equalsIgnoreCase(childNode.getNodeName())) {
							break;
else {
							return false;
						}
else {
						childNode = childNode.getNextSibling();
					}
				}
				if (.indexOf(element.getNodeName()) != -1) {
					// here just consider \n\r\t occupy one char
					 = matcher.start();
					int eIndex = matcher.end();
					 = content.substring(eIndex);
					return true;
				}
			}
		}
		return false;
	}
	/*
	 * Return the matched text might exist in header/footer.
	 */
		OdfFileDom styledom = null;
		OdfOfficeMasterStyles masterpage = null;
		OdfElement element = null;
		if (selected != null) {
			OdfElement containerElement = selected.getContainerElement();
			int index = selected.getIndex();
			String content = TextExtractor.getText(containerElement);
			int nextIndex = -1;
			Matcher matcher = .matcher(content);
			// start from the end index of the selected item
			if (matcher.find(index + selected.getText().length())) {
				// here just consider \n\r\t occupy one char
				nextIndex = matcher.start();
				int eIndex = matcher.end();
				 = content.substring(nextIndexeIndex);
			}
			if (nextIndex != -1) {
				return createSelection(selected.getContainerElement(), nextIndex);
			}
		}
		try {
			styledom = .getStylesDom();
			NodeList list = styledom.getElementsByTagName("office:master-styles");
			if (styledom == null) {
				return null;
			}
			if (list.getLength() > 0) {
				masterpage = (OdfOfficeMasterStyleslist.item(0);
else {
				return null;
			}
			if (selected == null) {
				element = (OdfElementgetNextMatchElementInTree(masterpagemasterpage);
else {
				element = (OdfElementgetNextMatchElementInTree(selected.getContainerElement(), masterpage);
			}
			if (element != null) {
				return createSelection(element);
else {
				return null;
			}
catch (Exception ex) {
		}
		return null;
	}
	/*
	 * Found the next <code>Selection</code> start from the
	 * <code>selected</code>.
	 */
	private TextSelection findNext(TextSelection selected) {
			TextSelection styleselected = findInHeaderFooter(selected);
			if (styleselected != null) {
				return styleselected;
			}
			selected = null;
		}
		if (selected == null) {
			OdfElement element = null;
			try {
catch (Exception ex) {
			}
			if (element != null) {
				return createSelection(element);
else {
				return null;
			}
		}
		OdfElement containerElement = selected.getContainerElement();
		int index = selected.getIndex();
		String content = TextExtractor.getText(containerElement);
		int nextIndex = -1;
		Matcher matcher = .matcher(content);
		// start from the end index of the selected item
		if (((content.length() > index + selected.getText().length())) 
				&& (matcher.find(index + selected.getText().length()))) {
			// here just consider \n\r\t occupy one char
			nextIndex = matcher.start();
			int eIndex = matcher.end();
			 = content.substring(nextIndexeIndex);
		}
		if (nextIndex != -1) {
			return createSelection(selected.getContainerElement(), nextIndex);
else {
			OdfElement element = (OdfElementgetNextMatchElement((NodecontainerElement);
			if (element != null) {
				return createSelection(element);
else {
				return null;
			}
		}
	}
	/*
	 * In order to keep the consist between value and display text, spreadsheet
	 * and chart document should use <code>CellSelection</code>.
	 */
	private TextSelection createSelection(OdfElement containerElementint nextIndex) {
		TextSelection item = null;
		Node parent = containerElement.getParentNode();
		while (parent != null) {
				Cell cell = Cell.getInstance(cellElement);
				item = new CellSelection(containerElementnextIndexcell);
				break;
else {
				OdfName odfName = ((OdfElementparent).getOdfName();
				String ns = odfName.getPrefix();
				if ("text".equals(ns)) {
					parent = parent.getParentNode();
else {
					break;
				}
			}
		}
		if (item == null) {
			item = new TextSelection(containerElementnextIndex);
		}
		return item;
	}
New to GrepCode? Check out our FAQ X