Skip to main content
summaryrefslogtreecommitdiffstats
blob: c36d1b89712714b1a9983ff1bd88cd44b4f093b0 (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
/*******************************************************************************
 * 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.resource.management.util;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.framework.resource.management.IResourceLocator;
import org.eclipse.osee.framework.resource.management.StandardOptions;
import org.eclipse.osee.framework.resource.management.exception.MalformedLocatorException;

/**
 * @author Andrew M. Finkbeiner
 */
public class OptionsProcessor {

   private final URI fileuri;
   private final URI locatoruri;
   private final IResource resource;
   private final String extension;
   private final boolean deCompressOnSave;
   private final boolean shouldCompress;
   private final boolean decompressOnAcquire;
   private final boolean compressOnAcquire;
   private final boolean overwrite;

   public OptionsProcessor(URI uri, IResourceLocator locator, IResource resource, PropertyStore options) throws MalformedLocatorException {
      this.resource = resource;
      decompressOnAcquire = options.getBoolean(StandardOptions.DecompressOnAquire.name());
      compressOnAcquire = options.getBoolean(StandardOptions.CompressOnAcquire.name());
      overwrite = options.getBoolean(StandardOptions.Overwrite.name());
      shouldCompress = options.getBoolean(StandardOptions.CompressOnSave.name());
      deCompressOnSave = options.getBoolean(StandardOptions.DecompressOnSave.name());
      extension = options.get(StandardOptions.Extension.name());

      StringBuilder sb = new StringBuilder(uri.toString());
      StringBuilder sb2 = new StringBuilder(locator.toString());
      if (Strings.isValid(extension)) {
         sb.append(".");
         sb.append(extension);
         sb2.append(".");
         sb2.append(extension);
      }
      if (shouldCompress) {
         sb.append(".");
         sb.append("zip");
         sb2.append(".");
         sb2.append("zip");
      }
      try {
         this.fileuri = new URI(sb.toString());
      } catch (URISyntaxException ex) {
         throw new MalformedLocatorException(sb.toString(), ex);
      }
      try {
         this.locatoruri = new URI(sb2.toString());
      } catch (URISyntaxException ex) {
         throw new MalformedLocatorException(sb2.toString(), ex);
      }
   }

   public File getStorageFile() throws OseeCoreException {
      File storageFile = new File(fileuri);
      if (!overwrite) {
         if (storageFile.exists()) {
            throw new OseeStateException("The file [%s] already exists.", storageFile.getAbsolutePath());
         }
      }
      File parent = storageFile.getParentFile();
      if (parent != null && !parent.exists()) {
         parent.mkdirs();
      }
      return storageFile;
   }

   public IResource getResourceToStore() throws OseeCoreException {
      IResource resourceToReturn;
      if (shouldCompress && !resource.isCompressed()) {
         resourceToReturn = Resources.compressResource(resource);
      } else if (deCompressOnSave && resource.isCompressed()) {
         resourceToReturn = Resources.decompressResource(resource);
      } else {
         resourceToReturn = resource;
      }
      return resourceToReturn;
   }

   public IResource getResourceToServer() throws OseeCoreException {
      IResource toReturn = null;
      File testFile = new File(this.fileuri);
      if (testFile.exists()) {
         boolean isCompressed = Lib.isCompressed(testFile);
         toReturn = new Resource(this.fileuri, isCompressed);

         if (compressOnAcquire && !isCompressed) {
            toReturn = Resources.compressResource(toReturn);
         } else if (decompressOnAcquire && isCompressed) {
            toReturn = Resources.decompressResource(toReturn);
         }
      }
      return toReturn;
   }

   public IResourceLocator getActualResouceLocator() throws OseeCoreException {
      return new ResourceLocator(this.locatoruri);
   }

}

Back to the top