/android/LGame-Android-0.3/src/org/loon/framework/android/game/action/avg/command/Command.java

http://loon-simple.googlecode.com/ · Java · 1004 lines · 692 code · 103 blank · 209 comment · 202 complexity · eec1aae62a9d5c5b8a0bafa94207f3bf MD5 · raw file

  1. package org.loon.framework.android.game.action.avg.command;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.Serializable;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. import java.util.List;
  12. import java.util.Set;
  13. import java.util.Map.Entry;
  14. import org.loon.framework.android.game.core.LRelease;
  15. import org.loon.framework.android.game.core.LSystem;
  16. import org.loon.framework.android.game.core.resource.Resources;
  17. import org.loon.framework.android.game.utils.CollectionUtils;
  18. import org.loon.framework.android.game.utils.StringUtils;
  19. import org.loon.framework.android.game.utils.collection.ArrayMap;
  20. /**
  21. * Copyright 2008 - 2010
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  24. * use this file except in compliance with the License. You may obtain a copy of
  25. * the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  31. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  32. * License for the specific language governing permissions and limitations under
  33. * the License.
  34. *
  35. * @project loonframework
  36. * @author chenpeng
  37. * @email ceponline@yahoo.com.cn
  38. * @version 0.1.2
  39. */
  40. @SuppressWarnings("unchecked")
  41. public class Command extends Conversion implements Serializable, LRelease {
  42. /**
  43. *
  44. */
  45. private static final long serialVersionUID = 1L;
  46. // ????
  47. private static HashMap scriptLazy;
  48. // ??????
  49. private static HashMap scriptContext;
  50. // ????
  51. private static ArrayMap functions;
  52. // ????
  53. private static HashMap setEnvironmentList;
  54. // ??????
  55. private static ArrayMap conditionEnvironmentList;
  56. // ??????
  57. private static StringBuffer reader;
  58. // ?????
  59. private String cacheCommandName;
  60. // ?????
  61. private boolean flaging = false;
  62. // ?????
  63. private boolean ifing = false;
  64. // ?????
  65. private boolean functioning = false;
  66. // ????
  67. private boolean esleflag = false;
  68. private boolean backIfBool = false;
  69. private String executeCommand;
  70. private String nowPosFlagName;
  71. private boolean addCommand;
  72. private boolean isInnerCommand;
  73. private boolean isRead;
  74. private boolean isCall;
  75. private boolean isCache;
  76. private boolean if_bool;
  77. private boolean elseif_bool;
  78. private Command innerCommand;
  79. private List temps;
  80. private List printTags;
  81. private List randTags;
  82. private int scriptSize;
  83. private int offsetPos;
  84. // ??????
  85. private String[] scriptList;
  86. // ???
  87. private String scriptName;
  88. /**
  89. * ?????????????
  90. *
  91. * @param fileName
  92. */
  93. public Command(String fileName) {
  94. initCommand();
  95. formatCommand(fileName);
  96. }
  97. /**
  98. * ?????????list??
  99. *
  100. * @param resource
  101. */
  102. public Command(String fileName, String[] res) {
  103. initCommand();
  104. formatCommand("function", res);
  105. }
  106. public static void initCommand() {
  107. if (scriptContext == null) {
  108. scriptContext = new HashMap(1500);
  109. functions = new ArrayMap(20);
  110. setEnvironmentList = new HashMap(20);
  111. conditionEnvironmentList = new ArrayMap(30);
  112. reader = new StringBuffer(3000);
  113. }
  114. }
  115. public void formatCommand(String fileName) {
  116. formatCommand(fileName, Command.includeFile(fileName));
  117. }
  118. public void formatCommand(String name, String[] res) {
  119. if (functions != null) {
  120. functions.clear();
  121. }
  122. conditionEnvironmentList.clear();
  123. setEnvironmentList.put(V_SELECT_KEY, "-1");
  124. scriptName = name;
  125. scriptList = res;
  126. scriptSize = res.length;
  127. offsetPos = 0;
  128. flaging = false;
  129. ifing = false;
  130. isCache = true;
  131. esleflag = false;
  132. backIfBool = false;
  133. functioning = false;
  134. esleflag = false;
  135. backIfBool = false;
  136. addCommand = false;
  137. isInnerCommand = false;
  138. isRead = false;
  139. isCall = false;
  140. isCache = false;
  141. if_bool = false;
  142. elseif_bool = false;
  143. }
  144. private boolean setupIF(String commandString, String nowPosFlagName,
  145. HashMap setEnvironmentList, ArrayMap conditionEnvironmentList) {
  146. boolean result = false;
  147. conditionEnvironmentList.put(nowPosFlagName, new Boolean(false));
  148. try {
  149. List temps = commandSplit(commandString);
  150. Object valueA = (String) temps.get(1);
  151. Object valueB = (String) temps.get(3);
  152. valueA = setEnvironmentList.get(valueA) == null ? valueA
  153. : setEnvironmentList.get(valueA);
  154. valueB = setEnvironmentList.get(valueB) == null ? valueB
  155. : setEnvironmentList.get(valueB);
  156. // ????
  157. if (!isNumber(valueB)) {
  158. try {
  159. // ??????????
  160. valueB = compute.parse(valueB);
  161. } catch (Exception e) {
  162. }
  163. }
  164. String condition = (String) temps.get(2);
  165. // ????
  166. if (valueA == null || valueB == null) {
  167. conditionEnvironmentList
  168. .put(nowPosFlagName, new Boolean(false));
  169. }
  170. // ??
  171. if ("==".equals(condition)) {
  172. conditionEnvironmentList.put(nowPosFlagName, new Boolean(
  173. result = valueA.toString().equals(valueB.toString())));
  174. // ??
  175. } else if ("!=".equals(condition)) {
  176. conditionEnvironmentList.put(nowPosFlagName, new Boolean(
  177. result = !valueA.toString().equals(valueB.toString())));
  178. // ??
  179. } else if (">".equals(condition)) {
  180. int numberA = Integer.parseInt(valueA.toString());
  181. int numberB = Integer.parseInt(valueB.toString());
  182. conditionEnvironmentList.put(nowPosFlagName, new Boolean(
  183. result = numberA > numberB));
  184. // ??
  185. } else if ("<".equals(condition)) {
  186. int numberA = Integer.parseInt(valueA.toString());
  187. int numberB = Integer.parseInt(valueB.toString());
  188. conditionEnvironmentList.put(nowPosFlagName, new Boolean(
  189. result = numberA < numberB));
  190. // ????
  191. } else if (">=".equals(condition)) {
  192. int numberA = Integer.parseInt(valueA.toString());
  193. int numberB = Integer.parseInt(valueB.toString());
  194. conditionEnvironmentList.put(nowPosFlagName, new Boolean(
  195. result = numberA >= numberB));
  196. // ????
  197. } else if ("<=".equals(condition)) {
  198. int numberA = Integer.parseInt(valueA.toString());
  199. int numberB = Integer.parseInt(valueB.toString());
  200. conditionEnvironmentList.put(nowPosFlagName, new Boolean(
  201. result = numberA <= numberB));
  202. }
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. }
  206. return result;
  207. }
  208. /**
  209. * ??????
  210. *
  211. */
  212. public void openCache() {
  213. isCache = true;
  214. }
  215. /**
  216. * ??????
  217. *
  218. */
  219. public void closeCache() {
  220. isCache = false;
  221. }
  222. /**
  223. * ????????
  224. *
  225. * @return
  226. */
  227. public String nowCacheOffsetName(String cmd) {
  228. return (scriptName + FLAG + offsetPos + FLAG + cmd).toLowerCase();
  229. }
  230. /**
  231. * ??????
  232. *
  233. */
  234. public static void resetCache() {
  235. if (scriptContext != null) {
  236. scriptContext.clear();
  237. }
  238. }
  239. public boolean isRead() {
  240. return isRead;
  241. }
  242. public void setRead(boolean isRead) {
  243. this.isRead = isRead;
  244. }
  245. /**
  246. * ???????????
  247. *
  248. * @return
  249. */
  250. public synchronized String[] getReads() {
  251. String result = reader.toString();
  252. result = result.replaceAll(SELECTS_TAG, "");
  253. return split(result, FLAG);
  254. }
  255. /**
  256. * ???????????
  257. *
  258. * @param index
  259. * @return
  260. */
  261. public synchronized String getRead(int index) {
  262. try {
  263. return getReads()[index];
  264. } catch (Exception e) {
  265. return null;
  266. }
  267. }
  268. /**
  269. * ????????????
  270. *
  271. * @param messages
  272. * @param startString
  273. * @param endString
  274. * @return
  275. */
  276. public static String getNameTag(String messages, String startString,
  277. String endString) {
  278. List results = getNameTags(messages, startString, endString);
  279. return (results == null || results.size() == 0) ? null
  280. : (String) results.get(0);
  281. }
  282. /**
  283. * ?????????list
  284. *
  285. * @param messages
  286. * @param startString
  287. * @param endString
  288. * @return
  289. */
  290. public static List getNameTags(String messages, String startString,
  291. String endString) {
  292. return Command.getNameTags(messages.toCharArray(), startString
  293. .toCharArray(), endString.toCharArray());
  294. }
  295. /**
  296. * ?????????list
  297. *
  298. * @param messages
  299. * @param startString
  300. * @param endString
  301. * @return
  302. */
  303. public static List getNameTags(char[] messages, char[] startString,
  304. char[] endString) {
  305. int dlength = messages.length;
  306. int slength = startString.length;
  307. int elength = endString.length;
  308. List tagList = new ArrayList(10);
  309. boolean lookup = false;
  310. int lookupStartIndex = 0;
  311. int lookupEndIndex = 0;
  312. int length;
  313. StringBuffer sbr = new StringBuffer(100);
  314. for (int i = 0; i < dlength; i++) {
  315. char tag = messages[i];
  316. if (tag == startString[lookupStartIndex]) {
  317. lookupStartIndex++;
  318. }
  319. if (lookupStartIndex == slength) {
  320. lookupStartIndex = 0;
  321. lookup = true;
  322. }
  323. if (lookup) {
  324. sbr.append(tag);
  325. }
  326. if (tag == endString[lookupEndIndex]) {
  327. lookupEndIndex++;
  328. }
  329. if (lookupEndIndex == elength) {
  330. lookupEndIndex = 0;
  331. lookup = false;
  332. length = sbr.length();
  333. if (length > 0) {
  334. tagList.add(sbr.substring(1, sbr.length() - elength));
  335. sbr.delete(0, length);
  336. }
  337. }
  338. }
  339. return tagList;
  340. }
  341. /**
  342. * ??????
  343. *
  344. * @param type
  345. */
  346. public void select(int type) {
  347. if (innerCommand != null) {
  348. innerCommand.setVariable(V_SELECT_KEY, String.valueOf(type));
  349. }
  350. setVariable(V_SELECT_KEY, String.valueOf(type));
  351. }
  352. public String getSelect() {
  353. return (String) getVariable(V_SELECT_KEY);
  354. }
  355. /**
  356. * ????
  357. *
  358. * @param key
  359. * @param value
  360. */
  361. public void setVariable(String key, Object value) {
  362. setEnvironmentList.put(key, value);
  363. }
  364. /**
  365. * ??????
  366. *
  367. * @param vars
  368. */
  369. public void setVariables(HashMap vars) {
  370. setEnvironmentList.putAll(vars);
  371. }
  372. /**
  373. * ??????
  374. *
  375. * @return
  376. */
  377. public HashMap getVariables() {
  378. return setEnvironmentList;
  379. }
  380. public Object getVariable(String key) {
  381. return setEnvironmentList.get(key);
  382. }
  383. /**
  384. * ????
  385. *
  386. * @param key
  387. */
  388. public void removeVariable(String key) {
  389. setEnvironmentList.remove(key);
  390. }
  391. /**
  392. * ????????????
  393. *
  394. * @return
  395. */
  396. public boolean next() {
  397. return (offsetPos < scriptSize);
  398. }
  399. /**
  400. * ?????????
  401. *
  402. * @param offset
  403. * @return
  404. */
  405. public boolean gotoIndex(final int offset) {
  406. boolean result = offset < scriptSize && offset > 0
  407. && offset != offsetPos;
  408. if (result) {
  409. offsetPos = offset;
  410. }
  411. return result;
  412. }
  413. /**
  414. * ?????????????list??
  415. *
  416. * @return
  417. */
  418. public List batchToList() {
  419. List reslist = new ArrayList(scriptSize);
  420. for (; next();) {
  421. String execute = doExecute();
  422. if (execute != null) {
  423. reslist.add(execute);
  424. }
  425. }
  426. return reslist;
  427. }
  428. /**
  429. * ?????????????string??
  430. *
  431. * @return
  432. */
  433. public String batchToString() {
  434. StringBuffer resString = new StringBuffer(scriptSize * 10);
  435. for (; next();) {
  436. String execute = doExecute();
  437. if (execute != null) {
  438. resString.append(execute);
  439. resString.append("\n");
  440. }
  441. }
  442. return resString.toString();
  443. }
  444. private void setupSET(String cmd) {
  445. if (cmd.startsWith(SET_TAG)) {
  446. List temps = commandSplit(cmd);
  447. int len = temps.size();
  448. String result = null;
  449. if (len == 4) {
  450. result = temps.get(3).toString();
  451. } else if (len > 4) {
  452. StringBuffer sbr = new StringBuffer(len);
  453. for (int i = 3; i < temps.size(); i++) {
  454. sbr.append(temps.get(i));
  455. }
  456. result = sbr.toString();
  457. }
  458. if (result != null) {
  459. // ????????
  460. Set set = setEnvironmentList.entrySet();
  461. for (Iterator it = set.iterator(); it.hasNext();) {
  462. Entry entry = (Entry) it.next();
  463. if (!(result.startsWith("\"") && result.endsWith("\""))) {
  464. result = replaceMatch(result, (String) entry.getKey(),
  465. entry.getValue().toString());
  466. }
  467. }
  468. // ????????
  469. if (result.startsWith("\"") && result.endsWith("\"")) {
  470. setEnvironmentList.put(temps.get(1), result.substring(1,
  471. result.length() - 1));
  472. } else if (isChinese(result) || isEnglishAndNumeric(result)) {
  473. setEnvironmentList.put(temps.get(1), result);
  474. } else {
  475. // ????????
  476. setEnvironmentList.put(temps.get(1), compute.parse(result));
  477. }
  478. }
  479. addCommand = false;
  480. }
  481. }
  482. /**
  483. * ?????
  484. *
  485. */
  486. private void setupRandom(String cmd) {
  487. // ?????
  488. if (cmd.indexOf(RAND_TAG) != -1) {
  489. randTags = Command.getNameTags(cmd, RAND_TAG + BRACKET_LEFT_TAG,
  490. BRACKET_RIGHT_TAG);
  491. if (randTags != null) {
  492. for (Iterator it = randTags.iterator(); it.hasNext();) {
  493. String key = (String) it.next();
  494. Object value = setEnvironmentList.get(key);
  495. // ?????
  496. if (value != null) {
  497. cmd = Command.replaceMatch(cmd, (RAND_TAG
  498. + BRACKET_LEFT_TAG + key + BRACKET_RIGHT_TAG)
  499. .intern(), value.toString());
  500. // ??????????
  501. } else if (isNumber(key)) {
  502. cmd = Command.replaceMatch(cmd, (RAND_TAG
  503. + BRACKET_LEFT_TAG + key + BRACKET_RIGHT_TAG)
  504. .intern(), String.valueOf(GLOBAL_RAND
  505. .nextInt(Integer.parseInt((String) key))));
  506. // ???
  507. } else {
  508. cmd = Command.replaceMatch(cmd, (RAND_TAG
  509. + BRACKET_LEFT_TAG + key + BRACKET_RIGHT_TAG)
  510. .intern(), String
  511. .valueOf(GLOBAL_RAND.nextInt()));
  512. }
  513. }
  514. }
  515. }
  516. }
  517. private void innerCallTrue() {
  518. isCall = true;
  519. isInnerCommand = true;
  520. }
  521. private void innerCallFalse() {
  522. isCall = false;
  523. isInnerCommand = false;
  524. innerCommand = null;
  525. }
  526. /**
  527. * ????????
  528. *
  529. * @return
  530. */
  531. public synchronized String doExecute() {
  532. this.executeCommand = null;
  533. this.addCommand = true;
  534. this.isInnerCommand = (innerCommand != null);
  535. this.if_bool = false;
  536. this.elseif_bool = false;
  537. try {
  538. // ??call??
  539. if (isInnerCommand && isCall) {
  540. setVariables(innerCommand.getVariables());
  541. if (innerCommand.next()) {
  542. return innerCommand.doExecute();
  543. } else {
  544. innerCallFalse();
  545. return executeCommand;
  546. }
  547. // ??????
  548. } else if (isInnerCommand && !isCall) {
  549. setVariables(innerCommand.getVariables());
  550. if (innerCommand.next()) {
  551. return innerCommand.doExecute();
  552. } else {
  553. innerCommand = null;
  554. isInnerCommand = false;
  555. return executeCommand;
  556. }
  557. }
  558. nowPosFlagName = String.valueOf(offsetPos);
  559. int length = conditionEnvironmentList.size();
  560. if (length > 0) {
  561. Object ifResult = conditionEnvironmentList.get(length - 1);
  562. if (ifResult != null) {
  563. backIfBool = ((Boolean) ifResult).booleanValue();
  564. }
  565. }
  566. // ??????
  567. String cmd = scriptList[offsetPos];
  568. // ??????
  569. if (cmd.startsWith(RESET_CACHE_TAG)) {
  570. resetCache();
  571. return executeCommand;
  572. }
  573. if (isCache) {
  574. // ????????
  575. cacheCommandName = nowCacheOffsetName(cmd);
  576. // ???????
  577. Object cache = scriptContext.get(cacheCommandName);
  578. if (cache != null) {
  579. return (String) cache;
  580. }
  581. }
  582. // ???
  583. if (flaging) {
  584. flaging = !(cmd.startsWith(FLAG_LS_E_TAG) || cmd
  585. .endsWith(FLAG_LS_E_TAG));
  586. return executeCommand;
  587. }
  588. if (!flaging) {
  589. // ????
  590. if (cmd.startsWith(FLAG_LS_B_TAG)
  591. && !cmd.endsWith(FLAG_LS_E_TAG)) {
  592. flaging = true;
  593. return executeCommand;
  594. } else if (cmd.startsWith(FLAG_LS_B_TAG)
  595. && cmd.endsWith(FLAG_LS_E_TAG)) {
  596. return executeCommand;
  597. }
  598. }
  599. // ???????
  600. setupRandom(cmd);
  601. // ????????
  602. setupSET(cmd);
  603. // ??????????
  604. if (cmd.endsWith(END_TAG)) {
  605. functioning = false;
  606. return executeCommand;
  607. }
  608. // ??????????
  609. if (cmd.startsWith(BEGIN_TAG)) {
  610. temps = commandSplit(cmd);
  611. if (temps.size() == 2) {
  612. functioning = true;
  613. functions.put(temps.get(1), new String[0]);
  614. return executeCommand;
  615. }
  616. }
  617. // ???????
  618. if (functioning) {
  619. int size = functions.size() - 1;
  620. String[] function = (String[]) functions.get(size);
  621. int index = function.length;
  622. function = (String[]) CollectionUtils.expand(function, 1);
  623. function[index] = cmd;
  624. functions.set(size, function);
  625. return executeCommand;
  626. }
  627. // ?????????
  628. if (((!esleflag && !ifing) || (esleflag && ifing))
  629. && cmd.startsWith(CALL_TAG) && !isCall) {
  630. temps = commandSplit(cmd);
  631. if (temps.size() == 2) {
  632. String functionName = (String) temps.get(1);
  633. String[] funs = (String[]) functions.get(functionName);
  634. if (funs != null) {
  635. innerCommand = new Command(scriptName + FLAG
  636. + functionName, funs);
  637. innerCommand.closeCache();
  638. innerCommand.setVariables(getVariables());
  639. innerCallTrue();
  640. return null;
  641. }
  642. }
  643. }
  644. if (!if_bool && !elseif_bool) {
  645. // ????????
  646. if_bool = cmd.startsWith(IF_TAG);
  647. elseif_bool = cmd.startsWith(ELSE_TAG);
  648. }
  649. // ????a
  650. if (if_bool) {
  651. esleflag = setupIF(cmd, nowPosFlagName, setEnvironmentList,
  652. conditionEnvironmentList);
  653. addCommand = false;
  654. ifing = true;
  655. // ????b
  656. } else if (elseif_bool) {
  657. String[] value = split(cmd, " ");
  658. if (!backIfBool && !esleflag) {
  659. // ??if??
  660. if (value.length > 1 && IF_TAG.equals(value[1])) {
  661. esleflag = setupIF(cmd.replaceAll(ELSE_TAG, "").trim(),
  662. nowPosFlagName, setEnvironmentList,
  663. conditionEnvironmentList);
  664. addCommand = false;
  665. // ???else
  666. } else if (value.length == 1 && ELSE_TAG.equals(value[0])) {
  667. esleflag = setupIF("if 1==1", nowPosFlagName,
  668. setEnvironmentList, conditionEnvironmentList);
  669. addCommand = false;
  670. }
  671. } else {
  672. esleflag = false;
  673. addCommand = false;
  674. conditionEnvironmentList.put(nowPosFlagName, new Boolean(
  675. false));
  676. }
  677. }
  678. // ????
  679. if (cmd.startsWith(IF_END_TAG)) {
  680. conditionEnvironmentList.clear();
  681. backIfBool = false;
  682. addCommand = false;
  683. ifing = false;
  684. if_bool = false;
  685. elseif_bool = false;
  686. return null;
  687. }
  688. if (backIfBool) {
  689. // ??????
  690. if (cmd.startsWith(INCLUDE_TAG)) {
  691. temps = commandSplit(cmd);
  692. String fileName = (String) temps.get(1);
  693. if (fileName != null) {
  694. innerCommand = new Command(fileName);
  695. isInnerCommand = true;
  696. return null;
  697. }
  698. }
  699. } else if (cmd.startsWith(INCLUDE_TAG) && !ifing && !backIfBool
  700. && !esleflag) {
  701. temps = commandSplit(cmd);
  702. String fileName = (String) temps.get(1);
  703. if (fileName != null) {
  704. innerCommand = new Command(fileName);
  705. isInnerCommand = true;
  706. return null;
  707. }
  708. }
  709. // ???????
  710. if (cmd.startsWith(OUT_TAG)) {
  711. isRead = false;
  712. addCommand = false;
  713. executeCommand = (SELECTS_TAG + " " + reader.toString())
  714. .intern();
  715. }
  716. // ?????
  717. if (isRead) {
  718. reader.append(cmd);
  719. reader.append(FLAG);
  720. addCommand = false;
  721. }
  722. // ?????
  723. if (cmd.startsWith(IN_TAG)) {
  724. reader.delete(0, reader.length());
  725. isRead = true;
  726. return executeCommand;
  727. }
  728. // ??????
  729. if (addCommand && ifing) {
  730. if (backIfBool && esleflag) {
  731. executeCommand = cmd;
  732. }
  733. } else if (addCommand) {
  734. executeCommand = cmd;
  735. }
  736. // ?????????
  737. if (executeCommand != null) {
  738. printTags = Command.getNameTags(executeCommand, PRINT_TAG
  739. + BRACKET_LEFT_TAG, BRACKET_RIGHT_TAG);
  740. if (printTags != null) {
  741. for (Iterator it = printTags.iterator(); it.hasNext();) {
  742. String key = (String) it.next();
  743. Object value = setEnvironmentList.get(key);
  744. if (value != null) {
  745. executeCommand = Command
  746. .replaceMatch(
  747. executeCommand,
  748. (PRINT_TAG + BRACKET_LEFT_TAG + key + BRACKET_RIGHT_TAG)
  749. .intern(), value.toString());
  750. } else {
  751. executeCommand = Command
  752. .replaceMatch(
  753. executeCommand,
  754. (PRINT_TAG + BRACKET_LEFT_TAG + key + BRACKET_RIGHT_TAG)
  755. .intern(), key);
  756. }
  757. }
  758. }
  759. if (isCache) {
  760. // ??????
  761. scriptContext.put(cacheCommandName, executeCommand);
  762. }
  763. }
  764. } catch (Exception ex) {
  765. throw new RuntimeException(ex);
  766. } finally {
  767. if (!isInnerCommand) {
  768. offsetPos++;
  769. }
  770. }
  771. return executeCommand;
  772. }
  773. /**
  774. * ????????
  775. *
  776. * @param fileName
  777. * @return
  778. */
  779. private static String[] includeFile(String fileName) {
  780. if (scriptLazy == null) {
  781. scriptLazy = new HashMap(10);
  782. }
  783. String key = fileName.trim().toLowerCase();
  784. String[] result = (String[]) scriptLazy.get(key);
  785. if (result == null) {
  786. InputStream in = null;
  787. BufferedReader reader = null;
  788. result = new String[0];
  789. int index = 0;
  790. try {
  791. in = Resources.openResource(fileName);
  792. reader = new BufferedReader(new InputStreamReader(in,
  793. LSystem.encoding));
  794. String record = null;
  795. while ((record = reader.readLine()) != null) {
  796. record = record.trim();
  797. if (record.length() > 0) {
  798. if (!(record.startsWith(FLAG_L_TAG)
  799. || record.startsWith(FLAG_C_TAG) || record
  800. .startsWith(FLAG_I_TAG))) {
  801. result = (String[]) CollectionUtils.expand(result,
  802. 1);
  803. result[index] = record;
  804. index++;
  805. }
  806. }
  807. }
  808. } catch (Exception ex) {
  809. throw new RuntimeException(ex);
  810. } finally {
  811. if (in != null) {
  812. try {
  813. in.close();
  814. in = null;
  815. } catch (IOException e) {
  816. e.printStackTrace();
  817. }
  818. }
  819. if (reader != null) {
  820. try {
  821. reader.close();
  822. reader = null;
  823. } catch (IOException e) {
  824. e.printStackTrace();
  825. }
  826. }
  827. }
  828. scriptLazy.put(key, result);
  829. return result;
  830. } else {
  831. return CollectionUtils.copyOf(result);
  832. }
  833. }
  834. /**
  835. * ???????????list
  836. *
  837. * @param src
  838. * @return
  839. */
  840. public static List commandSplit(final String src) {
  841. String[] cmds;
  842. String result = src.trim();
  843. result = result.replaceAll("\r", "");
  844. result = FLAG + result;
  845. result = result.replaceAll("\t", FLAG);
  846. if (StringUtils.charCount(result, '=') == 1) {
  847. result = result.replaceAll(" ", FLAG);
  848. result = result.replaceAll("=", (FLAG + "=" + FLAG).intern());
  849. } else {
  850. result = result.replaceAll(" ", FLAG);
  851. result = result.replaceAll("<=", (FLAG + "<=" + FLAG).intern());
  852. result = result.replaceAll(">=", (FLAG + ">=" + FLAG).intern());
  853. result = result.replaceAll("==", (FLAG + "==" + FLAG).intern());
  854. result = result.replaceAll("!=", (FLAG + "!=" + FLAG).intern());
  855. if (result.indexOf("<=") == -1) {
  856. result = result.replaceAll("<", (FLAG + "<" + FLAG).intern());
  857. }
  858. if (result.indexOf(">=") == -1) {
  859. result = result.replaceAll(">", (FLAG + ">" + FLAG).intern());
  860. }
  861. }
  862. result = result.replaceAll((FLAG + "{2,}").intern(), FLAG);
  863. result = result.substring(1);
  864. cmds = result.split(FLAG);
  865. return Arrays.asList(cmds);
  866. }
  867. public final static void close() {
  868. if (reader != null) {
  869. reader.delete(0, reader.length());
  870. }
  871. if (setEnvironmentList != null) {
  872. setEnvironmentList.clear();
  873. }
  874. if (conditionEnvironmentList != null) {
  875. conditionEnvironmentList.clear();
  876. }
  877. if (functions != null) {
  878. functions.clear();
  879. }
  880. if (scriptContext != null) {
  881. scriptContext.clear();
  882. }
  883. if (scriptLazy != null) {
  884. scriptLazy.clear();
  885. }
  886. }
  887. public void dispose() {
  888. if (temps != null) {
  889. temps.clear();
  890. temps = null;
  891. }
  892. if (printTags != null) {
  893. printTags.clear();
  894. printTags = null;
  895. }
  896. if (randTags != null) {
  897. randTags.clear();
  898. randTags = null;
  899. }
  900. }
  901. }