View Javadoc
1   /**
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.swid.builder.resource;
28  
29  import gov.nist.secauto.swid.builder.AbstractLanguageSpecificBuilder;
30  import gov.nist.secauto.swid.builder.ValidationException;
31  import gov.nist.secauto.swid.builder.resource.file.DirectoryBuilder;
32  import gov.nist.secauto.swid.builder.resource.file.FileBuilder;
33  import gov.nist.secauto.swid.builder.resource.firmware.FirmwareBuilder;
34  
35  import java.util.Collections;
36  import java.util.LinkedHashMap;
37  import java.util.LinkedList;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.stream.Collectors;
41  
42  public abstract class AbstractResourceCollectionBuilder<E extends AbstractResourceCollectionBuilder<E>>
43      extends AbstractLanguageSpecificBuilder<E> {
44    private Map<String, DirectoryBuilder> directoryMap;
45    private List<ResourceBuilder> resources;
46  
47    public AbstractResourceCollectionBuilder() {
48      super();
49    }
50  
51    @Override
52    public void reset() {
53      super.reset();
54      directoryMap = new LinkedHashMap<>();
55      resources = new LinkedList<>();
56    }
57  
58    /**
59     * Creates a new file builder based on resource pointed to by a sequence of path segments.
60     * 
61     * @param pathSegments
62     *          a sequence of path segements that represent a path to a resource
63     * @return a new file builder representing the provided path
64     */
65    public FileBuilder newFileResource(List<String> pathSegments) {
66      FileBuilder retval;
67      String filename;
68      if (pathSegments.size() > 1) {
69        List<String> directoryPath = pathSegments.subList(0, pathSegments.size() - 1);
70        DirectoryBuilder directoryBuilder = getDirectoryBuilder(directoryPath);
71        filename = pathSegments.get(pathSegments.size() - 1);
72        retval = directoryBuilder.newFileResource(filename);
73      } else {
74        filename = pathSegments.get(0);
75        retval = FileBuilder.create();
76        retval.name(filename);
77        resources.add(retval);
78      }
79      return retval;
80    }
81  
82    /**
83     * Creates a new firmware resource, adding it to this resource collection.
84     * 
85     * @return the new firmware builder
86     */
87    public FirmwareBuilder newFirmwareResource() {
88      FirmwareBuilder retval = FirmwareBuilder.create();
89      resources.add(retval);
90      return retval;
91    }
92  
93    private DirectoryBuilder getDirectoryBuilder(List<String> directoryPath) {
94      DirectoryBuilder retval = null;
95      for (String name : directoryPath) {
96        if (retval == null) {
97          DirectoryBuilder dir = directoryMap.get(name);
98          if (dir == null) {
99            dir = DirectoryBuilder.create();
100           dir.name(name);
101           directoryMap.put(name, dir);
102           resources.add(dir);
103         }
104         retval = dir;
105       } else {
106         retval = retval.getDirectoryResource(name);
107       }
108     }
109     return retval;
110   }
111 
112   /**
113    * Retrieves the child resources that match the specified builder..
114    * 
115    * @param <T>
116    *          the type of builder to filter on
117    * @param clazz
118    *          the builder to filter on
119    * @return the matching resources
120    */
121   public <T extends ResourceBuilder> List<T> getResources(Class<T> clazz) {
122     // List<T> retval = new LinkedList<>();
123     // for (ResourceBuilder builder : resources) {
124     // if (clazz.isInstance(builder)) {
125     // retval.add((T)builder);
126     // }
127     // }
128     @SuppressWarnings("unchecked")
129     List<? extends T> retval
130         = resources.stream().filter(e -> clazz.isInstance(e)).map(e -> (T) e).collect(Collectors.toList());
131     return Collections.unmodifiableList(retval);
132   }
133 
134   public List<ResourceBuilder> getResources() {
135     return Collections.unmodifiableList(resources);
136   }
137 
138   @Override
139   public void validate() throws ValidationException {
140     super.validate();
141   }
142 
143 }