View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * svgreal is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.engine;
19  
20  /**
21   * Class to split SVG attribute values into a stream of
22   * tokens. Each token is either a piece of text to be
23   * quoted verbatim by the SVG id normalizer, or an idref
24   * to be replaced by a normalized id ref.
25   * @author laaglu
26   */
27  public class IdRefTokenizer {
28  	protected static final String START = "url(#";
29  	protected static final String END = ")";
30  	static class IdRefToken {
31  		public static final int IDREF = 1;
32  		public static final int DATA = 2;
33  		int kind;
34  		String value;
35  		public int getKind() {
36  			return kind;
37  		}
38  		public String getValue() {
39  			return value;
40  		}
41  	}
42  	protected IdRefToken token = new IdRefToken();
43  	protected String str;
44  	protected int index1;
45  	protected int index2;
46  
47  	public void tokenize(String str) {
48  		this.str = str;
49  		index1 = 0;
50  		index2 = 0;
51  		token.kind = IdRefToken.IDREF;
52  		token.value = null;
53  	}
54  	public IdRefToken nextToken() {
55  		if (index1 != str.length()) {
56  			if (token.kind == IdRefToken.IDREF) {
57  				token.kind = IdRefToken.DATA;
58  				index2 = str.indexOf(START, index1);
59  				if (index2 != -1) {
60  					token.value = str.substring(index1, index2 + START.length());
61  					index1 = index2 + START.length();
62  				} else {
63  					token.value = str.substring(index1);
64  					index1 = str.length();
65  				}
66  			} else {
67  				index2 = str.indexOf(END, index1);
68  				if (index2 != -1) {
69  					token.value = str.substring(index1, index2);
70  					index1 = index2;
71  					token.kind = IdRefToken.IDREF;
72  				} else {
73  					token.value = str.substring(index1);
74  					index1 = str.length();
75  				}
76  			}
77  			return token;
78  		}
79  		return null;
80  	}
81  }