summaryrefslogtreecommitdiff
path: root/src/ext/plantuml/com/ctreber/acearth/util/StringParser.java
blob: 3ba3b1cd8803d709e2a6c1a3ada27ce156ce812f (plain)
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
package ext.plantuml.com.ctreber.acearth.util;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Cuts a string in words separated by white space. Quotes and square
 * brackets are recognized (that is, white space within is ignored).
 *
 * <p>&copy; 2002 Christian Treber, ct@ctreber.com
 * @author Christian Treber, ct@ctreber.com
 *
 */
public class StringParser
{
  public static List parse(String pLine)
  {
    final List lSections = new ArrayList();

    // True if within word.
    boolean lInSectionP = false;
    // Current char
    char lChar;
    // Wait for this character before switching back to normal parsing.
    char lSeparator = ' ';
    // Part count.
    int lSectionNo = 0;
    // Part start position
    int lSectionStart = 0;
    // Part end position
    int lSectionEnd = 0;

    final int lLen = pLine.length();
    for(int lCharNo = 0; lCharNo <= lLen; lCharNo++)
    {
      if(lCharNo < lLen)
      {
        lChar = pLine.charAt(lCharNo);
      } else
      {
        // This is a fictional last character.
        lChar = ' ';
      }

      if(lInSectionP)
      {
        // In section. Termination is by space or specific separator.
        if((lChar != ' ') || (lSeparator != ' '))
        {
          // It's not a space, or it is a space, but we wait for a special separator.
          if(lChar == lSeparator)
          {
            // We waited for this separator. Switch back to normal parsing.
            lSeparator = ' ';
            lSectionEnd = lCharNo - 1;
          } else
          {
            lSectionEnd = lCharNo;
          }
        } else
        {
          // Section has ended (with a space).
          lSections.add(pLine.substring(lSectionStart, lSectionEnd + 1));
          lSectionNo++;
          lInSectionP = false;
        }
      } else
      {
        // Not in a section, skipping white space.
        if(lChar != ' ')
        {
          // No white space: a section has started.
          if(lChar == '"')
          {
            // Special parsing "string"
            lSeparator = '"';
            lSectionStart = lCharNo + 1;
          } else if(lChar == '[')
          {
            // Special parsing "square brackets"
            lSeparator = ']';
            lSectionStart = lCharNo + 1;
          } else
          {
            // Use normal parsing.
            lSeparator = ' ';
            lSectionEnd = lSectionStart = lCharNo;
          }
          lInSectionP = true;
        } else
        {
          // More void...
        }
      }
    }

    return lSections;
  }
}