Skip to main content
summaryrefslogtreecommitdiffstats
blob: 11087949d7cce8b682a025bf05ae256ac8392031 (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
/*
 * Created on Apr 15, 2008
 *
 * PLACE_YOUR_DISTRIBUTION_STATEMENT_RIGHT_HERE
 */
package org.eclipse.osee.framework.resource.locator.attribute;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.URI;
import org.eclipse.osee.framework.resource.management.IResourceLocator;
import org.eclipse.osee.framework.resource.management.IResourceLocatorProvider;
import org.eclipse.osee.framework.resource.management.ResourceLocator;
import org.eclipse.osee.framework.resource.management.exception.MalformedLocatorException;

/**
 * @author Roberto E. Escobar
 */
public class AttributeLocatorProvider implements IResourceLocatorProvider {

   /* (non-Javadoc)
    * @see org.eclipse.osee.framework.resource.management.IResourceLocatorProvider#generateResourceLocator(java.lang.String)
    */
   @Override
   public IResourceLocator generateResourceLocator(String seed, String name) throws MalformedLocatorException {
      URI uri = null;
      try {
         uri = new URI(generatePath(seed, name));
      } catch (Exception ex) {
         throw new MalformedLocatorException(ex);
      }
      return new ResourceLocator(uri);
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.framework.resource.management.IResourceLocatorProvider#getResourceLocator(java.lang.String)
    */
   @Override
   public IResourceLocator getResourceLocator(String path) throws MalformedLocatorException {
      URI uri = null;
      if (isPathValid(path) != false) {
         try {
            uri = new URI(path);
         } catch (Exception ex) {
            throw new MalformedLocatorException(ex);
         }
      } else {
         throw new MalformedLocatorException(String.format("Invalid path hint: [%s]", path));
      }
      return new ResourceLocator(uri);
   }

   /* (non-Javadoc)
    * @see org.eclipse.osee.framework.resource.management.IResourceLocatorProvider#isValid(java.lang.String)
    */
   @Override
   public boolean isValid(String protocol) {
      return isArgValid(protocol) != false && protocol.startsWith("attr") != false;
   }

   private boolean isArgValid(String value) {
      return value != null && value.length() > 0;
   }

   private boolean isPathValid(String value) {
      return isArgValid(value) && value.startsWith("attr://");
   }

   private String generatePath(String seed, String name) throws MalformedLocatorException {
      StringBuilder builder = new StringBuilder("attr://");
      if (isArgValid(seed) != false && isArgValid(name) != false) {
         try {
            char[] buffer = new char[3];
            int cnt = -1;
            Reader in = new StringReader(seed);
            while ((cnt = in.read(buffer)) != -1) {
               builder.append(buffer, 0, cnt);
               builder.append("/");
            }
         } catch (IOException ex) {
            throw new MalformedLocatorException(ex);
         }
         builder.append(name);
      } else {
         throw new MalformedLocatorException("Invalid arguments during locator generation.");
      }
      return builder.toString();
   }
}

Back to the top