View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.util.ArrayList;
22  import java.util.regex.Pattern;
23  
24  /**
25   * Abstract store class provided as a convenience for developers
26   * who wish to build their own custom Store implementations for use
27   * with the Web Script framework.
28   * 
29   * @author muzquiano
30   */
31  public abstract class AbstractStore implements Store
32  {    
33      private PreviewContextProvider previewContextProvider = null;
34      
35      /**
36       * Sets the preview context provider
37       * 
38       * @param previewContextProvider
39       */
40      public void setPreviewContextProvider(PreviewContextProvider previewContextProvider)
41      {
42          this.previewContextProvider = previewContextProvider;
43      }
44      
45      /**
46       * Gets the preview context
47       * 
48       * @return preview context
49       */
50      public PreviewContext getPreviewContext()
51      {
52          PreviewContext previewContext = null;
53          
54          if (this.previewContextProvider != null)
55          {
56              previewContext = this.previewContextProvider.provide();
57          }
58          
59          return previewContext;
60      }
61      
62      /* (non-Javadoc)
63       * @see org.alfresco.web.scripts.Store#getDocumentPaths(java.lang.String, java.lang.String)
64       */
65      public String[] getDocumentPaths(String path, String filePathPattern)
66      {
67          /**
68           * An in-memory full-path pattern matching implementation of the
69           * getDocumentPaths method for file path pattern matching.
70           * 
71           * Inheriting classes may wish to override this to provide more
72           * efficient lookups. 
73           */
74  
75          ArrayList<String> array = new ArrayList<String>(64);
76          
77          // convert to a regular expression
78          String regexPattern = filePathPattern.replaceAll("\\*", ".*");        
79          if (!regexPattern.startsWith(".*"))
80          {
81              regexPattern = ".*" + regexPattern;
82          }
83          
84          // compile regular expression
85          Pattern pattern = Pattern.compile(regexPattern);
86          
87          // get all document paths
88          String[] allDocumentPaths = this.getAllDocumentPaths();
89          
90          // process matches
91          for (int i = 0; i < allDocumentPaths.length; i++)
92          {
93              String documentPath = allDocumentPaths[i];
94              
95              // fix up document paths so they match /a/b/c.gif
96              
97              documentPath = documentPath.replace("\\", "/");
98              if (!documentPath.startsWith("/"))
99              {
100                 documentPath = "/" + documentPath;
101             }
102             
103             if (pattern.matcher(documentPath).matches())
104             {
105                 if (documentPath.startsWith("/"))
106                 {
107                     documentPath = documentPath.substring(1);
108                 }
109                 array.add(documentPath);
110             }
111         }
112         
113         return array.toArray(new String[array.size()]);        
114     }
115 }