summaryrefslogtreecommitdiffstats
blob: 074fee6f3ecc010a954a89d5c5557dd34f937271 (plain)
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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.manager.servlet;

import java.io.StringReader;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.io.xml.AbstractSaxHandler;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * @author Roberto E. Escobar
 */
class ClientInstallInfo {
   private final static String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
   private final static String EMPTY_STRING = "";

   private final String name;
   private String os;
   private boolean isActive;
   private String execPath;
   private String comment;

   private ClientInstallInfo(String name) {
      this.name = name;
      this.os = null;
      this.execPath = EMPTY_STRING;
      this.isActive = false;
      this.comment = EMPTY_STRING;
   }

   public String getName() {
      return name;
   }

   public String getOs() {
      return os;
   }

   public String getExecPath() {
      return execPath;
   }

   public boolean isActive() {
      return this.isActive;
   }

   public String getComment() {
      return comment;
   }

   @Override
   public String toString() {
      return String.format("name:[%s] os:[%s] isActive:[%s] comment:[%s] location:[%s]", name, os, isActive, comment,
         execPath);
   }

   public static ClientInstallInfo createFromXml(String name, String data) throws OseeCoreException {
      ClientInstallInfo info = new ClientInstallInfo(name);
      try {
         if (!data.startsWith(XML_HEADER)) {
            data = XML_HEADER + data;
         }
         XMLReader reader = XMLReaderFactory.createXMLReader();
         reader.setContentHandler(new Parser(info));
         reader.parse(new InputSource(new StringReader(data)));
      } catch (Exception ex) {
         throw new OseeCoreException(String.format("Error parsing data for client install: [%s]", name), ex);
      }
      return info;
   }

   private final static class Parser extends AbstractSaxHandler {
      private final ClientInstallInfo info;

      private Parser(ClientInstallInfo info) {
         this.info = info;
      }

      @Override
      public void startElementFound(String uri, String localName, String name, Attributes attributes) throws SAXException {
         if (localName.equalsIgnoreCase("install")) {
            info.os = attributes.getValue("os");
            if (info.os != null) {
               info.os = info.os.toLowerCase();
            } else {
               info.os = EMPTY_STRING;
            }
            info.isActive = Boolean.valueOf(attributes.getValue("isActive"));
         }
      }

      @Override
      public void endElementFound(String uri, String localName, String name) throws SAXException {
         try {
            if (localName.equalsIgnoreCase("location")) {
               info.execPath = getContents();
               if (info.execPath == null) {
                  info.execPath = EMPTY_STRING;
               }
            } else if (localName.equalsIgnoreCase("comment")) {
               info.comment = getContents();
               if (info.comment == null) {
                  info.comment = EMPTY_STRING;
               }
            }
         } catch (Exception ex) {
            throw new IllegalStateException(ex);
         }
      }
   }
}