Skip to main content
summaryrefslogtreecommitdiffstats
blob: 929f9634d5485be8d4e1059c4d20d6ea58eb574d (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
/*******************************************************************************
 * 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.skynet.core.importing;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.jdk.core.type.CaseInsensitiveString;
import org.eclipse.osee.framework.jdk.core.type.HashCollection;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.attribute.AttributeTypeManager;

public final class RoughAttributeSet {
   private final HashCollection<CaseInsensitiveString, String> attributes =
      new HashCollection<CaseInsensitiveString, String>();
   private final Map<CaseInsensitiveString, URI> uriAttributes = new HashMap<CaseInsensitiveString, URI>(2, 1);

   public void clear() {
      attributes.clear();
      uriAttributes.clear();
   }

   public void addMultiple(String name, String[] values) {
      for (String value : values) {
         add(name, value);
      }
   }

   public void add(String name, String value) {
      attributes.put(new CaseInsensitiveString(name), value);
   }

   public Collection<String> getAttributeValueList(String attributeTypeName) {
      return attributes.getValues(new CaseInsensitiveString(attributeTypeName));
   }

   public String getSoleAttributeValue(String typeName) {
      Collection<String> valueAsCollection = getAttributeValueList(typeName);
      if (valueAsCollection == null) {
         return null;
      }
      return valueAsCollection.iterator().next();
   }

   public Collection<String> getAttributeValueList(IAttributeType attributeType) {
      return getAttributeValueList(attributeType.getName());
   }

   public void addURIAttribute(String name, URI url) {
      uriAttributes.put(new CaseInsensitiveString(name), url);
   }

   @Override
   public boolean equals(Object obj) {
      if (!(obj instanceof RoughAttributeSet)) {
         return false;
      }
      RoughAttributeSet other = (RoughAttributeSet) obj;
      return this.attributes.equals(other.attributes);
   }

   @Override
   public int hashCode() {
      return attributes.hashCode();
   }

   @Override
   public String toString() {
      return attributes.toString();
   }

   protected void translateAttributes(Artifact artifact) throws OseeCoreException {
      for (CaseInsensitiveString attrTypeName : attributes.keySet()) {
         Collection<String> values = attributes.getValues(attrTypeName);
         artifact.setAttributeValues(AttributeTypeManager.getType(attrTypeName.toString()), values);
      }
      transferBinaryAttributes(artifact);
   }

   private void transferBinaryAttributes(Artifact artifact) throws OseeCoreException {
      for (Entry<CaseInsensitiveString, URI> entry : uriAttributes.entrySet()) {
         InputStream inputStream = null;
         try {
            inputStream = new BufferedInputStream(entry.getValue().toURL().openStream());
            IAttributeType attributeType = AttributeTypeManager.getType(entry.getKey().toString());
            artifact.setSoleAttributeFromStream(attributeType, inputStream);
         } catch (Exception ex) {
            OseeExceptions.wrapAndThrow(ex);
         } finally {
            Lib.close(inputStream);
         }
      }
   }

   public Set<String> getAttributeTypeNames() {
      Set<String> typeNames = new HashSet<String>();
      for (CharSequence attrTypeName : attributes.keySet()) {
         typeNames.add(attrTypeName.toString());
      }
      for (CharSequence attrTypeName : uriAttributes.keySet()) {
         typeNames.add(attrTypeName.toString());
      }
      return typeNames;
   }
}

Back to the top