david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2004 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.wst.sse.core.internal.text; |
| 14 | |
| 15 | |
| 16 | |
| 17 | import java.util.Vector; |
| 18 | |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 19 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; |
| 20 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
| 21 | import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegionList; |
david_williams | 2aecf08 | 2005-04-13 05:03:21 +0000 | [diff] [blame] | 22 | import org.eclipse.wst.sse.core.internal.util.Assert; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 23 | |
| 24 | |
| 25 | public class StructuredDocumentRegionIterator { |
| 26 | |
| 27 | public final static IStructuredDocumentRegion adjustStart(IStructuredDocumentRegion headNode, int adjustment) { |
| 28 | IStructuredDocumentRegion aNode = headNode; |
| 29 | while (aNode != null) { |
| 30 | aNode.adjustStart(adjustment); |
| 31 | aNode = aNode.getNext(); |
| 32 | } |
| 33 | return headNode; |
| 34 | } |
| 35 | |
| 36 | public final static int countRegions(IStructuredDocumentRegionList flatNodes) { |
| 37 | int result = 0; |
| 38 | if (flatNodes != null) { |
| 39 | int length = flatNodes.getLength(); |
| 40 | for (int i = 0; i < length; i++) { |
| 41 | IStructuredDocumentRegion node = flatNodes.item(i); |
| 42 | // don't know why, but we're getting null pointer exceptions |
| 43 | // in this method |
| 44 | if (node != null) { |
| 45 | result = result + node.getNumberOfRegions(); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | public final static String getText(CoreNodeList flatNodes) { |
| 53 | String result = null; |
| 54 | if (flatNodes == null) { |
| 55 | result = ""; //$NON-NLS-1$ |
| 56 | } else { |
| 57 | StringBuffer buff = new StringBuffer(); |
| 58 | //IStructuredDocumentRegion aNode = null; |
| 59 | int length = flatNodes.getLength(); |
| 60 | for (int i = 0; i < length; i++) { |
| 61 | buff.append(flatNodes.item(i).getText()); |
| 62 | } |
| 63 | result = buff.toString(); |
| 64 | } |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | public final static CoreNodeList setParentDocument(CoreNodeList nodelist, IStructuredDocument textStore) { |
| 69 | Assert.isNotNull(nodelist, "nodelist was null in CoreNodeList::setTextStore(CoreNodeList, StructuredTextStore)"); //$NON-NLS-1$ |
| 70 | int len = nodelist.getLength(); |
| 71 | for (int i = 0; i < len; i++) { |
| 72 | IStructuredDocumentRegion node = nodelist.item(i); |
| 73 | //Assert.isNotNull(node, "who's putting null in the node list? in |
| 74 | // CoreNodeList::setTextStore(CoreNodeList, |
| 75 | // StructuredTextStore)"); //$NON-NLS-1$ |
| 76 | node.setParentDocument(textStore); |
| 77 | } |
| 78 | return nodelist; |
| 79 | } |
| 80 | |
| 81 | // public final static IStructuredDocumentRegion |
| 82 | // setStructuredDocument(IStructuredDocumentRegion headNode, |
| 83 | // BasicStructuredDocument structuredDocument) { |
| 84 | // IStructuredDocumentRegion aNode = headNode; |
| 85 | // while (aNode != null) { |
| 86 | // aNode.setParentDocument(structuredDocument); |
| 87 | // aNode = (IStructuredDocumentRegion) aNode.getNext(); |
| 88 | // } |
| 89 | // return headNode; |
| 90 | // } |
| 91 | public final static IStructuredDocumentRegion setParentDocument(IStructuredDocumentRegion headNode, IStructuredDocument document) { |
| 92 | IStructuredDocumentRegion aNode = headNode; |
| 93 | while (aNode != null) { |
| 94 | aNode.setParentDocument(document); |
| 95 | aNode = aNode.getNext(); |
| 96 | } |
| 97 | return headNode; |
| 98 | } |
| 99 | |
| 100 | public final static Vector toVector(IStructuredDocumentRegion headNode) { |
| 101 | IStructuredDocumentRegion aNode = headNode; |
| 102 | Vector v = new Vector(); |
| 103 | while (aNode != null) { |
| 104 | v.addElement(aNode); |
| 105 | aNode = aNode.getNext(); |
| 106 | } |
| 107 | return v; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * |
| 112 | */ |
| 113 | private StructuredDocumentRegionIterator() { |
| 114 | } |
| 115 | } |