summaryrefslogtreecommitdiffstats
blob: f17ec1e3c9d745a39e2913f1cad354a9aa8de0ae (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
124
125
/*******************************************************************************
 * 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.ui.data.model.editor.model.xml;

import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.ui.data.model.editor.model.DataType;

/**
 * @author Roberto E. Escobar
 */
public abstract class BaseXmlDataType<T extends DataType> {

   public BaseXmlDataType() {
   }

   protected void writeCDataElement(XMLStreamWriter writer, String tag, String value) throws XMLStreamException {
      if (value != null) {
         value = value.trim();
         if (Strings.isValid(value)) {
            writer.writeStartElement(tag);
            writer.writeCData(value);
            writer.writeEndElement();
         }
      }
   }

   protected void writeTextElement(XMLStreamWriter writer, String tag, String value) throws XMLStreamException {
      if (value != null) {
         value = value.trim();
         if (Strings.isValid(value)) {
            writer.writeStartElement(tag);
            writer.writeCharacters(value);
            writer.writeEndElement();
         }
      }
   }

   protected void writeIdAttributes(XMLStreamWriter writer, DataType dataType) throws XMLStreamException {
      writer.writeAttribute("name", dataType.getName());
   }

   public final void write(XMLStreamWriter writer, T dataType) throws XMLStreamException {
      writer.writeStartElement(getElementName());
      writeIdAttributes(writer, dataType);
      writeBody(writer, dataType);
      writer.writeEndElement();
   }

   protected abstract T newDataTypeInstance();

   protected void populateFromAttributes(T dataType, String tag, Map<String, String> attributes) throws XMLStreamException {
      if (tag.equals(getElementName())) {
         dataType.setName(attributes.get("name"));
      }
   }

   protected void populateFromTextElement(T dataType, String tag, String text) throws XMLStreamException {

   }

   protected void populateFromCDataElement(T dataType, String tag, String text) throws XMLStreamException {

   }

   private Map<String, String> getAttributes(XMLStreamReader reader) throws XMLStreamException {
      int count = reader.getAttributeCount();
      Map<String, String> attributes = new HashMap<String, String>();
      for (int index = 0; index < count; index++) {
         attributes.put(reader.getAttributeLocalName(index), reader.getAttributeValue(index));
      }
      return attributes;
   }

   public final void fromXml(XMLStreamReader reader, IDataTypeCollector<T> collector) throws XMLStreamException {
      T object = null;
      String currentElement = null;
      boolean isDone = false;
      while (reader.hasNext() && !isDone) {
         int eventType = reader.getEventType();
         switch (eventType) {
            case XMLStreamConstants.START_ELEMENT:
               currentElement = reader.getLocalName();
               if (currentElement.equals(getElementName())) {
                  object = newDataTypeInstance();
               }
               populateFromAttributes(object, currentElement, getAttributes(reader));
               break;
            case XMLStreamConstants.END_ELEMENT:
               if (currentElement.equals(getElementName())) {
                  isDone = true;
                  collector.collect(object);
               }
               currentElement = null;
               break;
            case XMLStreamConstants.CDATA:
               populateFromCDataElement(object, currentElement, reader.getText());
               break;
            case XMLStreamConstants.CHARACTERS:
               populateFromTextElement(object, currentElement, reader.getText());
               break;
            default:
               break;
         }
         reader.next();
      }
   }

   public abstract String getElementName();

   protected abstract void writeBody(XMLStreamWriter writer, T dataType) throws XMLStreamException;
}