ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package org.jbpm.pvm.internal.wire.xml;
 
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
 
import org.jbpm.api.JbpmException;
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.util.XmlUtil;
import org.jbpm.pvm.internal.wire.Descriptor;
import org.jbpm.pvm.internal.wire.WireContext;
import org.jbpm.pvm.internal.wire.WireDefinition;
import org.jbpm.pvm.internal.wire.binding.ByteBinding;
import org.jbpm.pvm.internal.wire.binding.CharBinding;
import org.jbpm.pvm.internal.wire.binding.ClassBinding;
import org.jbpm.pvm.internal.wire.binding.DoubleBinding;
import org.jbpm.pvm.internal.wire.binding.FloatBinding;
import org.jbpm.pvm.internal.wire.binding.IntBinding;
import org.jbpm.pvm.internal.wire.binding.ListBinding;
import org.jbpm.pvm.internal.wire.binding.MapBinding;
import org.jbpm.pvm.internal.wire.binding.NullBinding;
import org.jbpm.pvm.internal.wire.binding.ObjectBinding;
import org.jbpm.pvm.internal.wire.binding.RefBinding;
import org.jbpm.pvm.internal.wire.binding.SetBinding;
import org.jbpm.pvm.internal.wire.binding.StringBinding;
import org.jbpm.pvm.internal.wire.descriptor.AbstractDescriptor;
import org.jbpm.pvm.internal.wire.descriptor.ObjectDescriptor;
import org.jbpm.pvm.internal.xml.Bindings;
import org.jbpm.pvm.internal.xml.Parse;
import org.jbpm.pvm.internal.xml.Parser;
import org.w3c.dom.Element;
 
/**
 * parses object wiring xml and constructs a WireDefinition.
 *
 * <p>To learn the full XML syntax, check out the xsd docs
 * </p>
 * <ul><li>To describe an object:<br/>
 * <ul>
 *   <li><b><code>&lt;object .../&gt;</code></b>: see {@link ObjectBinding}</li>
 * </ul></li>
 * <li>To describe basic types:<br/>
 * <ul>
 *   <li><b><code>&lt;boolean .../&gt;</code>, <code>&lt;true /&gt;</code> and <code>&lt;false/&gt;</code></b>: see {@link BooleanBinding}</li>
 *   <li><b><code>&lt;byte .../&gt;</code></b>: see {@link ByteBinding}</li>
 *   <li><b><code>&lt;char .../&gt;</code></b>: see {@link CharBinding}</li>
 *   <li><b><code>&lt;double .../&gt;</code></b>: see {@link DoubleBinding}</li>
 *   <li><b><code>&lt;float .../&gt;</code></b>: see {@link FloatBinding}</li>
 *   <li><b><code>&lt;int .../&gt;</code></b>: see {@link IntBinding}</li>
 *   <li><b><code>&lt;string .../&gt;</code></b>: see {@link StringBinding}</li>
 * </ul></li>
 * <li>To describe collections:<br/>
 * <ul>
 *   <li><b><code>&lt;map .../&gt;</code></b>: see {@link MapBinding}</li>
 *   <li><b><code>&lt;set .../&gt;</code></b>: see {@link SetBinding}</li>
 *   <li><b><code>&lt;list .../&gt;</code></b>: see {@link ListBinding}</li>
 * </ul></li>
 * <li>Others:<br/>
 * <ul>
 *   <li><b><code>&lt;class .../&gt;</code></b>: see {@link ClassBinding}</li>
 *   <li><b><code>&lt;ref .../&gt;</code></b>: see {@link RefBinding}</li>
 *   <li><b><code>&lt;null .../&gt;</code></b>: see {@link NullBinding}</li>
 * </ul></li>
 * </ul>
 *
 * <h3>Bindings</h3>
 *
 * <p>The defaults bindings for the Wiring XML are divided in two categories:
 * </p>
 *
 * <ul>
 *     <li>Descriptors, registered with the {@link #CATEGORY_DESCRIPTOR} category</li>
 *     <li>Operations, registered with the {@link #CATEGORY_OPERATION} category</li>
 * </ul>
 *
 * <p>Once a parser is created, bindings can be added, overwritten and removed
 * to customize that parser instance.
 * </p>
 *
 * <h3 id='args'>Describing arguments</h3>
 *
 * <p>An ArgDescriptor is defined by a <b><code>&lt;arg&gt;</code></b> xml element.</p>
 *
 * <p>This element can have an attribute "type", which specifies name of the argument's type.</p>
 * <p>This element contains <b>one</b> child element that defines a {@link Descriptor}. This descriptor specifies the value to give to the argument.</p>
 *
 * <h4>Example</h4>
 *
 * Consider the following class:
 * <pre> public class Hello {
 *   public static String sayHello(String name) {
 *     return "Hello " + name + " !";
 *   }
 * }</pre>
 *
 * The following Xml declaration will create an object 's' of class 'String' (see {@link ObjectDescriptor}).
 * This object is created by invoking <code>Hello.sayHello</code> with the value <code>world</code> as a parameter.
 *
 * <pre> &lt;objects&gt;
 *   &lt;object name="s" class='Hello' method='sayHello'&gt;
 *     &lt;arg&gt;
 *      &lt;string value='world' /&gt;
 *     &lt;/arg&gt;
 *   &lt;/object&gt;
 * &lt;/objects&gt;</pre>
 *
 * The created object 's' will be a String, containing "Hello world !".
 * 
 * <h3 id='init'>Initialization</h3>
 * 
 * <p>The initialization method can be defined with the <code>init</code> attribute. 
 * For more details on how initialization works, see section 'Initialization' of {@link WireContext}.</p>
 * 
 * The <code>init</code> attribute can have these values:
 * <ul>
 *   <li><code>lazy</code>: for lazy creation and delayed initialization</li>
 *   <li><code>required</code>: for lazy creation and immediate initialization</li>
 *   <li><code>eager</code>: for eager creation and delayed initialization</li>
 *   <li><code>immediate</code>: for eager creation and immediate initialization</li>
 * </ul>
 *
 * @author Tom Baeyens
 * @author Guillaume Porcher (documentation)
 */
