001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.css.lex;
015:
016: import java.util.Iterator;
017: import java.util.LinkedList;
018: import java.util.List;
019: import java.util.ListIterator;
020:
021: /**
022: *
023: * @author jmarranz
024: */
025: public class SourceCode {
026: protected List tokens;
027: protected StringBuffer code;
028:
029: /** Creates a new instance of SourceCode */
030: public SourceCode(String code) {
031: this .tokens = Token.parse(code);
032: this .code = new StringBuffer(code);
033: }
034:
035: public SourceCode() {
036: this .tokens = new LinkedList();
037: this .code = new StringBuffer();
038: }
039:
040: public SourceCode(List tokens) {
041: this .tokens = tokens;
042: this .code = new StringBuffer(toStringTokens(tokens));
043: }
044:
045: public SourceCode(String code, List tokens) {
046: this .code = new StringBuffer(code);
047: this .tokens = tokens;
048: // Se supone que code se corresponde con los tokens
049: }
050:
051: public static SourceCode newSourceCode(String code) {
052: return new SourceCode(code);
053: }
054:
055: public boolean equals(Object other) {
056: if (super .equals(other))
057: return true;
058: if (!(other instanceof SourceCode))
059: return false;
060: return toString().equals(other.toString());
061: }
062:
063: public int hashCode() {
064: return toString().hashCode();
065: }
066:
067: public void addToken(Token token) {
068: tokens.add(token);
069: code.append(token.toString());
070: }
071:
072: public int tokenCount() {
073: return tokens.size();
074: }
075:
076: public Token getToken(int i) {
077: return (Token) tokens.get(i);
078: }
079:
080: public String toString() {
081: return code.toString();
082: }
083:
084: public String toStringTokens() {
085: return toStringTokens(tokens);
086: }
087:
088: public static String toStringTokens(List tokens) {
089: StringBuffer code = new StringBuffer();
090: for (Iterator it = tokens.iterator(); it.hasNext();) {
091: Token token = (Token) it.next();
092: code.append(token.toString());
093: }
094: return code.toString();
095: }
096:
097: public SourceCode[] split(Token byTok) {
098: // Simula la función String.split pero con tokens
099: SourceCode[] resTmp = new SourceCode[tokens.size() / 2 + 1]; // En este array caben todas las posibles soluciones
100: SourceCode current = new SourceCode();
101: resTmp[0] = current;
102: int i = 0;
103: for (Iterator it = tokens.iterator(); it.hasNext();) {
104: Token token = (Token) it.next();
105: if (token.equals(byTok)) {
106: if (current.tokenCount() > 0) // Si no se cumple es que el actual está vacío, lo ignoramos y perdemos
107: i++;
108: current = new SourceCode();
109: resTmp[i] = current;
110: } else {
111: current.addToken(token);
112: }
113: }
114:
115: if (current.tokenCount() == 0)
116: i--; // los vacíos no se incluyen, podría ser -1
117:
118: SourceCode[] res = new SourceCode[i + 1];
119: for (int j = 0; j <= i; j++)
120: res[j] = resTmp[j];
121: return res;
122: }
123:
124: public SourceCode trim() {
125: List resTokens = new LinkedList();
126: resTokens.addAll(this .tokens);
127:
128: boolean modified = false;
129:
130: for (ListIterator it = resTokens.listIterator(); it.hasNext();) {
131: Token token = (Token) it.next();
132: if (!token.getClass().equals(Space.class))
133: break;
134: it.remove();
135: modified = true;
136: }
137:
138: for (ListIterator it = resTokens.listIterator(resTokens.size()); it
139: .hasPrevious();) {
140: Token token = (Token) it.previous();
141: if (!token.getClass().equals(Space.class))
142: break;
143: it.remove();
144: modified = true;
145: }
146:
147: if (modified)
148: return new SourceCode(resTokens);
149: else
150: return new SourceCode(code.toString(), resTokens);
151: }
152: }
|