tracefw/tracecompiler/src/com.nokia.tracecompiler/src/com/nokia/tracecompiler/source/SourceStringSearch.java
changeset 56 aa2539c91954
parent 54 a151135b0cf9
child 60 e54443a6878c
child 62 1c2bb2fc7c87
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
     1 /*
       
     2 * Copyright (c) 2008 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 * String search algorithm
       
    17 *
       
    18 */
       
    19 package com.nokia.tracecompiler.source;
       
    20 
       
    21 import java.util.List;
       
    22 
       
    23 /**
       
    24  * String search algorithm
       
    25  * 
       
    26  */
       
    27 public class SourceStringSearch extends SourceSearch {
       
    28 
       
    29 	/**
       
    30 	 * String to be searched from the source.
       
    31 	 */
       
    32 	private String searchString;
       
    33 
       
    34 	/**
       
    35 	 * Number of different characters in source file alphabet. Assumes that
       
    36 	 * 8-bit encoding is used
       
    37 	 */
       
    38 	private final static int CHARACTER_COUNT = 256; // CodForChk_Dis_Magic
       
    39 
       
    40 	/**
       
    41 	 * Mask for bad character shifts (0xFF)
       
    42 	 */
       
    43 	private static final int BAD_CHARACTER_MASK = 0xFF; // CodForChk_Dis_Magic
       
    44 
       
    45 	/**
       
    46 	 * String search shifts.
       
    47 	 */
       
    48 	private int[] badCharacterShifts = new int[CHARACTER_COUNT];
       
    49 
       
    50 	/**
       
    51 	 * User data associated with search
       
    52 	 */
       
    53 	private Object searchData;
       
    54 
       
    55 	/**
       
    56 	 * Creates a new string search
       
    57 	 * 
       
    58 	 * @param parser
       
    59 	 *            the parser containing the source to be searched
       
    60 	 * @param searchString
       
    61 	 *            the string to be searched
       
    62 	 * @param startOffset
       
    63 	 *            offset to the start of search
       
    64 	 * @param endOffset
       
    65 	 *            offset to end of search or -1 for rest of the document
       
    66 	 * @param flags
       
    67 	 *            the search flags
       
    68 	 */
       
    69 	public SourceStringSearch(SourceParser parser, String searchString,
       
    70 			int startOffset, int endOffset, int flags) {
       
    71 		super(parser, startOffset, endOffset, flags);
       
    72 		setSearchString(searchString);
       
    73 	}
       
    74 
       
    75 	/*
       
    76 	 * (non-Javadoc)
       
    77 	 * 
       
    78 	 * @see com.nokia.tracecompiler.source.SourceSearch#findNext()
       
    79 	 */
       
    80 	@Override
       
    81 	public int findNext() {
       
    82 		try {
       
    83 			SourceDocumentInterface source = parser.getSource();
       
    84 			int stringIndex = searchString.length() - 1;
       
    85 			boolean found = false;
       
    86 			while (!found && searchIndex + stringIndex < searchEnd) {
       
    87 				char dataChar;
       
    88 				char searchChar;
       
    89 				boolean match = false;
       
    90 				do {
       
    91 					dataChar = source.getChar(searchIndex + stringIndex);
       
    92 					searchChar = searchString.charAt(stringIndex);
       
    93 					if ((flags & SourceParser.IGNORE_CASE) != 0) {
       
    94 						searchChar = Character.toLowerCase(searchChar);
       
    95 						dataChar = Character.toLowerCase(dataChar);
       
    96 					}
       
    97 					if (searchChar == '?' || searchChar == dataChar) {
       
    98 						match = true;
       
    99 						stringIndex--;
       
   100 					} else {
       
   101 						match = false;
       
   102 					}
       
   103 				} while (match && stringIndex >= 0);
       
   104 				// If string was not found, resets index and skips according
       
   105 				// to the shift table
       
   106 				if (stringIndex < 0) {
       
   107 					found = true;
       
   108 					// Checks the previous character if match beginning is set
       
   109 					if ((flags & SourceParser.MATCH_WORD_BEGINNING) != 0) {
       
   110 						if ((searchIndex > 0)
       
   111 								&& isPartOfWord(source.getChar(searchIndex - 1))) {
       
   112 							found = false;
       
   113 						}
       
   114 					}
       
   115 					// Checks the character after data if match end is set
       
   116 					if (found && ((flags & SourceParser.MATCH_WORD_END) != 0)) {
       
   117 						if (((searchIndex + searchString.length()) < source
       
   118 								.getLength())
       
   119 								&& isPartOfWord(source.getChar(searchIndex
       
   120 										+ searchString.length()))) {
       
   121 							found = false;
       
   122 						}
       
   123 					}
       
   124 				}
       
   125 				if (!found) {
       
   126 					int diff = searchString.length() - 1 - stringIndex;
       
   127 					stringIndex = searchString.length() - 1;
       
   128 					int skip;
       
   129 
       
   130 					if (dataChar > badCharacterShifts.length - 1) {
       
   131 						skip = 1;
       
   132 					} else {
       
   133 						skip = badCharacterShifts[dataChar] - diff;
       
   134 						if (skip <= 0) {
       
   135 							skip = 1;
       
   136 						}
       
   137 					}
       
   138 
       
   139 					searchIndex += skip;
       
   140 					skipExcludedArea();
       
   141 				}
       
   142 			}
       
   143 			if (!found) {
       
   144 				searchIndex = -1;
       
   145 			}
       
   146 		} catch (SourceParserException e) {
       
   147 			searchIndex = -1;
       
   148 		}
       
   149 		int ret = searchIndex;
       
   150 		if (searchIndex != -1) {
       
   151 			searchIndex += searchString.length();
       
   152 		}
       
   153 		return ret;
       
   154 	}
       
   155 
       
   156 	/**
       
   157 	 * Checks if the character is part of a word
       
   158 	 * 
       
   159 	 * @param c
       
   160 	 *            the character to be checked
       
   161 	 * @return true if part of word
       
   162 	 */
       
   163 	private boolean isPartOfWord(char c) {
       
   164 		return Character.isLetterOrDigit(c) || c == '_';
       
   165 	}
       
   166 
       
   167 	/*
       
   168 	 * (non-Javadoc)
       
   169 	 * 
       
   170 	 * @see com.nokia.tracecompiler.source.SourceSearch#findAll(java.util.List)
       
   171 	 */
       
   172 	@Override
       
   173 	public void findAll(List<Integer> list) throws SourceParserException {
       
   174 		resetSearch(0, -1);
       
   175 		int index;
       
   176 		do {
       
   177 			index = findNext();
       
   178 			if (index > 0) {
       
   179 				list.add(new Integer(index));
       
   180 			}
       
   181 		} while (index > 0);
       
   182 	}
       
   183 
       
   184 	/**
       
   185 	 * Changes the search string
       
   186 	 * 
       
   187 	 * @param searchString
       
   188 	 *            the string to be searched
       
   189 	 */
       
   190 	public void setSearchString(String searchString) {
       
   191 		this.searchString = searchString;
       
   192 		int length = searchString.length();
       
   193 		// Elements not found in the string get the maximum shift distance
       
   194 		for (int i = 0; i < badCharacterShifts.length; i++) {
       
   195 			badCharacterShifts[i] = searchString.length();
       
   196 		}
       
   197 		// Characters of the search string are mapped into the
       
   198 		// shift distances array. If a character is found multiple
       
   199 		// times, the smallest shitft distance is stored
       
   200 		for (int i = 0; i < searchString.length() - 1; i++) {
       
   201 			if ((flags & SourceParser.IGNORE_CASE) != 0) {
       
   202 				badCharacterShifts[Character
       
   203 						.toLowerCase(searchString.charAt(i))
       
   204 						& BAD_CHARACTER_MASK] = length - i - 1;
       
   205 
       
   206 			} else {
       
   207 				badCharacterShifts[searchString.charAt(i) & BAD_CHARACTER_MASK] = length
       
   208 						- i - 1;
       
   209 			}
       
   210 		}
       
   211 	}
       
   212 
       
   213 	/**
       
   214 	 * Returns the search string
       
   215 	 * 
       
   216 	 * @return the string
       
   217 	 */
       
   218 	public String getSearchString() {
       
   219 		return searchString;
       
   220 	}
       
   221 
       
   222 	/**
       
   223 	 * Checks if the given string matches the search string
       
   224 	 * 
       
   225 	 * @param string
       
   226 	 *            the string
       
   227 	 * @return true if they match
       
   228 	 */
       
   229 	public boolean isSearchStringMatch(String string) {
       
   230 		int index = 0;
       
   231 		boolean match = true;
       
   232 		while (index < string.length() && match) {
       
   233 			char dataChar = string.charAt(index);
       
   234 			char searchChar = searchString.charAt(index);
       
   235 			if ((flags & SourceParser.IGNORE_CASE) != 0) {
       
   236 				searchChar = Character.toLowerCase(searchChar);
       
   237 				dataChar = Character.toLowerCase(dataChar);
       
   238 			}
       
   239 			if (searchChar == '?' || searchChar == dataChar) {
       
   240 				index++;
       
   241 			} else {
       
   242 				match = false;
       
   243 			}
       
   244 		}
       
   245 		return match;
       
   246 	}
       
   247 
       
   248 	/**
       
   249 	 * Sets the user variable associated with this searcher
       
   250 	 * 
       
   251 	 * @param data
       
   252 	 *            the variable
       
   253 	 */
       
   254 	public void setSearchData(Object data) {
       
   255 		searchData = data;
       
   256 	}
       
   257 
       
   258 	/**
       
   259 	 * Gets the user variable associated with this searcher
       
   260 	 * 
       
   261 	 * @return the variable
       
   262 	 */
       
   263 	public Object getSearchData() {
       
   264 		return searchData;
       
   265 	}
       
   266 
       
   267 }