public class WireParser extends Parser {
 
  public static final String[] DEFAULT_WIRE_BINDING_RESOURCES = new String[]{
    "jbpm.wire.bindings.xml",
    "jbpm.user.wire.bindings.xml"
  }; 
 
  private static final long serialVersionUID = 1L;
  
  private static final Log log = Log.getLog(WireParser.class.getName());
 
  public static final String CATEGORY_DESCRIPTOR = "descriptor";
  public static final String CATEGORY_OPERATION = "operation";
  public static final String CATEGORY_INTERCEPTOR = "interceptor";
 
  /** static instance of WireParser */
  private static WireParser instance;
  /** default bindings for handling wiring xml elements. */
  private static Bindings defaultBindings; // initialized at the bottom of this file
  
  /**
   * Constructs a new WireParser with the default bindings.
   */
  public WireParser() {
    super(defaultBindings);
  }
 
  /**
   * Default method to get an instance of the WireParser
   * @return the static instance of WireParser
   */
  public static synchronized WireParser getInstance() {
    if (instance==null) {
      instance = new WireParser();
    }
    return instance;
  }
 
  /**
   * Convenience method to parse a wiring xml.
   * @param xmlString the xml string to parse
   * @return the WireDefinition created by parsing the xml given in input.
   * @see #parseXmlString(String)
   */
  public static WireDefinition parseXmlString(String xmlString) {
    return (WireDefinition) getInstance()
            .createParse()
            .setString(xmlString)
            .execute()
            .checkErrors("wire definition xml string")
            .getDocumentObject();
  }
 
  // document element parsing /////////////////////////////////////////////////
 
