Skip to main content
summaryrefslogtreecommitdiffstats
blob: b459766c6710e12ee918ab35495c9c6f99b62e03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.db.internal.search.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.jdk.core.type.MatchLocation;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.orcs.search.CaseType;

/**
 * @author Roberto E. Escobar
 */
public final class WordOrderMatcher {

   public WordOrderMatcher() {
      // Utility Class
   }

   public List<MatchLocation> findInStream(InputStream inputStream, String toSearch, CaseType caseType, boolean findAllMatchLocations) throws OseeCoreException {
      List<MatchLocation> matchLocations = new ArrayList<MatchLocation>();
      Reader reader = null;
      try {
         reader = new InputStreamReader(inputStream, "UTF-8");
         boolean isCaseInsensitive = !caseType.isCaseSensitive();
         char[] charsToSearch = WordsUtil.removeExtraSpacesAndSpecialCharacters(toSearch, isCaseInsensitive);
         int charCount = 0;
         int index = 0;
         int value = 0;
         boolean lastCharacterAddedWasWhiteSpace = false;
         boolean currCharValid = false;
         MatchLocation matchLocation = new MatchLocation();
         while (value != -1) {
            value = reader.read();
            charCount++;
            char currChar = (char) value;
            if (isCaseInsensitive) {
               currChar = Character.toLowerCase(currChar);
            }

            if (currChar != '\r' && currChar != '\n') {
               if (WordsUtil.isPunctuationOrApostrophe(currChar)) {
                  currChar = ' ';
               }

               if (Character.isWhitespace(currChar)) {
                  if (!lastCharacterAddedWasWhiteSpace) {
                     currCharValid = true;
                     lastCharacterAddedWasWhiteSpace = true;
                  } else {
                     currCharValid = false;
                  }
               } else {
                  currCharValid = true;
                  lastCharacterAddedWasWhiteSpace = false;
               }
            }

            if (currCharValid) {
               if (charsToSearch[index] != currChar) {
                  index = 0;
                  matchLocation.reset();
               }

               if (charsToSearch[index] == currChar) {
                  if (index == 0) {
                     matchLocation.setStartPosition(charCount);
                  }

                  if (index + 1 < charsToSearch.length) {
                     index++;
                  } else {
                     matchLocation.setEndPosition(charCount);
                     matchLocations.add(matchLocation.clone());
                     index = 0;
                     if (!findAllMatchLocations) {
                        break;
                     }
                  }
               }
            }
         }
      } catch (UnsupportedEncodingException ex) {
         OseeExceptions.wrapAndThrow(ex);
      } catch (IOException ex) {
         OseeExceptions.wrapAndThrow(ex);
      } finally {
         Lib.close(reader);
      }
      return matchLocations;
   }
}

Back to the top