Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a1e1ae91831a96606ccf101c1d6ceb5889d0448d (plain) (blame)
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
/*
 * Copyright (c) 2014 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.oomph.util;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Eike Stepper
 */
public final class XMLUtil
{
  private XMLUtil()
  {
  }

  public static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException
  {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setValidating(false);
    return documentBuilderFactory.newDocumentBuilder();
  }

  public static Document loadDocument(DocumentBuilder documentBuilder, InputStream inputStream) throws SAXException, IOException
  {
    try
    {
      return documentBuilder.parse(inputStream);
    }
    finally
    {
      IOUtil.close(inputStream);
    }
  }

  public static Document loadDocument(DocumentBuilder documentBuilder, File file) throws SAXException, IOException
  {
    return documentBuilder.parse(file);
  }

  public static Element loadRootElement(DocumentBuilder documentBuilder, InputStream inputStream) throws Exception
  {
    Document document = loadDocument(documentBuilder, inputStream);
    return document.getDocumentElement();
  }

  public static Element loadRootElement(DocumentBuilder documentBuilder, File file) throws Exception
  {
    Document document = loadDocument(documentBuilder, file);
    return document.getDocumentElement();
  }

  public static int handleElements(NodeList nodeList, ElementHandler handler) throws Exception
  {
    int count = 0;
    for (int i = 0; i < nodeList.getLength(); i++)
    {
      Node node = nodeList.item(i);
      if (node instanceof Element)
      {
        Element element = (Element)node;
        handler.handleElement(element);
        ++count;
      }
    }

    return count;
  }

  public static int handleChildElements(Element rootElement, ElementHandler handler) throws Exception
  {
    NodeList childNodes = rootElement.getChildNodes();
    return handleElements(childNodes, handler);
  }

  public static int handleElementsByTagName(Element rootElement, String tagName, ElementHandler handler) throws Exception
  {
    int count = 0;

    NodeList nodeList = rootElement.getElementsByTagName(tagName);
    count += handleElements(nodeList, handler);

    int pos = tagName.indexOf(':');
    if (pos != -1)
    {
      tagName = tagName.substring(pos + 1);
      nodeList = rootElement.getElementsByTagName(tagName);
      count += handleElements(nodeList, handler);
    }

    return count;
  }

  /**
   * @author Eike Stepper
   */
  public interface ElementHandler
  {
    public void handleElement(Element element) throws Exception;
  }

  /**
   * @author Eike Stepper
   */
  public static final class ElementUpdater
  {
    private final String tagName;

    private String textValue;

    private int index;

    public ElementUpdater(final Element rootElement, String tagName) throws Exception
    {
      this.tagName = tagName;
      handleElementsByTagName(rootElement, tagName, new ElementHandler()
      {
        public void handleElement(Element element) throws Exception
        {
          if (element.getParentNode() == rootElement)
          {
            textValue = element.getTextContent();
          }

          if (textValue == null)
          {
            ++index;
          }
        }
      });
    }

    public String update(String text, String newValue)
    {
      if (!ObjectUtil.equals(textValue, newValue))
      {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < index; i++)
        {
          builder.append(".*?<" + tagName + ">.*?</" + tagName + ">");
        }

        builder.append(".*?<" + tagName + ">(.*?)</" + tagName + ">.*");

        Pattern pattern = Pattern.compile(builder.toString(), Pattern.DOTALL);
        Matcher matcher = pattern.matcher(text);
        if (matcher.matches())
        {
          text = text.substring(0, matcher.start(1)) + newValue + text.substring(matcher.end(1));
        }
      }

      return text;
    }
  }
}

Back to the top