Source code: jpicedt/format/pstricks/parser/PsObjectExpression.java
1 /*
2 PsObjectExpression.java - August 4, 2002 - jPicEdt 1.3.2, a picture editor for LaTeX.
3 Copyright (C) 1999-2002 Sylvain Reynal
4
5 Département de Physique
6 Ecole Nationale Supérieure de l'Electronique et de ses Applications (ENSEA)
7 6, avenue du Ponceau
8 F-95014 CERGY CEDEX
9
10 Tel : +33 130 736 245
11 Fax : +33 130 736 667
12 e-mail : reynal@ensea.fr
13 jPicEdt web page : http://www.jpicedt.org/
14
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License
17 as published by the Free Software Foundation; either version 2
18 of the License, or any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30 package jpicedt.format.pstricks.parser;
31
32 import jpicedt.graphic.io.parser.*;
33 import jpicedt.graphic.model.*;
34
35 import java.util.*;
36
37 /**
38 * Handles <b>\\newpsobject{myobj}{ps_object}{par1=val1,...}</B>, for instance
39 * <b>\\newpsobject{myline}{psline}{linecolor=green}</B> (param is optional, though in this case, it'd be pretty useless!)
40 * @author $Author: reynal $
41 * @version $Id: PsObjectExpression.java,v 1.2 2002/08/05 16:44:10 reynal Exp $
42 */
43 public class PsObjectExpression extends AbstractRegularExpression {
44
45 private Pool pool;
46 private AbstractRegularExpression exp1,exp2,exp3;
47 private String macroName; // e.g. "\\myline"
48 private String shape; // e.g. "psline"
49 private String param; // e.g. "linecolor=green,fillstyle=solid"
50 private PstricksParser parser; // used to add new rules
51 private Element prototype;
52
53 public PsObjectExpression(PstricksParser p, Pool pl){
54 this.pool = pl;
55 this.parser = p;
56 // myobj}
57 exp1 = new WordExpression("}", true, true, true){ // no line-feed + postfix.
58 public void action(ParserEvent e){
59 if (DEBUG) System.out.println(e);
60 macroName = "\\" + getValue(); // e.g. "\\PST@Border"
61 }};
62 // ps_object}
63 exp2 = new WordExpression("}", true, true, true){ // no line-feed
64 public void action(ParserEvent e){
65 if (DEBUG) System.out.println(e);
66 shape = getValue(); // e.g. "psline" or "psbezier"
67 }};
68 // params :
69 exp3 = new OptionalExpression(new EnclosingExpression("{",null,"}"){
70 public void action(ParserEvent e){
71 if (DEBUG) System.out.println(e);
72 param = getEnclosedString(); // e.g. "linecolor=green,fillstyle=solid"
73 }});
74
75
76 }
77
78 public boolean interpret(Context context) throws ParserException {
79
80 int oldCaretPos = context.getCaretPosition(); // if fails in the end
81
82 if (!context.matchAndMove("\\newpsobject{")) return false;
83 WHITE_SPACES_OR_EOL.interpret(context);
84
85 // retrieve "macroName" :
86 if (!exp1.interpret(context)) throw new ParserException.IncompleteSequence(context,this);
87 WHITE_SPACES_OR_EOL.interpret(context);
88
89 // retrieve "shape" :
90 if (!context.matchAndMove("{")) throw new ParserException.IncompleteSequence(context,this);
91 if (!exp2.interpret(context)) throw new ParserException.IncompleteSequence(context,this);
92
93 // (optionally) fills attribute set assoc. with PstricksParser.KEY_ATTRIBUTES_PSOBJECTS :
94 if (exp3.interpret(context)){
95
96 // associates macroName with paramString for later use at instanciation time,
97 // e.g. associates "\\psline" with "linecolor=green" :
98 HashMap map = (HashMap)pool.get(PstricksParser.KEY_NEWPSOBJECTS);
99 map.put(macroName,param);
100 }
101
102 // add new rule to parser :
103 if (shape.equals("psbezier")) {
104 parser.add(new PsBezierExpression(pool,macroName));
105 return true;
106 }
107 else if (shape.equals("pscircle")) {
108 parser.add(new PsCircleExpression(pool,macroName));
109 return true;
110 }
111 else if (shape.equals("psellipse")) {
112 parser.add(new PsEllipseExpression(pool,macroName));
113 return true;
114 }
115 else if (shape.equals("psframe")) {
116 parser.add(new PsFrameExpression(pool,macroName));
117 return true;
118 }
119 else if (shape.equals("pspolygon")) {
120 parser.add(new PsPolygonExpression(pool,PsPolygonExpression.POLYGON,macroName));
121 return true;
122 }
123 else if (shape.equals("psline")) {
124 parser.add(new PsPolygonExpression(pool,PsPolygonExpression.LINE,macroName));
125 return true;
126 }
127 else if (shape.equals("psdots")) {
128 parser.add(new PsPolygonExpression(pool,PsPolygonExpression.DOTS,macroName));
129 return true;
130 }
131 else if (shape.equals("psarc")) {
132 parser.add(new PsArcExpression(pool,PsArcExpression.ARC,macroName));
133 return true;
134 }
135 else if (shape.equals("pswedge")) {
136 parser.add(new PsArcExpression(pool,PsArcExpression.WEDGE,macroName));
137 return true;
138 }
139 else { // shape not supported
140 context.moveCaretTo(oldCaretPos);
141 return false; // skip, and go to next expression in the parser tree
142 }
143
144
145 }
146
147 public String toString(){
148 return "[PsObjectExpression]";
149 }
150
151 }