11package fr .lip6 .move .gal .mcc .properties ;
22
33import java .util .ArrayList ;
4- import java .util .Arrays ;
54import java .util .HashMap ;
65import java .util .List ;
76import java .util .Map ;
@@ -25,7 +24,8 @@ public class PropHandler extends DefaultHandler {
2524
2625 private boolean dotext = false ;
2726 private boolean isLTL = false ;
28-
27+ private StringBuilder textBuffer = new StringBuilder (); // Added for text accumulation
28+
2929 private PetriNet spec ;
3030
3131 public PropHandler (PetriNet spn , boolean isLTL ) {
@@ -34,8 +34,8 @@ public PropHandler(PetriNet spn, boolean isLTL) {
3434 }
3535
3636 @ Override
37- public final void startElement (String uri , String localName ,
38- String baliseName , Attributes attributes ) throws SAXException {
37+ public final void startElement (String uri , String localName , String baliseName , Attributes attributes )
38+ throws SAXException {
3939 if ("property" .equals (baliseName )) { //$NON-NLS-1$
4040 Property pdesc = new Property ();
4141 stack .push (pdesc );
@@ -91,46 +91,34 @@ public final void startElement(String uri, String localName,
9191 // NOTHING
9292 } else if ("next" .equals (baliseName )) { //$NON-NLS-1$
9393 // NOTHING
94- } else if ("description" .equals (baliseName )) { //$NON-NLS-1$
95- dotext = true ;
96- } else if ("place" .equals (baliseName )) { //$NON-NLS-1$
97- dotext = true ;
98- } else if ("integer-constant" .equals (baliseName )) { //$NON-NLS-1$
99- dotext = true ;
100- } else if ("id" .equals (baliseName )) { //$NON-NLS-1$
101- dotext = true ;
102- } else if ("transition" .equals (baliseName )) { //$NON-NLS-1$
94+ } else if ("description" .equals (baliseName ) || "place" .equals (baliseName )
95+ || "integer-constant" .equals (baliseName ) || "id" .equals (baliseName )
96+ || "transition" .equals (baliseName )) { //$NON-NLS-1$
10397 dotext = true ;
98+ textBuffer .setLength (0 ); // Clear buffer at start of text elements
10499 } else if ("globally" .equals (baliseName )) { //$NON-NLS-1$
105- // NOTHING
100+ // NOTHING
106101 } else {
107102 getLog ().warning ("Unexpected XML tag in property file :" + baliseName );
108103 }
109104 }
110105
111106 @ Override
112- public void characters (char [] chars , int beg , int length )
113- throws SAXException {
107+ public void characters (char [] chars , int beg , int length ) throws SAXException {
114108 if (dotext ) {
115- String name = new String (Arrays .copyOfRange (chars , beg , beg
116- + length ));
117- stack .push (name );
118-
109+ textBuffer .append (chars , beg , length ); // Accumulate text
119110 }
120111 }
121112
122113 @ Override
123- public void endElement (String uri , String localName , String baliseName )
124- throws SAXException {
125- if ("property" .equals (baliseName )) { //$NON-NLS-1$
114+ public void endElement (String uri , String localName , String baliseName ) throws SAXException {
115+ if ("property" .equals (baliseName )) { //$NON-NLS-1$
126116 spec .getProperties ().add ((Property ) stack .pop ());
127-
128117 // reset to null ?
129118 } else if ("formula" .equals (baliseName )) { //$NON-NLS-1$
130119 Expression child = (Expression ) stack .pop ();
131120 Property pdesc = (Property ) stack .peek ();
132121 pdesc .setBody (child );
133-
134122 } else if ("integer-le" .equals (baliseName )) { //$NON-NLS-1$
135123 popBinary (Op .LEQ );
136124 } else if ("integer-lt" .equals (baliseName )) { //$NON-NLS-1$
@@ -146,34 +134,34 @@ public void endElement(String uri, String localName, String baliseName)
146134 } else if ("negation" .equals (baliseName )) { //$NON-NLS-1$
147135 stack .push (Expression .not ((Expression ) stack .pop ()));
148136 } else if ("is-fireable" .equals (baliseName )) { //$NON-NLS-1$
149-
137+ // No-op
150138 } else if ("tokens-count" .equals (baliseName )) { //$NON-NLS-1$
151-
139+ // No-op
152140 } else if ("deadlock" .equals (baliseName )) {
153141 stack .push (Expression .op (Op .DEAD , null , null ));
154142 } else if ("description" .equals (baliseName )) { //$NON-NLS-1$
155- String name = ( String ) stack . pop ();
143+ String name = textBuffer . toString (). trim ();
156144 Property prop = (Property ) stack .peek ();
157145 // prop.setComment(name);
158146 dotext = false ;
159-
160- } else if ("place" .equals (baliseName )) {
161- String name = ( String ) stack . pop ();
147+ textBuffer . setLength ( 0 );
148+ } else if ("place" .equals (baliseName )) {
149+ String name = textBuffer . toString (). trim ();
162150 NaryOp context = (NaryOp ) stack .peek ();
163151 context .addChild (Expression .var (findPlace (name )));
164152 dotext = false ;
165-
153+ textBuffer . setLength ( 0 );
166154 } else if ("integer-constant" .equals (baliseName )) {
167- String name = ( String ) stack . pop ();
155+ String name = textBuffer . toString (). trim ();
168156 stack .push (Expression .constant (Integer .parseInt (name )));
169157 dotext = false ;
170-
158+ textBuffer . setLength ( 0 );
171159 } else if ("id" .equals (baliseName )) { //$NON-NLS-1$
172- String name = ( String ) stack . pop ();
160+ String name = textBuffer . toString (). trim ();
173161 Property prop = (Property ) stack .peek ();
174- prop .setName (name );
162+ prop .setName (name );
175163 dotext = false ;
176-
164+ textBuffer . setLength ( 0 );
177165 } else if ("disjunction" .equals (baliseName )) { //$NON-NLS-1$
178166 popNary (Op .OR );
179167 } else if ("conjunction" .equals (baliseName )) { //$NON-NLS-1$
@@ -182,18 +170,20 @@ public void endElement(String uri, String localName, String baliseName)
182170 popNary (Op .MULT );
183171 } else if ("sum" .equals (baliseName )) { //$NON-NLS-1$
184172 popNary (Op .ADD );
185- } else if ("transition" .equals (baliseName )) {
186- String name = ( String ) stack . pop ();
173+ } else if ("transition" .equals (baliseName )) {
174+ String name = textBuffer . toString (). trim ();
187175 NaryOp enab = (NaryOp ) stack .peek ();
188- enab .addChild (Expression .trans (findTransition (name )));
189- dotext = false ;
176+ enab .addChild (Expression .trans (findTransition (name )));
177+ dotext = false ;
178+ textBuffer .setLength (0 );
190179 } else if ("before" .equals (baliseName )) { //$NON-NLS-1$
191180 // NOTHING
192181 } else if ("reach" .equals (baliseName )) { //$NON-NLS-1$
193182 // NOTHING
194- } else if (! isLTL ) {
195- // temporal operator handling for CTL properties
196- if ( ("globally" .equals (baliseName ) || "finally" .equals (baliseName ) || "next" .equals (baliseName ) || "until" .equals (baliseName ) ) ) {
183+ } else if (!isLTL ) {
184+ // temporal operator handling for CTL properties
185+ if (("globally" .equals (baliseName ) || "finally" .equals (baliseName ) || "next" .equals (baliseName )
186+ || "until" .equals (baliseName ))) {
197187 stack .push (baliseName );
198188 } else if ("all-paths" .equals (baliseName )) { //$NON-NLS-1$
199189 String childbalise = (String ) stack .pop ();
@@ -220,8 +210,8 @@ public void endElement(String uri, String localName, String baliseName)
220210 }
221211 } else {
222212 // temporal operator handling for LTL properties
223- if ("all-paths" .equals (baliseName )) {
224- // only as first node
213+ if ("all-paths" .equals (baliseName )) {
214+ // only as first node
225215 // hence leave stack alone and skip this node
226216 } else if ("globally" .equals (baliseName )) {
227217 stack .push (Expression .op (Op .G , (Expression ) stack .pop (), null ));
@@ -238,23 +228,24 @@ public void endElement(String uri, String localName, String baliseName)
238228 private Map <String , Integer > pcache = null ;
239229
240230 private int findPlace (String name ) {
241- if (spec instanceof ISparsePetriNet ) {
242- ISparsePetriNet spn = (ISparsePetriNet ) spec ;
243- if (pcache == null ) {
244- // Initialize the cache with an appropriate initial capacity to optimize performance and minimize rehashing
245- pcache = new HashMap <>((spn .getPnames ().size () * 4 + 2 ) / 3 );
246- int i = 0 ;
247- for (String pl : spn .getPnames ()) {
248- pcache .put (pl , i ++);
249- }
250- }
251- }
252- Integer index = (pcache == null ) ? spec .getPlaceIndex (normalizeName (name )) : pcache .get (normalizeName (name ));
253- if (index == null || index < 0 ) {
254- System .out .println ("Unknown place :\" " + name + "\" in property !" );
255- throw new IllegalArgumentException ("Unknown place :\" " + name + "\" in property !" );
256- }
257- return index ;
231+ if (spec instanceof ISparsePetriNet ) {
232+ ISparsePetriNet spn = (ISparsePetriNet ) spec ;
233+ if (pcache == null ) {
234+ // Initialize the cache with an appropriate initial capacity to optimize
235+ // performance and minimize rehashing
236+ pcache = new HashMap <>((spn .getPnames ().size () * 4 + 2 ) / 3 );
237+ int i = 0 ;
238+ for (String pl : spn .getPnames ()) {
239+ pcache .put (pl , i ++);
240+ }
241+ }
242+ }
243+ Integer index = (pcache == null ) ? spec .getPlaceIndex (normalizeName (name )) : pcache .get (normalizeName (name ));
244+ if (index == null || index < 0 ) {
245+ System .out .println ("Unknown place :\" " + name + "\" in property !" );
246+ throw new IllegalArgumentException ("Unknown place :\" " + name + "\" in property !" );
247+ }
248+ return index ;
258249 }
259250
260251 public void popNary (Op op ) {
@@ -268,34 +259,35 @@ public void popNary(Op op) {
268259 stack .push (Expression .nop (op , operands ));
269260 }
270261
271-
272262 public void popBinary (Op op ) {
273263 Expression r = (Expression ) stack .pop ();
274264 Expression l = (Expression ) stack .pop ();
275265 stack .push (Expression .op (op , l , r ));
276266 }
277267
278- private Map <String ,Integer > tcache = null ;
268+ private Map <String , Integer > tcache = null ;
269+
279270 private int findTransition (String name ) {
280271 if (spec instanceof ISparsePetriNet ) {
281272 ISparsePetriNet spn = (ISparsePetriNet ) spec ;
282273 if (tcache == null ) {
283- // no reindex https://stackoverflow.com/questions/434989/hashmap-initialization-parameters-load-initialcapacity
284- tcache = new HashMap <> ( (spn .getTnames ().size () *4 +2 )/3 );
285- int i =0 ;
274+ // no reindex
275+ // https://stackoverflow.com/questions/434989/hashmap-initialization-parameters-load-initialcapacity
276+ tcache = new HashMap <>((spn .getTnames ().size () * 4 + 2 ) / 3 );
277+ int i = 0 ;
286278 for (String tr : spn .getTnames ()) {
287279 tcache .put (tr , i ++);
288280 }
289- }
281+ }
290282 }
291283 int index ;
292284 if (tcache == null )
293285 index = spec .getTransitionIndex (normalizeName (name ));
294286 else
295287 index = tcache .get (normalizeName (name ));
296288 if (index < 0 ) {
297- System .out .println ("Unknown transition named :\" " + name + "\" " + " in property." );
298- throw new IllegalArgumentException ("Unknown transition named :\" " + name + "\" " + " in property." );
289+ System .out .println ("Unknown transition named :\" " + name + "\" " + " in property." );
290+ throw new IllegalArgumentException ("Unknown transition named :\" " + name + "\" " + " in property." );
299291 }
300292 return index ;
301293 }
@@ -306,12 +298,11 @@ public static String normalizeName(String text) {
306298 res = res .replace ('/' , '_' );
307299 res = res .replace ('*' , 'x' );
308300 res = res .replace ('=' , '_' );
309-
301+
310302 return res ;
311303 }
312-
304+
313305 private Logger getLog () {
314306 return Logger .getLogger ("fr.lip6.move.gal" );
315307 }
316-
317- }
308+ }
0 commit comments