View Javadoc

1   /**
2    * Licensed to jclouds, Inc. (jclouds) under one or more
3    * contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  jclouds licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.jclouds.cim.xml;
20  
21  import static com.google.common.base.Predicates.equalTo;
22  import static com.google.common.base.Predicates.not;
23  import static com.google.common.collect.Iterables.filter;
24  import static com.google.common.collect.Iterables.transform;
25  import static org.jclouds.util.SaxUtils.currentOrNull;
26  import static org.jclouds.util.SaxUtils.equalsOrSuffix;
27  
28  import java.net.URI;
29  
30  import org.jclouds.cim.VirtualSystemSettingData;
31  import org.jclouds.cim.VirtualSystemSettingData.AutomaticRecoveryAction;
32  import org.jclouds.cim.VirtualSystemSettingData.AutomaticShutdownAction;
33  import org.jclouds.cim.VirtualSystemSettingData.AutomaticStartupAction;
34  import org.jclouds.http.functions.ParseSax;
35  import org.xml.sax.Attributes;
36  
37  import com.google.common.base.Function;
38  import com.google.common.base.Splitter;
39  
40  /**
41   * @author Adrian Cole
42   */
43  public class VirtualSystemSettingDataHandler extends ParseSax.HandlerWithResult<VirtualSystemSettingData> {
44     protected StringBuilder currentText = new StringBuilder();
45  
46     protected VirtualSystemSettingData.Builder builder = VirtualSystemSettingData.builder();
47  
48     public VirtualSystemSettingData getResult() {
49        try {
50           return builder.build();
51        } finally {
52           builder = VirtualSystemSettingData.builder();
53        }
54     }
55  
56     @Override
57     public void startElement(String uri, String localName, String qName, Attributes attrs) {
58     }
59  
60     @Override
61     public void endElement(String uri, String localName, String qName) {
62        String current = currentOrNull(currentText);
63        if (current != null) {
64           if (equalsOrSuffix(qName, "ElementName")) {
65              builder.elementName(current);
66           } else if (equalsOrSuffix(qName, "InstanceID")) {
67              builder.instanceID(current);
68           } else if (equalsOrSuffix(qName, "Caption")) {
69              builder.caption(current);
70           } else if (equalsOrSuffix(qName, "Description")) {
71              builder.description(current);
72           } else if (equalsOrSuffix(qName, "AutomaticRecoveryAction")) {
73              builder.automaticRecoveryAction(AutomaticRecoveryAction.fromValue(current));
74           } else if (equalsOrSuffix(qName, "AutomaticShutdownAction")) {
75              builder.automaticShutdownAction(AutomaticShutdownAction.fromValue(current));
76           } else if (equalsOrSuffix(qName, "AutomaticStartupAction")) {
77              builder.automaticStartupAction(AutomaticStartupAction.fromValue(current));
78           } else if (equalsOrSuffix(qName, "AutomaticStartupActionDelay")) {
79              // TODO parse the format for intervals: ddddddddhhmmss.mmmmmm:000
80              builder.automaticStartupActionDelay(null);
81           } else if (equalsOrSuffix(qName, "AutomaticStartupActionSequenceNumber")) {
82              builder.automaticStartupActionSequenceNumber(new Integer(current));
83           } else if (equalsOrSuffix(qName, "ConfigurationDataRoot")) {
84              builder.configurationDataRoot(URI.create(current));
85           } else if (equalsOrSuffix(qName, "ConfigurationFile")) {
86              builder.configurationFile(URI.create(current));
87           } else if (equalsOrSuffix(qName, "ConfigurationID")) {
88              builder.configurationID(current);
89           } else if (equalsOrSuffix(qName, "CreationTime")) {
90              // TODO parse the format for timestamps: yyyymmddhhmmss.mmmmmmsutc
91              builder.creationTime(null);
92           } else if (equalsOrSuffix(qName, "LogDataRoot")) {
93              builder.logDataRoot(URI.create(current));
94           } else if (equalsOrSuffix(qName, "RecoveryFile")) {
95              builder.recoveryFile(URI.create(current));
96           } else if (equalsOrSuffix(qName, "RecoveryFile")) {
97              builder.recoveryFile(URI.create(current));
98           } else if (equalsOrSuffix(qName, "SuspendDataRoot")) {
99              builder.suspendDataRoot(URI.create(current));
100          } else if (equalsOrSuffix(qName, "SwapFileDataRoot")) {
101             builder.swapFileDataRoot(URI.create(current));
102          } else if (equalsOrSuffix(qName, "VirtualSystemIdentifier")) {
103             builder.virtualSystemIdentifier(current);
104          } else if (equalsOrSuffix(qName, "VirtualSystemType")) {
105             builder.virtualSystemTypes(filter(transform(Splitter.on(',').split(current),
106                      new Function<String, String>() {
107 
108                         @Override
109                         public String apply(String input) {
110                            return input.trim();
111                         }
112 
113                      }), not(equalTo(""))));
114          } else if (equalsOrSuffix(qName, "Notes")) {
115             builder.notes(current);
116          }
117       }
118       currentText = new StringBuilder();
119    }
120 
121    public void characters(char ch[], int start, int length) {
122       currentText.append(ch, start, length);
123    }
124 }