1   /*
2    * PRELYTIS.
3    * Copyright 2007, PRELYTIS S.A., and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */
22  package org.jdbc4olap.xmla;
23  
24  import java.util.ArrayList;
25  import java.util.Enumeration;
26  import java.util.List;
27  import java.util.Properties;
28  import java.util.Arrays;
29  
30  import javax.xml.soap.SOAPElement;
31  import javax.xml.soap.SOAPException;
32  import javax.xml.soap.SOAPFactory;
33  
34  /**
35   * @author <a href="mailto:fsiberchicot@jdbc4olap.org">Florian SIBERCHICOT</a>
36   * @author <a href="mailto:drollo@compositesw.com">Dan Rollo</a>
37   */
38  class XmlaProperties {
39  
40      private static final String[] FORBIDDEN_PROPERTIES = {XmlaLogin.PROPERTY_NAME_USER, XmlaLogin.PROPERTY_NAME_PASSWORD};
41      private static final List<String> FORBIDDEN_LIST = new ArrayList<String>(Arrays.asList(FORBIDDEN_PROPERTIES));
42  
43      private final Properties properties;
44  
45      XmlaProperties() {
46          properties = new Properties();
47      }
48  
49      XmlaProperties(final Properties props) {
50          this();
51          addProperties(props);
52      }
53  
54      void addProperties(final Properties props) {
55          if (props != null) {
56              for (Enumeration e = props.propertyNames(); e.hasMoreElements();) {
57                  String key = (String) e.nextElement();
58                  String value = props.getProperty(key);
59                  setProperty(key, value);
60              }
61          }
62      }
63  
64      String getProperty(final String key) {
65          return properties.getProperty(key);
66      }
67  
68      void setProperty(final String key, final String value) {
69          if (key != null && value != null && !FORBIDDEN_LIST.contains(key.toLowerCase())) {
70              properties.setProperty(key, value);
71          }
72      }
73  
74      boolean containsProperty(final String key) {
75          return properties.containsKey(key);
76      }
77  
78      SOAPElement getXMLA() throws SOAPException {
79          final SOAPFactory factory = XmlaConn.SOAP_FACTORY;
80          final SOAPElement props = factory.createElement("Properties", "", "urn:schemas-microsoft-com:xml-analysis");
81          final SOAPElement propertyList = props.addChildElement(factory.createName("PropertyList", "", "urn:schemas-microsoft-com:xml-analysis"));
82          for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) {
83              final String key = (String) e.nextElement();
84              final String value = properties.getProperty(key);
85              if (value != null && !"".equals(value)) {
86                  final SOAPElement soapProperty = propertyList.addChildElement(factory.createName(key));
87                  soapProperty.addTextNode(value);
88              }
89          }
90          return props;
91      }
92  }