View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.List;
7   import java.util.Map;
8   import java.util.Set;
9   import java.util.regex.Matcher;
10  import java.util.regex.Pattern;
11  
12  import org.apache.commons.lang.StringEscapeUtils;
13  import org.apache.commons.lang.StringUtils;
14  
15  /**
16   * The WebResourceFormatter should take a PluginResource and add the BatchResourceContents dependency information to 
17   * the output as required.
18   */
19  class BatchResourceContentsWebFormatter
20  {
21      /**
22       * Match the opening of an HTML tag up to the first attribute or a tag with no attributes at
23       * all (i.e. followed immediately with the > symbol).
24       */
25      private static final Pattern TAG_NAME_PATTERN = Pattern.compile("(<\\w+)[\\s|>]");
26      
27      /**
28       * Modify the supplied formattedResource to contain any necessary data attributes required to carry additional
29       * information about the PluginResource being written.
30       * 
31       * @param resource
32       * @param formattedResource
33       * @return
34       */
35      static String insertBatchResourceContents(final PluginResource resource, String formattedResource)
36      {
37          Map<String,String> dependencyAttributes = createDependencyAttributes(resource);
38          if (dependencyAttributes.isEmpty())
39              return formattedResource;
40          
41          List<String> nameValues = new ArrayList<String>(dependencyAttributes.size());
42          for (Map.Entry<String,String> attr : dependencyAttributes.entrySet())
43          {
44              String nameValuePair = StringEscapeUtils.escapeHtml(attr.getKey()) + "=\"" + StringEscapeUtils.escapeHtml(attr.getValue()) + "\"";
45              nameValues.add(nameValuePair);
46          }
47          
48          String nameValuesStr = StringUtils.join(nameValues, ' ');
49          
50          Matcher matcher = TAG_NAME_PATTERN.matcher(formattedResource);
51          if (matcher.find())
52          {
53              StringBuilder builder = new StringBuilder(formattedResource);
54              builder.insert(matcher.end(1), " " + nameValuesStr);
55              return builder.toString();
56          }
57          
58          return formattedResource;
59      }
60      
61      static private Map<String,String> createDependencyAttributes(final PluginResource resource)
62      {
63          Map<String,String> dependencyAttributes = new HashMap<String,String>();
64          
65         Set<BatchedWebResourceDescriptor> descriptors = resource.getBatchedWebResourceDescriptors();
66          List<String> dependencyValues = new ArrayList<String>(descriptors.size());
67          for (BatchedWebResourceDescriptor descriptor : descriptors)
68          {
69              String attributeValue = descriptor.getCompleteKey() + "[" + descriptor.getPluginVersion() + "]";
70              dependencyValues.add(attributeValue);
71          }
72          
73          if (!dependencyValues.isEmpty())
74          {
75              dependencyAttributes.put("data-atlassian-webresource-contents", StringUtils.join(dependencyValues, ','));
76          }
77          
78          if (resource instanceof ContextBatchPluginResource)
79          {
80              ContextBatchPluginResource contextBatchPluginResource = (ContextBatchPluginResource)resource;
81              
82              Iterator<String> contextsIterator = contextBatchPluginResource.getContexts().iterator();
83              if (contextsIterator.hasNext())
84              {
85                  dependencyAttributes.put("data-atlassian-webresource-contexts", StringUtils.join(contextsIterator, ','));
86              }
87  
88              Iterator<String> excludedContextsIterator = contextBatchPluginResource.getExcludedContexts().iterator();
89              if (excludedContextsIterator.hasNext())
90              {
91                  dependencyAttributes.put("data-atlassian-webresource-excluded-contexts", StringUtils.join(excludedContextsIterator,','));                
92              }
93          }
94          
95          return dependencyAttributes;
96      }
97  }