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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.pvm.internal.util;
 
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
 
import org.jbpm.api.JbpmException;
import org.jbpm.pvm.internal.xml.Parse;
 
import javax.xml.namespace.QName;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Attr;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
/**
 * convenience methods to make reading org.w3c.dom models easier.
 *
 * @author Tom Baeyens
 */
public class XmlUtil {
 
  private XmlUtil() {
    // hide default constructor to prevent instantiation
  }
 
  public static List<Element> elements(Element element, String tagName) {
    if (element==null) {
      return Collections.emptyList();
    }
    NodeList activityList = element.getChildNodes();
    if ( (activityList == null)
         || (activityList.getLength()==0)
       ) {
      return Collections.emptyList();
    }
    List<Element> elements = new ArrayList<Element>();
    for (int i = 0; i < activityList.getLength(); i++) {
      Node child = activityList.item(i);
      if (Element.class.isAssignableFrom(child.getClass())) {
        Element childElement = (Element) child;
        String childTagName = getTagLocalName(childElement);
        if (childTagName.equals(tagName)) {
          if (elements == null) {
            elements = new ArrayList<Element>();
          }
          elements.add(childElement);
        }
      }
    }
    return elements;
  }
 
  public static List<Element> elements(Element element, Set<String> allowedTagNames) {
    if (element==null) {
      return Collections.emptyList();
    }
    NodeList activityList = element.getChildNodes();
    if ( (activityList == null)
         || (activityList.getLength()==0)
       ) {
      return Collections.emptyList();
    }
    List<Element> elements = new ArrayList<Element>();
    for (int i = 0; i < activityList.getLength(); i++) {
      Node child = activityList.item(i);
      if (Element.class.isAssignableFrom(child.getClass())) {
        Element childElement = (Element) child;
        String childTagName = getTagLocalName(childElement);
        if (allowedTagNames.contains(childTagName)) {
          if (elements == null) {
            elements = new ArrayList<Element>();
          }
          elements.add(childElement);
        }
      }
    }
    return elements;
  }
 
  public static Element element(Element element, String tagName) {
    return element(element, tagName, false, null);
  }
 
  public static Element element(Element element, String tagName, boolean required, Parse parse) {
    if (element==null) {
      return null;
    }
    NodeList activityList = element.getChildNodes();
    for (int i = 0; (i < activityList.getLength()); i++) {
      Node child = activityList.item(i);
      if ((Element.class.isAssignableFrom(child.getClass())) && (getTagLocalName((Element) child)).equals(tagName)) {
        return (Element) child;
      }
    }
    
    if (required && (parse!=null)) {
      parse.addProblem("nested element <"+XmlUtil.getTagLocalName(element)+"><"+tagName+" ... />... is required", element);
    }
    return null;
  }
 
 
  public static List<Element> elements(Element element) {
    if (element==null) {
      return Collections.emptyList();
    }
    NodeList activityList = element.getChildNodes();
    if ( (activityList == null)
         || (activityList.getLength()==0)
       ) {
      return Collections.emptyList();
    }
    List<Element> elements = new ArrayList<Element>();
    if ((activityList != null) && (activityList.getLength() > 0)) {
      elements = new ArrayList<Element>();
      for (int i = 0; i < activityList.getLength(); i++) {
        Node activity = activityList.item(i);
        if (activity instanceof Element) {
          elements.add((Element) activity);
        }
      }
    }
    return elements;
  }
 
  public static List<Element> elements(Element element, String ns, String localName) {
    if (element==null) {
      return Collections.emptyList();
    }
    NodeList activityList = element.getChildNodes();
    if ( (activityList == null)
         || (activityList.getLength()==0)
       ) {
      return Collections.emptyList();
    }
    List<Element> matchingElements = new ArrayList<Element>();
    NodeList nl = element.getChildNodes();
    for (int i=0;i<nl.getLength();i++) {
      Node n = nl.item(i);
      if (n instanceof Element && n.getLocalName() != null && n.getLocalName().equals(localName) && n.getNamespaceURI() != null && n.getNamespaceURI().equals(ns)) {
        matchingElements.add((Element)n);
      }
    }
    return matchingElements;
  }
 
  public static List<Element> elementsQName(Element element, Set<QName> allowedTagNames) {
    if (element==null) {
      return Collections.emptyList();
    }
    NodeList activityList = element.getChildNodes();
    if ( (activityList == null)
         || (activityList.getLength()==0)
       ) {
      return Collections.emptyList();
    }
    List<Element> elements = new ArrayList<Element>();
    if (activityList != null) {
      for (int i = 0; i < activityList.getLength(); i++) {
        Node child = activityList.item(i);
        if (Element.class.isAssignableFrom(child.getClass())) {
          Element childElement = (Element) child;
          QName childElementQName = new QName(childElement.getNamespaceURI(), childElement.getLocalName());
          if (allowedTagNames.contains(childElementQName)) {
            if (elements == null) {
              elements = new ArrayList<Element>();
            }
            elements.add(childElement);
          }
        }
      }
    }
    return elements;
  }
 
