Skip to main content
summaryrefslogtreecommitdiffstats
blob: 98e779a01cddfd8619a9bed3f15539232ab31cb6 (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
/*******************************************************************************
 * Copyright (c) 2013 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.jaxrs.server.internal.ext;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
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 com.google.common.io.InputSupplier;

/**
 * @author David W. Miller
 */
public class RestResourceConcatenator {
   private final Set<String> resources = new HashSet<String>();
   private String header;
   private String footer;
   private Pattern resourcePattern = null;

   public void initialize(String xmlTag) {
      if (resourcePattern != null) {
         throw new OseeArgumentException("Already initialized");
      }
      validateTag(xmlTag);
      String regex = String.format("(.*?<%s.*?>)(.+?)(</%s>)", xmlTag, xmlTag);

      resourcePattern = Pattern.compile(regex, Pattern.DOTALL);
   }

   @Override
   public String toString() {
      return String.format("%s Object: \n  contents {%s}\n", this.getClass().getName(), getResources());
   }

   public void addResource(InputSupplier<? extends InputStream> supplier) throws IOException {
      Conditions.checkNotNull(supplier, "InputStreamSupplier");
      InputStream is = null;
      try {
         is = supplier.getInput();
         processResource(Lib.inputStreamToString(is));
      } finally {
         Lib.close(is);
      }
   }

   public InputStream getAsInputStream() throws UnsupportedEncodingException {
      if (resources.isEmpty() || header == null || footer == null) {
         throw new OseeCoreException("Rest resource not ready - no data");
      }
      return Lib.stringToInputStream(getResources());
   }

   public String getResources() {
      StringBuilder sb = new StringBuilder();
      sb.append(header);
      for (String resource : resources) {
         sb.append(resource);
      }
      sb.append(footer);
      return sb.toString();
   }

   private void processResource(String input) {
      Conditions.checkNotNullOrEmpty(input, "bundle resource");
      Matcher match = resourcePattern.matcher(input);
      while (match.find()) {
         if (resources.isEmpty()) {
            header = match.group(1);
            footer = match.group(3);
         }
         resources.add(match.group(2));
      }
   }

   private void validateTag(String xmlTag) {

      Conditions.checkNotNullOrEmpty(xmlTag, "xmlTag");
      // resources can only have three tags: "resourceDoc", "applicationDocs" or "grammars"
      boolean valid = false;
      if (xmlTag.equals("resourceDoc")) {
         valid = true;
      } else if (xmlTag.equals("applicationDocs")) {
         valid = true;
      } else if (xmlTag.equals("grammars")) {
         valid = true;
      }
      if (!valid) {
         throw new OseeArgumentException("Invalid resource document tag");
      }
   }
}

Back to the top