Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1cc8ec8ed65f56f61c235894750736d37db6a1b1 (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
 * 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.orcs.core.internal.relation.order;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
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.core.data.IRelationType;
import org.eclipse.osee.framework.core.data.IRelationTypeSide;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.core.enums.RelationSorter;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.orcs.data.RelationTypes;

/**
 * @author Roberto E. Escobar
 */
public class OrderParser {

   private static final String ROOT_ELEMENT = "OrderList";
   private static final String START_TAG = "Order";
   private static final String RELATION_TYPE_TAG = "relType";
   private static final String ORDER_TYPE_TAG = "orderType";
   private static final String SIDE_TAG = "side";
   private static final String LIST_TAG = "list";

   private static final ThreadLocal<XMLInputFactory> inputFactory = new ThreadLocal<XMLInputFactory>() {

      @Override
      protected XMLInputFactory initialValue() {
         return XMLInputFactory.newInstance();
      }
   };

   private static final ThreadLocal<XMLOutputFactory> outputFactory = new ThreadLocal<XMLOutputFactory>() {

      @Override
      protected XMLOutputFactory initialValue() {
         return XMLOutputFactory.newInstance();
      }
   };

   private final RelationTypes relationCache;

   public OrderParser(RelationTypes relationCache) {
      this.relationCache = relationCache;
   }

   public void loadFromXml(HasOrderData hasOrderData, String rawData) throws OseeCoreException {
      Conditions.checkNotNull(hasOrderData, "orderData");
      if (Strings.isValid(rawData) && rawData.trim().length() > 0) {
         Reader reader = new StringReader(rawData);
         try {
            XMLStreamReader streamReader = inputFactory.get().createXMLStreamReader(reader);
            while (streamReader.hasNext()) {
               process(streamReader, hasOrderData);
               streamReader.next();
            }
         } catch (XMLStreamException ex) {
            OseeCoreException.wrapAndThrow(ex);
         } finally {
            Lib.close(reader);
         }
      }
   }

   private void process(XMLStreamReader reader, HasOrderData hasOrderData) throws OseeCoreException {
      int eventType = reader.getEventType();
      switch (eventType) {
         case XMLStreamConstants.START_ELEMENT:
            String localName = reader.getLocalName();
            String uri = reader.getNamespaceURI();
            if (START_TAG.equals(localName)) {
               final String relationTypeName = reader.getAttributeValue(uri, RELATION_TYPE_TAG);
               String orderType = reader.getAttributeValue(uri, ORDER_TYPE_TAG);
               String relationSide = reader.getAttributeValue(uri, SIDE_TAG);
               String rawList = reader.getAttributeValue(uri, LIST_TAG);
               if (relationTypeName != null && orderType != null && relationSide != null) {
                  List<String> list = Collections.emptyList();
                  if (rawList != null) {
                     list = new ArrayList<>();
                     StringTokenizer tokenizer = new StringTokenizer(rawList, ",");
                     while (tokenizer.hasMoreTokens()) {
                        list.add(tokenizer.nextToken());
                     }
                  }

                  // TODO don't store relation type by name - use type UUID
                  IRelationType type = Iterables.find(relationCache.getAll(), new Predicate<IRelationType>() {
                     @Override
                     public boolean apply(IRelationType type) {
                        return type.getName().equalsIgnoreCase(relationTypeName);
                     }
                  });

                  RelationSide side = RelationSide.fromString(relationSide);
                  IRelationTypeSide typeSide = TokenFactory.createRelationTypeSide(side, type.getId(), type.getName());
                  OrderData orderData = new OrderData(RelationSorter.valueOfGuid(orderType), list);
                  hasOrderData.add(typeSide, orderData);
               }
            }
            break;
         default:
            break;
      }
   }

   public String toXml(HasOrderData hasOrderData) throws OseeCoreException {
      Conditions.checkNotNull(hasOrderData, "orderData");
      StringWriter writer = new StringWriter();
      XMLStreamWriter xmlWriter = null;
      try {
         xmlWriter = outputFactory.get().createXMLStreamWriter(writer);
         xmlWriter.writeStartElement(ROOT_ELEMENT);
         for (Entry<IRelationTypeSide, OrderData> entry : hasOrderData) {
            writeEntry(xmlWriter, entry.getKey(), entry.getValue());
         }
         xmlWriter.writeEndElement();
         xmlWriter.writeEndDocument();
      } catch (XMLStreamException ex) {
         OseeCoreException.wrapAndThrow(ex);
      } finally {
         if (xmlWriter != null) {
            try {
               xmlWriter.close();
            } catch (XMLStreamException ex) {
               OseeCoreException.wrapAndThrow(ex);
            }
         }
      }
      return writer.toString();
   }

   private void writeEntry(XMLStreamWriter xmlWriter, IRelationTypeSide typeAndSide, OrderData orderData) throws XMLStreamException {
      xmlWriter.writeStartElement(START_TAG);
      // TODO don't store relation type by name - use type UUID
      xmlWriter.writeAttribute(RELATION_TYPE_TAG, typeAndSide.getName());
      xmlWriter.writeAttribute(SIDE_TAG, typeAndSide.getSide().name());
      xmlWriter.writeAttribute(ORDER_TYPE_TAG, orderData.getSorterId().getGuid());

      List<String> guids = orderData.getOrderIds();
      if (!guids.isEmpty()) {
         xmlWriter.writeAttribute(LIST_TAG, org.eclipse.osee.framework.jdk.core.util.Collections.toString(",", guids));
      }
      xmlWriter.writeEndElement();
   }
}

Back to the top