  public static Element element(Element element) {
    Element onlyChild = null;
    List<Element> elements = elements(element);
    if (!elements.isEmpty()) {
      onlyChild = elements.get(0);
    }
    return onlyChild;
  }
 
  public static String toString(Node node) {
    if (node == null) return "null";
 
    try {
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer = transformerFactory.newTransformer();
      transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 
      StringWriter stringWriter = new StringWriter();
      transformer.transform(new DOMSource(node), new StreamResult(stringWriter));
      return stringWriter.toString();
    } 
    catch (TransformerException e) {
      throw new JbpmException("could not transform dom node to string", e);
    }
  }
 
  public static String getContentText(Element element) {
    return element.getTextContent();
  }
 
  public static boolean isTextOnly(Element element) {
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
      if (child.getNodeType() == Node.ELEMENT_NODE)
        return false;
    }
    return true;
  }
 
  public static List<Attr> attributes(Element element) {
    NamedNodeMap attributeMap = element.getAttributes();
    if ((attributeMap == null) || (attributeMap.getLength() == 0)) {
      return Collections.emptyList();
    }
 
    List<Attr> attributes = new ArrayList<Attr>();
    for (int i = 0; i < attributeMap.getLength(); i++) {
      attributes.add((Attr) attributeMap.item(i));
    }
 
    return attributes;
  }
 
  public static List<Node> contents(Element element) {
    NodeList activityList = element.getChildNodes();
    if ((activityList == null) || (activityList.getLength() == 0)) {
      return Collections.emptyList();
    }
 
    List<Node> contents = new ArrayList<Node>();
    for (int i = 0; i < activityList.getLength(); i++) {
      contents.add((Node) activityList.item(i));
    }
 
    return contents;
  }
 
  public static String getTagLocalName(Element element) {
    if (element == null) {
      return null;
    }
    String localName = element.getLocalName();
    if (localName != null) {
      return localName;
    }
    return element.getTagName();
  }
 
  /** the attribute value or null if the attribute is not present */
  public static String attribute(Element element, String attributeName) {
    if (element.hasAttribute(attributeName)) {
      return element.getAttribute(attributeName);
    } else {
      return null;
    }
  }
 
  /** convenience method to combine extraction of a string attribute value.
   * 
   * If the attribute exists, it is returned.  If the attribute is not present, null 
   * is returned.  The attribute is not present and it is required, 
   * a problem will be added to the parse.  */
  public static String attribute(Element element, String attributeName, boolean required, Parse parse) {
    return attribute(element, attributeName, required, parse, null);
  }
 
  /** convenience method to combine extraction of a string attribute value.
   * 
   * If the attribute exists, it is returned.  If the attribute is not present, the 
   * defaultValue is returned.  The attribute is not present and it is required, 
   * a problem will be added to the parse.  */
  public static String attribute(Element element, String attributeName, boolean required, Parse parse, String defaultValue) {
    if (element.hasAttribute(attributeName)) {
      String value = element.getAttribute(attributeName);
      if (required && "".equals(value)) {
        parse.addProblem("attribute <"+XmlUtil.getTagLocalName(element)+" "+attributeName+"=\"\" is empty", element);
      }
      return value;
    } 
 
    if (required) {
      parse.addProblem("attribute <"+XmlUtil.getTagLocalName(element)+" "+attributeName+"=\"...\" is required", element);
    }
    
    return defaultValue;
  }
  
  
  /** parse an attribute as an integer. */
  public static Integer attributeInteger(Element element, String attributeName, boolean required, Parse parse) {
    String valueText = attribute(element, attributeName, required, parse);
 
    if (valueText!=null) {
      try {
        return Integer.parseInt(valueText);
      } catch (NumberFormatException e) {
        parse.addProblem(errorMessageAttribute(element, attributeName, valueText, "value not parsable as integer"), element);
      }
    }
 
    return null;
  }
 
  /** parse an attribute as an boolean. */
  public static Boolean attributeBoolean(Element element, String attributeName, boolean required, Parse parse) {
    return attributeBoolean(element, attributeName, required, parse, null);
  }
  
  /** parse an attribute as an boolean. */
  public static Boolean attributeBoolean(Element element, String attributeName, boolean required, Parse parse, Boolean defaultValue) {
    String valueText = attribute(element, attributeName, required, parse);
    if (valueText!=null) {
      Boolean value = parseBooleanValue(valueText);
      if (value==null) {
        parse.addProblem(errorMessageAttribute(element, attributeName, valueText, "value not in {true, enabled, on, false, disabled, off}"), element);
      }
      return value; 
    }
    return defaultValue;
  }
 
  public static Boolean parseBooleanValue(String valueText) {
    if (valueText!=null) {
      // if we have to check for value true
      if ( ("true".equals(valueText))
          || ("enabled".equals(valueText))
          || ("on".equals(valueText))
        ) {
       return Boolean.TRUE;
 
      } else if ( ("false".equals(valueText))
           || ("disabled".equals(valueText))
           || ("off".equals(valueText))
        ) {
        return Boolean.FALSE;
      }
    }
    
    return null;
  }
  
  public static String errorMessageAttribute(Element element, String attributeName, String attributeValue, String message) {
    return "attribute <"+XmlUtil.getTagLocalName(element)+" "+attributeName+"=\""+attributeValue+"\" "+message;
  }
 
  public static List<String> parseList(Element element, String singularTagName) {
    // a null value for text represents a wildcard
    String text = XmlUtil.attribute(element, singularTagName + "s");
    // so next we'll convert a '*' into the text null value, which indicates a
    // wildcard
    if ("*".equals(text)) {
      text = null;
    }
    if (element.hasAttribute(singularTagName)) {
      String eventText = element.getAttribute(singularTagName);
      text = (text == null ? eventText : text + "," + eventText);
    }
    List<String> eventNames = parseCommaSeparatedList(text);
    return eventNames;
  }
 
  /**
   * parses comma or space separated list. A null return value means a wildcard.
   *
   * @return List of tokens or null if the commaSeparatedListText is null, '*',
   *         or empty
   */
  public static List<String> parseCommaSeparatedList(String commaSeparatedListText) {
    List<String> entries = null;
    if (commaSeparatedListText != null) {
      if (!"*".equals(commaSeparatedListText)) {
        StringTokenizer tokenizer = new StringTokenizer(commaSeparatedListText, ", ");
        while (tokenizer.hasMoreTokens()) {
          if (entries == null) {
            entries = new ArrayList<String>();
          }
          entries.add(tokenizer.nextToken());
        }
      }
    }
    return entries;
  }
 
  public static class NamespaceValue {
 
    public String prefix;
    public String localPart;
 
    public NamespaceValue(String prefix, String localPart) {
      this.prefix = prefix;
      this.localPart = localPart;
    }
  }
 
  public static NamespaceValue attributeNamespaceValue(Element element, String attributeName) {
    NamespaceValue namespaceValue = null;
    String text = attribute(element, attributeName);
    if (text != null) {
      int colonIndex = text.indexOf(':');
      if (colonIndex == -1) {
        namespaceValue = new NamespaceValue(null, text);
      } else {
        String prefix = text.substring(0, colonIndex);
        String localPart = null;
        if (text.length() > colonIndex + 1) {
          localPart = text.substring(colonIndex + 1);
        }
        namespaceValue = new NamespaceValue(prefix, localPart);
      }
    }
    return namespaceValue;
  }
 
  public static QName attributeQName(Element element, String attributeName) {
    QName qname = null;
 
    NamespaceValue namespaceValue = attributeNamespaceValue(element, attributeName);
    String text = attribute(element, attributeName);
    if (namespaceValue!=null) {
      if (namespaceValue.prefix==null) {
        qname = new QName(text);
      } else {
        String uri = element.lookupNamespaceURI(namespaceValue.prefix);
        if (uri==null) {
          throw new JbpmException("unknown prefix in qname "+text);
        } else if (namespaceValue.localPart==null) {
          throw new JbpmException("no local part in qname "+text);
        } else {
          qname = new QName(uri, namespaceValue.localPart, namespaceValue.prefix);
        }
      }
    }
    return qname;
  }
 
  public static QName getQNameFromString(Element element, String qnameAsString) {
    if (qnameAsString == null || element == null) {
      return null;
    }
    int colonIndex = qnameAsString.indexOf(":");
    String prefix = qnameAsString.substring(0, colonIndex);
    String localName = qnameAsString.substring(colonIndex + 1);
    String ns = getNamespaceURI(element, prefix);
    return new QName(ns, localName, prefix);
  }
 
  public static String getNamespaceURI(final org.w3c.dom.Node n, final String prefix) {
    Node prefixDeclaration = n.getAttributes().getNamedItem("xmlns:" + prefix);
    if (prefixDeclaration != null) {
      // we have found the good NameSpace
      return prefixDeclaration.getNodeValue();
    }
    // we have found the good NameSpace
    // we look for the NameSpace in the parent Node
    return getNamespaceURI(n.getParentNode(), prefix);
  }
}