1 31 32 package org.opencms.test; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsResource; 36 import org.opencms.main.CmsException; 37 38 import java.util.HashMap ; 39 import java.util.Map ; 40 41 47 public class OpenCmsTestResourceStorage { 48 49 50 public static String DEFAULT_STORAGE = "default"; 51 52 53 public static String GLOBAL_STORAGE = "global"; 54 55 56 private String m_name; 57 58 59 private Map m_precalcState; 60 61 62 private String m_sourceNameMapping; 63 64 65 private Map m_storage; 66 67 68 private String m_targetNameMapping; 69 70 75 public OpenCmsTestResourceStorage(String name) { 76 77 m_storage = new HashMap (); 78 m_precalcState = new HashMap (); 79 m_sourceNameMapping = null; 80 m_targetNameMapping = null; 81 m_name = name; 82 } 83 84 92 public void add(CmsObject cms, String resourceName, CmsResource resource) throws CmsException { 93 94 m_storage.put(resourceName, new OpenCmsTestResourceStorageEntry(cms, resourceName, resource)); 95 m_precalcState.put(resourceName, preCalculateState(resource)); 96 } 97 98 99 106 public OpenCmsTestResourceStorageEntry get(String resourceName) throws Exception { 107 108 String mappedResourceName = mapResourcename(resourceName); 109 110 OpenCmsTestResourceStorageEntry entry = null; 111 entry = (OpenCmsTestResourceStorageEntry)m_storage.get(mappedResourceName); 112 113 if (entry == null) { 114 throw new Exception ("Not found in storage " + resourceName + " -> " + mappedResourceName); 115 } 116 117 return entry; 118 } 119 120 125 public String getName() { 126 127 return m_name; 128 } 129 130 135 public int size() { 136 return m_storage.size(); 137 } 138 139 146 public int getPreCalculatedState(String resourceName) throws Exception { 147 148 String mappedResourceName = mapResourcename(resourceName); 149 150 Integer state = null; 151 state = (Integer )m_precalcState.get(mappedResourceName); 152 if (state == null) { 153 throw new Exception ("Not found in storage " + resourceName + " -> " + mappedResourceName); 154 } 155 return state.intValue(); 156 } 157 158 159 160 165 public String getSourceNameMapping() { 166 167 return m_sourceNameMapping; 168 } 169 170 175 public String getTargetNameMapping() { 176 177 return m_targetNameMapping; 178 } 179 180 186 public void setMapping(String source, String target) { 187 188 m_sourceNameMapping = source; 189 m_targetNameMapping = target; 190 } 191 192 195 public void resetMapping() { 196 197 m_sourceNameMapping = null; 198 m_targetNameMapping = null; 199 } 200 201 210 public String mapResourcename(String resourceName) { 211 212 if (m_sourceNameMapping != null && m_targetNameMapping != null) { 214 if (resourceName.startsWith(m_sourceNameMapping)) { 216 resourceName = m_targetNameMapping + resourceName.substring(m_sourceNameMapping.length()); 218 } 219 } 220 return resourceName; 221 } 222 223 224 238 private Integer preCalculateState(CmsResource res) { 239 240 int newState = CmsResource.STATE_UNCHANGED; 241 int state = res.getState(); 242 switch (state) { 243 case CmsResource.STATE_UNCHANGED: 244 newState = CmsResource.STATE_CHANGED; 245 break; 246 case CmsResource.STATE_CHANGED: 247 newState = CmsResource.STATE_CHANGED; 248 break; 249 case CmsResource.STATE_NEW: 250 newState = CmsResource.STATE_NEW; 251 break; 252 case CmsResource.STATE_DELETED: 253 newState = CmsResource.STATE_DELETED; 254 break; 255 default: 256 newState = CmsResource.STATE_UNCHANGED; 257 break; 258 } 259 return new Integer (newState); 260 } 261 } | Popular Tags |