You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LexicalUnitImpl.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * Copyright (c) 1999 World Wide Web Consortium
  3. * (Massachusetts Institute of Technology, Institut National de Recherche
  4. * en Informatique et en Automatique, Keio University).
  5. * All Rights Reserved. http://www.w3.org/Consortium/Legal/
  6. *
  7. * $Id: LexicalUnitImpl.java,v 1.3 2000/02/15 02:08:19 plehegar Exp $
  8. */
  9. package com.vaadin.sass.parser;
  10. import java.io.Serializable;
  11. import org.w3c.css.sac.LexicalUnit;
  12. import com.vaadin.sass.util.ColorUtil;
  13. /**
  14. * @version $Revision: 1.3 $
  15. * @author Philippe Le Hegaret
  16. */
  17. public class LexicalUnitImpl implements LexicalUnit, SCSSLexicalUnit,
  18. Serializable {
  19. private static final long serialVersionUID = -6649833716809789399L;
  20. LexicalUnit prev;
  21. LexicalUnit next;
  22. short type;
  23. int line;
  24. int column;
  25. int i;
  26. float f;
  27. short dimension;
  28. String sdimension;
  29. String s;
  30. String fname;
  31. LexicalUnitImpl params;
  32. LexicalUnitImpl(short type, int line, int column, LexicalUnitImpl p) {
  33. if (p != null) {
  34. prev = p;
  35. p.next = this;
  36. }
  37. this.line = line;
  38. this.column = column - 1;
  39. this.type = type;
  40. }
  41. LexicalUnitImpl(int line, int column, LexicalUnitImpl previous, int i) {
  42. this(SAC_INTEGER, line, column, previous);
  43. this.i = i;
  44. }
  45. LexicalUnitImpl(int line, int column, LexicalUnitImpl previous,
  46. short dimension, String sdimension, float f) {
  47. this(dimension, line, column, previous);
  48. this.f = f;
  49. this.dimension = dimension;
  50. this.sdimension = sdimension;
  51. }
  52. LexicalUnitImpl(int line, int column, LexicalUnitImpl previous, short type,
  53. String s) {
  54. this(type, line, column, previous);
  55. this.s = s;
  56. }
  57. LexicalUnitImpl(short type, int line, int column, LexicalUnitImpl previous,
  58. String fname, LexicalUnitImpl params) {
  59. this(type, line, column, previous);
  60. this.fname = fname;
  61. this.params = params;
  62. }
  63. public int getLineNumber() {
  64. return line;
  65. }
  66. public int getColumnNumber() {
  67. return column;
  68. }
  69. @Override
  70. public short getLexicalUnitType() {
  71. return type;
  72. }
  73. @Override
  74. public LexicalUnit getNextLexicalUnit() {
  75. return next;
  76. }
  77. public void setNextLexicalUnit(LexicalUnit n) {
  78. next = n;
  79. }
  80. @Override
  81. public LexicalUnit getPreviousLexicalUnit() {
  82. return prev;
  83. }
  84. @Override
  85. public int getIntegerValue() {
  86. return i;
  87. }
  88. void setIntegerValue(int i) {
  89. this.i = i;
  90. }
  91. @Override
  92. public float getFloatValue() {
  93. return f;
  94. }
  95. public void setFloatValue(float f) {
  96. this.f = f;
  97. }
  98. @Override
  99. public String getDimensionUnitText() {
  100. switch (type) {
  101. case SAC_PERCENTAGE:
  102. return "%";
  103. case SAC_EM:
  104. return "em";
  105. case SAC_EX:
  106. return "ex";
  107. case SAC_PIXEL:
  108. return "px";
  109. case SAC_CENTIMETER:
  110. return "cm";
  111. case SAC_MILLIMETER:
  112. return "mm";
  113. case SAC_INCH:
  114. return "in";
  115. case SAC_POINT:
  116. return "pt";
  117. case SAC_PICA:
  118. return "pc";
  119. case SAC_DEGREE:
  120. return "deg";
  121. case SAC_RADIAN:
  122. return "rad";
  123. case SAC_GRADIAN:
  124. return "grad";
  125. case SAC_MILLISECOND:
  126. return "ms";
  127. case SAC_SECOND:
  128. return "s";
  129. case SAC_HERTZ:
  130. return "Hz";
  131. case SAC_KILOHERTZ:
  132. return "kHz";
  133. case SAC_DIMENSION:
  134. return sdimension;
  135. default:
  136. throw new IllegalStateException("invalid dimension " + type);
  137. }
  138. }
  139. @Override
  140. public String getStringValue() {
  141. return s;
  142. }
  143. public void setStringValue(String str) {
  144. s = str;
  145. }
  146. @Override
  147. public String getFunctionName() {
  148. return fname;
  149. }
  150. @Override
  151. public LexicalUnitImpl getParameters() {
  152. return params;
  153. }
  154. @Override
  155. public LexicalUnitImpl getSubValues() {
  156. return params;
  157. }
  158. @Override
  159. public String toString() {
  160. short type = getLexicalUnitType();
  161. String text = null;
  162. switch (type) {
  163. case SCSS_VARIABLE:
  164. text = "$" + s;
  165. break;
  166. case LexicalUnit.SAC_OPERATOR_COMMA:
  167. text = ",";
  168. break;
  169. case LexicalUnit.SAC_OPERATOR_PLUS:
  170. text = "+";
  171. break;
  172. case LexicalUnit.SAC_OPERATOR_MINUS:
  173. text = "-";
  174. break;
  175. case LexicalUnit.SAC_OPERATOR_MULTIPLY:
  176. text = "*";
  177. break;
  178. case LexicalUnit.SAC_OPERATOR_SLASH:
  179. text = "/";
  180. break;
  181. case LexicalUnit.SAC_OPERATOR_MOD:
  182. text = "%";
  183. break;
  184. case LexicalUnit.SAC_OPERATOR_EXP:
  185. text = "^";
  186. break;
  187. case LexicalUnit.SAC_OPERATOR_LT:
  188. text = "<";
  189. break;
  190. case LexicalUnit.SAC_OPERATOR_GT:
  191. text = ">";
  192. break;
  193. case LexicalUnit.SAC_OPERATOR_LE:
  194. text = "<=";
  195. break;
  196. case LexicalUnit.SAC_OPERATOR_GE:
  197. text = "=>";
  198. break;
  199. case LexicalUnit.SAC_OPERATOR_TILDE:
  200. text = "~";
  201. break;
  202. case LexicalUnit.SAC_INHERIT:
  203. text = "inherit";
  204. break;
  205. case LexicalUnit.SAC_INTEGER:
  206. text = Integer.toString(getIntegerValue(), 10);
  207. break;
  208. case LexicalUnit.SAC_REAL:
  209. text = getFloatValue() + "";
  210. break;
  211. case LexicalUnit.SAC_EM:
  212. case LexicalUnit.SAC_EX:
  213. case LexicalUnit.SAC_PIXEL:
  214. case LexicalUnit.SAC_INCH:
  215. case LexicalUnit.SAC_CENTIMETER:
  216. case LexicalUnit.SAC_MILLIMETER:
  217. case LexicalUnit.SAC_POINT:
  218. case LexicalUnit.SAC_PICA:
  219. case LexicalUnit.SAC_PERCENTAGE:
  220. case LexicalUnit.SAC_DEGREE:
  221. case LexicalUnit.SAC_GRADIAN:
  222. case LexicalUnit.SAC_RADIAN:
  223. case LexicalUnit.SAC_MILLISECOND:
  224. case LexicalUnit.SAC_SECOND:
  225. case LexicalUnit.SAC_HERTZ:
  226. case LexicalUnit.SAC_KILOHERTZ:
  227. case LexicalUnit.SAC_DIMENSION:
  228. float f = getFloatValue();
  229. int i = (int) f;
  230. if ((i) == f) {
  231. text = i + getDimensionUnitText();
  232. } else {
  233. text = f + getDimensionUnitText();
  234. }
  235. break;
  236. case LexicalUnit.SAC_URI:
  237. text = "url(" + getStringValue() + ")";
  238. break;
  239. case LexicalUnit.SAC_RGBCOLOR:
  240. case LexicalUnit.SAC_COUNTER_FUNCTION:
  241. case LexicalUnit.SAC_COUNTERS_FUNCTION:
  242. case LexicalUnit.SAC_RECT_FUNCTION:
  243. case LexicalUnit.SAC_FUNCTION:
  244. String funcName = getFunctionName();
  245. LexicalUnitImpl firstParam = getParameters();
  246. if ("round".equals(funcName)) {
  247. firstParam
  248. .setFloatValue(Math.round(firstParam.getFloatValue()));
  249. text = firstParam.toString();
  250. } else if ("ceil".equals(funcName)) {
  251. firstParam.setFloatValue((float) Math.ceil(firstParam
  252. .getFloatValue()));
  253. text = firstParam.toString();
  254. } else if ("floor".equals(funcName)) {
  255. firstParam.setFloatValue((float) Math.floor(firstParam
  256. .getFloatValue()));
  257. text = firstParam.toString();
  258. } else if ("abs".equals(funcName)) {
  259. firstParam.setFloatValue(Math.abs(firstParam.getFloatValue()));
  260. text = firstParam.toString();
  261. } else if ("darken".equals(funcName)) {
  262. LexicalUnitImpl dark = ColorUtil.darken(this);
  263. text = dark.toString();
  264. } else if ("lighten".equals(funcName)) {
  265. text = ColorUtil.lighten(this).toString();
  266. } else {
  267. text = getFunctionName() + "(" + getParameters() + ")";
  268. }
  269. break;
  270. case LexicalUnit.SAC_IDENT:
  271. text = getStringValue();
  272. break;
  273. case LexicalUnit.SAC_STRING_VALUE:
  274. // @@SEEME. not exact
  275. text = "\"" + getStringValue() + "\"";
  276. break;
  277. case LexicalUnit.SAC_ATTR:
  278. text = "attr(" + getStringValue() + ")";
  279. break;
  280. case LexicalUnit.SAC_UNICODERANGE:
  281. text = "@@TODO";
  282. break;
  283. case LexicalUnit.SAC_SUB_EXPRESSION:
  284. text = getSubValues().toString();
  285. break;
  286. default:
  287. text = "@unknown";
  288. break;
  289. }
  290. if (getNextLexicalUnit() != null) {
  291. if (getNextLexicalUnit().getLexicalUnitType() == SAC_OPERATOR_COMMA) {
  292. return text + getNextLexicalUnit();
  293. }
  294. return text + ' ' + getNextLexicalUnit();
  295. } else {
  296. return text;
  297. }
  298. }
  299. @Override
  300. public LexicalUnitImpl divide(LexicalUnitImpl denominator) {
  301. setFloatValue(getFloatValue() / denominator.getIntegerValue());
  302. return this;
  303. }
  304. @Override
  305. public LexicalUnitImpl add(LexicalUnitImpl another) {
  306. setFloatValue(getFloatValue() + another.getFloatValue());
  307. return this;
  308. }
  309. @Override
  310. public LexicalUnitImpl minus(LexicalUnitImpl another) {
  311. setFloatValue(getFloatValue() - another.getFloatValue());
  312. return this;
  313. }
  314. @Override
  315. public LexicalUnitImpl multiply(LexicalUnitImpl another) {
  316. setFloatValue(getFloatValue() * another.getIntegerValue());
  317. return this;
  318. }
  319. public void replaceValue(LexicalUnitImpl another) {
  320. type = another.getLexicalUnitType();
  321. i = another.getIntegerValue();
  322. f = another.getFloatValue();
  323. dimension = another.getDimension();
  324. sdimension = another.getSdimension();
  325. s = another.getStringValue();
  326. fname = getFunctionName();
  327. params = another.getParameters();
  328. prev = another.getPreviousLexicalUnit();
  329. LexicalUnit finalNextInAnother = another;
  330. while (finalNextInAnother.getNextLexicalUnit() != null) {
  331. finalNextInAnother = finalNextInAnother.getNextLexicalUnit();
  332. }
  333. ((LexicalUnitImpl) finalNextInAnother).setNextLexicalUnit(next);
  334. next = another.next;
  335. }
  336. public short getDimension() {
  337. return dimension;
  338. }
  339. public String getSdimension() {
  340. return sdimension;
  341. }
  342. // here some useful function for creation
  343. public static LexicalUnitImpl createVariable(int line, int column,
  344. LexicalUnitImpl previous, String name) {
  345. return new LexicalUnitImpl(line, column, previous, SCSS_VARIABLE, name);
  346. }
  347. public static LexicalUnitImpl createNumber(int line, int column,
  348. LexicalUnitImpl previous, float v) {
  349. int i = (int) v;
  350. if (v == i) {
  351. return new LexicalUnitImpl(line, column, previous, i);
  352. } else {
  353. return new LexicalUnitImpl(line, column, previous, SAC_REAL, "", v);
  354. }
  355. }
  356. public static LexicalUnitImpl createInteger(int line, int column,
  357. LexicalUnitImpl previous, int i) {
  358. return new LexicalUnitImpl(line, column, previous, i);
  359. }
  360. public static LexicalUnitImpl createPercentage(int line, int column,
  361. LexicalUnitImpl previous, float v) {
  362. return new LexicalUnitImpl(line, column, previous, SAC_PERCENTAGE,
  363. null, v);
  364. }
  365. static LexicalUnitImpl createEMS(int line, int column,
  366. LexicalUnitImpl previous, float v) {
  367. return new LexicalUnitImpl(line, column, previous, SAC_EM, null, v);
  368. }
  369. static LexicalUnitImpl createEXS(int line, int column,
  370. LexicalUnitImpl previous, float v) {
  371. return new LexicalUnitImpl(line, column, previous, SAC_EX, null, v);
  372. }
  373. static LexicalUnitImpl createPX(int line, int column,
  374. LexicalUnitImpl previous, float v) {
  375. return new LexicalUnitImpl(line, column, previous, SAC_PIXEL, null, v);
  376. }
  377. static LexicalUnitImpl createCM(int line, int column,
  378. LexicalUnitImpl previous, float v) {
  379. return new LexicalUnitImpl(line, column, previous, SAC_CENTIMETER,
  380. null, v);
  381. }
  382. static LexicalUnitImpl createMM(int line, int column,
  383. LexicalUnitImpl previous, float v) {
  384. return new LexicalUnitImpl(line, column, previous, SAC_MILLIMETER,
  385. null, v);
  386. }
  387. static LexicalUnitImpl createIN(int line, int column,
  388. LexicalUnitImpl previous, float v) {
  389. return new LexicalUnitImpl(line, column, previous, SAC_INCH, null, v);
  390. }
  391. static LexicalUnitImpl createPT(int line, int column,
  392. LexicalUnitImpl previous, float v) {
  393. return new LexicalUnitImpl(line, column, previous, SAC_POINT, null, v);
  394. }
  395. static LexicalUnitImpl createPC(int line, int column,
  396. LexicalUnitImpl previous, float v) {
  397. return new LexicalUnitImpl(line, column, previous, SAC_PICA, null, v);
  398. }
  399. static LexicalUnitImpl createDEG(int line, int column,
  400. LexicalUnitImpl previous, float v) {
  401. return new LexicalUnitImpl(line, column, previous, SAC_DEGREE, null, v);
  402. }
  403. static LexicalUnitImpl createRAD(int line, int column,
  404. LexicalUnitImpl previous, float v) {
  405. return new LexicalUnitImpl(line, column, previous, SAC_RADIAN, null, v);
  406. }
  407. static LexicalUnitImpl createGRAD(int line, int column,
  408. LexicalUnitImpl previous, float v) {
  409. return new LexicalUnitImpl(line, column, previous, SAC_GRADIAN, null, v);
  410. }
  411. static LexicalUnitImpl createMS(int line, int column,
  412. LexicalUnitImpl previous, float v) {
  413. if (v < 0) {
  414. throw new ParseException("Time values may not be negative");
  415. }
  416. return new LexicalUnitImpl(line, column, previous, SAC_MILLISECOND,
  417. null, v);
  418. }
  419. static LexicalUnitImpl createS(int line, int column,
  420. LexicalUnitImpl previous, float v) {
  421. if (v < 0) {
  422. throw new ParseException("Time values may not be negative");
  423. }
  424. return new LexicalUnitImpl(line, column, previous, SAC_SECOND, null, v);
  425. }
  426. static LexicalUnitImpl createHZ(int line, int column,
  427. LexicalUnitImpl previous, float v) {
  428. if (v < 0) {
  429. throw new ParseException("Frequency values may not be negative");
  430. }
  431. return new LexicalUnitImpl(line, column, previous, SAC_HERTZ, null, v);
  432. }
  433. static LexicalUnitImpl createKHZ(int line, int column,
  434. LexicalUnitImpl previous, float v) {
  435. if (v < 0) {
  436. throw new ParseException("Frequency values may not be negative");
  437. }
  438. return new LexicalUnitImpl(line, column, previous, SAC_KILOHERTZ, null,
  439. v);
  440. }
  441. static LexicalUnitImpl createDimen(int line, int column,
  442. LexicalUnitImpl previous, float v, String s) {
  443. return new LexicalUnitImpl(line, column, previous, SAC_DIMENSION, s, v);
  444. }
  445. static LexicalUnitImpl createInherit(int line, int column,
  446. LexicalUnitImpl previous) {
  447. return new LexicalUnitImpl(line, column, previous, SAC_INHERIT,
  448. "inherit");
  449. }
  450. public static LexicalUnitImpl createIdent(int line, int column,
  451. LexicalUnitImpl previous, String s) {
  452. return new LexicalUnitImpl(line, column, previous, SAC_IDENT, s);
  453. }
  454. static LexicalUnitImpl createString(int line, int column,
  455. LexicalUnitImpl previous, String s) {
  456. return new LexicalUnitImpl(line, column, previous, SAC_STRING_VALUE, s);
  457. }
  458. static LexicalUnitImpl createURL(int line, int column,
  459. LexicalUnitImpl previous, String s) {
  460. return new LexicalUnitImpl(line, column, previous, SAC_URI, s);
  461. }
  462. static LexicalUnitImpl createAttr(int line, int column,
  463. LexicalUnitImpl previous, String s) {
  464. return new LexicalUnitImpl(line, column, previous, SAC_ATTR, s);
  465. }
  466. static LexicalUnitImpl createCounter(int line, int column,
  467. LexicalUnitImpl previous, LexicalUnit params) {
  468. return new LexicalUnitImpl(SAC_COUNTER_FUNCTION, line, column,
  469. previous, "counter", (LexicalUnitImpl) params);
  470. }
  471. public static LexicalUnitImpl createCounters(int line, int column,
  472. LexicalUnitImpl previous, LexicalUnit params) {
  473. return new LexicalUnitImpl(SAC_COUNTERS_FUNCTION, line, column,
  474. previous, "counters", (LexicalUnitImpl) params);
  475. }
  476. public static LexicalUnitImpl createRGBColor(int line, int column,
  477. LexicalUnitImpl previous, LexicalUnit params) {
  478. return new LexicalUnitImpl(SAC_RGBCOLOR, line, column, previous, "rgb",
  479. (LexicalUnitImpl) params);
  480. }
  481. public static LexicalUnitImpl createRect(int line, int column,
  482. LexicalUnitImpl previous, LexicalUnit params) {
  483. return new LexicalUnitImpl(SAC_RECT_FUNCTION, line, column, previous,
  484. "rect", (LexicalUnitImpl) params);
  485. }
  486. public static LexicalUnitImpl createFunction(int line, int column,
  487. LexicalUnitImpl previous, String fname, LexicalUnit params) {
  488. return new LexicalUnitImpl(SAC_FUNCTION, line, column, previous, fname,
  489. (LexicalUnitImpl) params);
  490. }
  491. public static LexicalUnitImpl createUnicodeRange(int line, int column,
  492. LexicalUnit previous, LexicalUnit params) {
  493. // @@ return new LexicalUnitImpl(line, column, previous, null,
  494. // SAC_UNICODERANGE, params);
  495. return null;
  496. }
  497. public static LexicalUnitImpl createComma(int line, int column,
  498. LexicalUnitImpl previous) {
  499. return new LexicalUnitImpl(SAC_OPERATOR_COMMA, line, column, previous);
  500. }
  501. public static LexicalUnitImpl createSlash(int line, int column,
  502. LexicalUnitImpl previous) {
  503. return new LexicalUnitImpl(SAC_OPERATOR_SLASH, line, column, previous);
  504. }
  505. }