1   /*
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  
22  package org.efaps.update.schema.program.staticsource;
23  
24  import java.io.IOException;
25  import java.net.URL;
26  import java.util.UUID;
27  
28  import javax.xml.parsers.DocumentBuilder;
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import javax.xml.parsers.ParserConfigurationException;
31  
32  import org.efaps.ci.CIAdminProgram;
33  import org.efaps.update.schema.program.AbstractSourceImporter;
34  import org.efaps.update.util.InstallationException;
35  import org.w3c.dom.Document;
36  import org.w3c.dom.Element;
37  import org.w3c.dom.Node;
38  import org.w3c.dom.NodeList;
39  import org.xml.sax.SAXException;
40  
41  /**
42   * TODO comment!
43   *
44   * @author The eFaps Team
45   * @version $Id$
46   */
47  public class BPMImporter
48      extends AbstractSourceImporter
49  {
50      /**
51       * Possible tagnames for the process tag.
52       */
53      private static String[] PROCESSTAGNAMES = new String[] {"process", "bpmn2:process"};
54  
55      /**
56       * Default constructor.
57       *
58       * @param _url      URL to the XSLT file
59       * @throws InstallationException if BPM importer could not be initialized
60       *                               because file from <code>_url</code> could
61       *                               not be read
62       */
63      public BPMImporter(final URL _url)
64          throws InstallationException
65      {
66          super(CIAdminProgram.BPM, _url);
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      protected String evalProgramName()
74          throws InstallationException
75      {
76          String ret = "";
77          try {
78              final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
79              final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
80              final Document doc = dBuilder.parse(getUrl().openStream(), AbstractSourceImporter.ENCODING);
81              doc.getDocumentElement().normalize();
82  
83              for (final String tagName  : BPMImporter.PROCESSTAGNAMES) {
84                  final NodeList processNodeList = doc.getElementsByTagName(tagName);
85                  if (processNodeList != null && processNodeList.getLength() > 0) {
86                      final Node processNode = processNodeList.item(0);
87                      if (processNode.getNodeType() == Node.ELEMENT_NODE) {
88                          final Element eElement = (Element) processNode;
89                          ret = eElement.getAttribute("id");
90                      }
91                      break;
92                  }
93              }
94          } catch (final ParserConfigurationException e) {
95              throw new InstallationException("could not Parse the given URL", e);
96          } catch (final SAXException e) {
97              throw new InstallationException("could not Parse the given URL", e);
98          } catch (final IOException e) {
99              throw new InstallationException("could not Read the given URL", e);
100         }
101         return ret;
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     protected UUID evalUUID()
109     {
110         return null;
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @Override
117     protected String evalRevision()
118         throws InstallationException
119     {
120         String ret = "";
121         try {
122             final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
123             final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
124             final Document doc = dBuilder.parse(getUrl().openStream(), AbstractSourceImporter.ENCODING);
125             doc.getDocumentElement().normalize();
126 
127             for (final String tagName  : BPMImporter.PROCESSTAGNAMES) {
128                 final NodeList processNodeList = doc.getElementsByTagName(tagName);
129                 if (processNodeList != null && processNodeList.getLength() > 0) {
130                     final Node processNode = processNodeList.item(0);
131                     if (processNode.getNodeType() == Node.ELEMENT_NODE) {
132                         final Element eElement = (Element) processNode;
133                         ret = eElement.getAttribute("tns:version");
134                     }
135                     break;
136                 }
137             }
138         } catch (final ParserConfigurationException e) {
139             throw new InstallationException("could not Parse the given URL", e);
140         } catch (final SAXException e) {
141             throw new InstallationException("could not Parse the given URL", e);
142         } catch (final IOException e) {
143             throw new InstallationException("could not Read the given URL", e);
144         }
145         return ret;
146     }
147 }