1+ // Copyright (c) 2012 Chan Wai Shing
2+ //
3+ // Permission is hereby granted, free of charge, to any person obtaining
4+ // a copy of this software and associated documentation files (the
5+ // "Software"), to deal in the Software without restriction, including
6+ // without limitation the rights to use, copy, modify, merge, publish,
7+ // distribute, sublicense, and/or sell copies of the Software, and to
8+ // permit persons to whom the Software is furnished to do so, subject to
9+ // the following conditions:
10+ //
11+ // The above copyright notice and this permission notice shall be
12+ // included in all copies or substantial portions of the Software.
13+ //
14+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+ // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+ package io .github .kbiakov .codeview .highlight .parser ;
22+
23+ import java .util .ArrayList ;
24+ import java .util .List ;
25+
26+ /**
27+ * The parser parsed result.
28+ *
29+ * This class include the information needed to highlight the syntax.
30+ * Information includes where the content located in the document (offset and
31+ * length) and what style(s) should be applied on that segment of content.
32+ *
33+ * @author Chan Wai Shing <[email protected] > 34+ */
35+ public class ParseResult {
36+
37+ /**
38+ * The start position of the content.
39+ */
40+ protected int offset ;
41+ /**
42+ * The length of the content.
43+ */
44+ protected int length ;
45+ /**
46+ * The style keys of the content. The style at higher index of the list will
47+ * override the style of the lower index.
48+ */
49+ protected List <String > styleKeys ;
50+
51+ /**
52+ * Constructor.
53+ *
54+ * @param offset the start position of the content
55+ * @param length the length of the content
56+ * @param styleKeys the style keys of the content
57+ */
58+ public ParseResult (int offset , int length , List <String > styleKeys ) {
59+ this .offset = offset ;
60+ this .length = length ;
61+ this .styleKeys = new ArrayList <String >(styleKeys );
62+ }
63+
64+ /**
65+ * The start position of the content.
66+ * @return the start position of the content
67+ */
68+ public int getOffset () {
69+ return offset ;
70+ }
71+
72+ /**
73+ * The start position of the content.
74+ * @param offset the start position of the content
75+ */
76+ public void setOffset (int offset ) {
77+ this .offset = offset ;
78+ }
79+
80+ /**
81+ * The length of the content.
82+ * @return the length of the content
83+ */
84+ public int getLength () {
85+ return length ;
86+ }
87+
88+ /**
89+ * The length of the content.
90+ * @param length the length of the content
91+ */
92+ public void setLength (int length ) {
93+ this .length = length ;
94+ }
95+
96+ /**
97+ * Get the style keys represented by one string key, see
98+ * {@link Theme#getStylesAttributeSet(String)}.
99+ * @return the style keys of the content
100+ */
101+ public String getStyleKeysString () {
102+ StringBuilder sb = new StringBuilder (10 );
103+ for (int i = 0 , iEnd = styleKeys .size (); i < iEnd ; i ++) {
104+ if (i != 0 ) {
105+ sb .append (" " );
106+ }
107+ sb .append (styleKeys .get (i ));
108+ }
109+ return sb .toString ();
110+ }
111+
112+ /**
113+ * The style keys of the content.
114+ * @param styleKeys the style keys of the content
115+ */
116+ public void setStyleKeys (List <String > styleKeys ) {
117+ this .styleKeys = new ArrayList <String >(styleKeys );
118+ }
119+
120+ /**
121+ * The style keys of the content.
122+ * @param styleKey the style key
123+ * @return see the return value of {@link List#add(Object)}
124+ */
125+ public boolean addStyleKey (String styleKey ) {
126+ return styleKeys .add (styleKey );
127+ }
128+
129+ /**
130+ * The style keys of the content.
131+ * @param styleKey the style key
132+ * @return see the return value of {@link List#remove(Object)}
133+ */
134+ public boolean removeStyleKey (String styleKey ) {
135+ return styleKeys .remove (styleKey );
136+ }
137+
138+ /**
139+ * The style keys of the content.
140+ */
141+ public void clearStyleKeys () {
142+ styleKeys .clear ();
143+ }
144+
145+ /**
146+ * The style keys for this matched result, see {@link syntaxhighlighter.theme}.
147+ * @return the style keys
148+ */
149+ public List <String > getStyleKeys () {
150+ return new ArrayList <String >(styleKeys );
151+ }
152+
153+ /**
154+ * {@inheritDoc}
155+ */
156+ @ Override
157+ public String toString () {
158+ StringBuilder sb = new StringBuilder ();
159+
160+ sb .append ("[" );
161+ sb .append (offset );
162+ sb .append ("; " );
163+ sb .append (length );
164+ sb .append ("; " );
165+ for (int i = 0 , iEnd = styleKeys .size (); i < iEnd ; i ++) {
166+ if (i != 0 ) {
167+ sb .append (", " );
168+ }
169+ sb .append (styleKeys .get (i ));
170+ }
171+ sb .append ("]" );
172+
173+ return sb .toString ();
174+ }
175+ }
0 commit comments