tracesrv/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/source/SourceSearch.java
changeset 56 aa2539c91954
parent 41 838cdffd57ce
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 * Base class for source search classes
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.source;
       
    20 
       
    21 import java.util.List;
       
    22 
       
    23 /**
       
    24  * Base class for source search classes.
       
    25  * 
       
    26  */
       
    27 public abstract class SourceSearch {
       
    28 
       
    29 	/**
       
    30 	 * Source parser
       
    31 	 */
       
    32 	protected final SourceParser parser;
       
    33 
       
    34 	/**
       
    35 	 * Current index within data during search
       
    36 	 */
       
    37 	protected int searchIndex;
       
    38 
       
    39 	/**
       
    40 	 * End index for search
       
    41 	 */
       
    42 	protected int searchEnd;
       
    43 
       
    44 	/**
       
    45 	 * Index of the next excluded area during search.
       
    46 	 */
       
    47 	protected int searchExcludedIndex;
       
    48 
       
    49 	/**
       
    50 	 * Search flags
       
    51 	 */
       
    52 	protected int flags;
       
    53 
       
    54 	/**
       
    55 	 * Creates a new search
       
    56 	 * 
       
    57 	 * @param parser
       
    58 	 *            the parser containing the source
       
    59 	 * @param startOffset
       
    60 	 *            offset to the start of search
       
    61 	 * @param endOffset
       
    62 	 *            offset to end of search or -1 for rest of the document
       
    63 	 * @param flags
       
    64 	 *            search flags
       
    65 	 */
       
    66 	protected SourceSearch(SourceParser parser, int startOffset, int endOffset,
       
    67 			int flags) {
       
    68 		this.parser = parser;
       
    69 		this.flags = flags;
       
    70 		resetSearch(startOffset, endOffset);
       
    71 	}
       
    72 
       
    73 	/**
       
    74 	 * Resets the current search with a new offset
       
    75 	 * 
       
    76 	 * @param startOffset
       
    77 	 *            the offset to the start of search
       
    78 	 * @param endOffset
       
    79 	 *            offset to end of search or -1 for rest of the document
       
    80 	 */
       
    81 	public void resetSearch(int startOffset, int endOffset) {
       
    82 		searchIndex = startOffset;
       
    83 		searchEnd = endOffset == -1 ? parser.getDataLength() : endOffset;
       
    84 		// Calculates the starting position for the search based on the
       
    85 		// offset and excluded areas
       
    86 		int excluded = parser.findExcludedAreaIndex(startOffset);
       
    87 		if (excluded >= 0) {
       
    88 			SourceExcludedArea area = parser.getExcludedAreas().get(excluded);
       
    89 			searchIndex = area.getOffset() + area.getLength();
       
    90 			if (excluded < parser.getExcludedAreas().size() - 1) {
       
    91 				searchExcludedIndex = excluded + 1;
       
    92 			} else {
       
    93 				searchExcludedIndex = -1;
       
    94 			}
       
    95 		} else {
       
    96 			// If binarySearch returns < 0, startIndex was between some
       
    97 			// excluded areas.
       
    98 			searchExcludedIndex = -1 - excluded;
       
    99 			if (searchExcludedIndex > parser.getExcludedAreas().size()) {
       
   100 				searchExcludedIndex = -1;
       
   101 			}
       
   102 		}
       
   103 		skipNonExcludedAreas();
       
   104 	}
       
   105 
       
   106 	/**
       
   107 	 * Changes the index to point behind an excluded area if current index is
       
   108 	 * within one.
       
   109 	 */
       
   110 	protected void skipExcludedArea() {
       
   111 		// If data index is within an excluded area, it is moved to end of
       
   112 		// it and the next excluded area is selected
       
   113 		boolean changedExcludedArea;
       
   114 		do {
       
   115 			// If the excluded area changes, the flag is set and the next
       
   116 			// excluded area is also checked. Otherwise the excluded area index
       
   117 			// gets left behind if the search index jumps past multiple excluded
       
   118 			// areas
       
   119 			changedExcludedArea = false;
       
   120 			if (searchExcludedIndex >= 0
       
   121 					&& searchExcludedIndex < parser.getExcludedAreas().size()) {
       
   122 				SourceExcludedArea area = parser.getExcludedAreas().get(
       
   123 						searchExcludedIndex);
       
   124 				if (area.getOffset() <= searchIndex) {
       
   125 					// If the search offset has skipped past an excluded area,
       
   126 					// the index is not changed
       
   127 					if (area.getOffset() + area.getLength() > searchIndex) {
       
   128 						searchIndex = area.getOffset() + area.getLength();
       
   129 					}
       
   130 					searchExcludedIndex++;
       
   131 					skipNonExcludedAreas();
       
   132 					changedExcludedArea = true;
       
   133 				}
       
   134 			}
       
   135 		} while (changedExcludedArea);
       
   136 	}
       
   137 
       
   138 	/**
       
   139 	 * Skips past excluded areas which are not actually excluded due to flags
       
   140 	 */
       
   141 	private void skipNonExcludedAreas() {
       
   142 		if (searchExcludedIndex >= 0
       
   143 				&& searchExcludedIndex < parser.getExcludedAreas().size()) {
       
   144 			boolean notExcluded = true;
       
   145 			do {
       
   146 				// If an excluded area is not excluded due to flags,
       
   147 				// the excluded area index is moved past that area
       
   148 				SourceExcludedArea area = parser.getExcludedAreas().get(
       
   149 						searchExcludedIndex);
       
   150 				if (!SourceParser.isExcluded(area.getType(), flags)) {
       
   151 					searchExcludedIndex++;
       
   152 				} else {
       
   153 					notExcluded = false;
       
   154 				}
       
   155 			} while (notExcluded
       
   156 					&& searchExcludedIndex < parser.getExcludedAreas().size());
       
   157 		}
       
   158 	}
       
   159 
       
   160 	/**
       
   161 	 * Returns the next occurence
       
   162 	 * 
       
   163 	 * @return the index or -1
       
   164 	 */
       
   165 	public abstract int findNext();
       
   166 
       
   167 	/**
       
   168 	 * Finds all occurences
       
   169 	 * 
       
   170 	 * @param list
       
   171 	 *            the list where the data is stored
       
   172 	 * @throws SourceParserException
       
   173 	 *             if search fails
       
   174 	 */
       
   175 	public abstract void findAll(List<Integer> list)
       
   176 			throws SourceParserException;
       
   177 
       
   178 }