  /**
   * This method builds the WireDefinition from the DOM tree.
   * This methods parses all child activities of the documentElement that correspond to a Descriptor definition.
   * @param documentElement the root element of the document
   * @param parse Parse object that contains all information for the current parse operation.
   * @return an instance of WireDefinition containing the resulting WireDefinition.
   * @see Parser#parseDocumentElement(Element, Parse)
   */
  public Object parseDocumentElement(Element documentElement, Parse parse) {
    List<Element> elements = XmlUtil.elements(documentElement);
 
    
    WireDefinition wireDefinition = parse.contextStackFind(WireDefinition.class);
    if (wireDefinition==null) {
      wireDefinition = new WireDefinition();
    }
    
    parse.contextStackPush(wireDefinition);
    try {
      for (Element descriptorElement: elements) {
        Descriptor descriptor = (Descriptor) parseElement(descriptorElement, parse, CATEGORY_DESCRIPTOR);
        
        // add the descriptor
        if ( (wireDefinition!=null)
             && (descriptor!=null)
           ) {
          wireDefinition.addDescriptor(descriptor);
        }
      }
    } finally {
      parse.contextStackPop();
    }
 
    return wireDefinition;
  }
 
  /**
   *  This method parses an arbitrary element in the document based on the bindings in the given category.
   *  This method calls the {@link Parser#parseElement(Element, Parse, String)} method to build the resulting object.
   *  If the resulting object is a subclass of {@link AbstractDescriptor}, the related fields are initialized.
   *
   *  @param element the element to parse
   *  @param parse Parse object that contains all information for the current parse operation.
   *  @param category is the category in which the tagName should be resolved to an ElementHandler.
   *  If category is null, all the categories will be scanned for an appropriate binding in random order.
   *  @return the java object created from the DOM element
   */
  public Object parseElement(Element element, Parse parse, String category) {
    if (element==null) return null;
    Object object = super.parseElement(element, parse, category);
    if ( (object!=null)
         && (object instanceof Descriptor)
       ) {
      
      Descriptor descriptor = (Descriptor) object;
      
      if (descriptor instanceof AbstractDescriptor) {
        AbstractDescriptor abstractDescriptor = (AbstractDescriptor) descriptor;
        if(element.hasAttribute("name")){
          String name = element.getAttribute("name");
          // get the name
          abstractDescriptor.setName(name);
        }
 
        if (element.hasAttribute("init")) {
          // get the init
          String initText = element.getAttribute("init");
 
          if("eager".equalsIgnoreCase(initText)){
            abstractDescriptor.setInit(AbstractDescriptor.INIT_EAGER);
          }else if("immediate".equalsIgnoreCase(initText)){
            abstractDescriptor.setInit(AbstractDescriptor.INIT_IMMEDIATE);
          } else if("required".equalsIgnoreCase(initText)){
            abstractDescriptor.setInit(AbstractDescriptor.INIT_REQUIRED);
          } else {
            // init='lazy' or default value
            abstractDescriptor.setInit(AbstractDescriptor.INIT_LAZY);
          }
        }
      }
    }
    return object;
  }
 
  // other methods ////////////////////////////////////////////////////////////
 
  static {
    // default descriptor parsers ///////////////////////////////////////////////
    defaultBindings = new Bindings();
    
    BindingParser bindingParser = new BindingParser();
 
    for (String wireResource: DEFAULT_WIRE_BINDING_RESOURCES) {
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      Enumeration<URL> resourceUrls;
      try {
        resourceUrls = classLoader.getResources(wireResource);
      } catch (Exception e) {
        throw new JbpmException("couldn't get resource urls for "+wireResource, e);
      }
      if (resourceUrls.hasMoreElements()) {
        while (resourceUrls.hasMoreElements()) {
          URL resourceUrl = resourceUrls.nextElement();
          log.trace("loading wire bindings from resource: "+resourceUrl);
          bindingParser.createParse()
            .setUrl(resourceUrl)
            .contextStackPush(defaultBindings)
            .execute()
            .checkErrors(resourceUrl.toString());
        }
      } else {
        log.trace("skipping unavailable wire bindings resource "+wireResource);
      }
    }
  }
}