View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of vectomatic2.
4    * 
5    * vectomatic2 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   * vectomatic2 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 vectomatic2.  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  	}
53  	public IdRefToken nextToken() {
54  		if (index1 != str.length()) {
55  			if (token.kind == IdRefToken.IDREF) {
56  				token.kind = IdRefToken.DATA;
57  				index2 = str.indexOf(START, index1);
58  				if (index2 != -1) {
59  					token.value = str.substring(index1, index2 + START.length());
60  					index1 = index2 + START.length();
61  				} else {
62  					token.value = str.substring(index1);
63  					index1 = str.length();
64  				}
65  			} else {
66  				index2 = str.indexOf(END, index1);
67  				if (index2 != -1) {
68  					token.value = str.substring(index1, index2);
69  					index1 = index2;
70  					token.kind = IdRefToken.IDREF;
71  				} else {
72  					token.value = str.substring(index1);
73  					index1 = str.length();
74  				}
75  			}
76  			return token;
77  		}
78  		return null;
79  	}
80  }