This project has retired. For details please refer to its Attic page.
ContentTypeCacheLevelImpl xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.cache.impl;
20  
21  import java.util.Map;
22  import java.util.SortedMap;
23  import java.util.TreeMap;
24  
25  /**
26   * Content type cache.
27   *
28   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
29   *
30   */
31  public class ContentTypeCacheLevelImpl extends MapCacheLevelImpl {
32  
33      private static final long serialVersionUID = 1L;
34  
35      /**
36       * Constructor.
37       */
38      public ContentTypeCacheLevelImpl() {
39          enableKeyFallback(null);
40      }
41  
42      @Override
43      public Object get(String key) {
44          return super.get(normalize(key));
45      }
46  
47      @Override
48      public void put(Object value, String key) {
49          super.put(value, normalize(key));
50      }
51  
52      @Override
53      public void remove(String key) {
54          super.remove(normalize(key));
55      }
56  
57      /**
58       * Normalizes the key which should be a content type. It's quite simple at
59       * the moment but should cover most cases.
60       */
61      private static String normalize(String key) {
62          if (key == null) {
63              return null;
64          }
65  
66          StringBuilder sb = new StringBuilder();
67          int parameterStart = 0;
68  
69          // first, get the MIME type
70          for (int i = 0; i < key.length(); i++) {
71              char c = key.charAt(i);
72  
73              if (Character.isWhitespace(c)) {
74                  continue;
75              } else if (c == ';') {
76                  parameterStart = i;
77                  break;
78              }
79  
80              sb.append(Character.toLowerCase(c));
81          }
82  
83          // if parameters have been found, gather them
84          if (parameterStart > 0) {
85              SortedMap<String, String> parameter = new TreeMap<String, String>();
86              StringBuilder ksb = new StringBuilder();
87              StringBuilder vsb = new StringBuilder();
88              boolean isKey = true;
89  
90              for (int i = parameterStart + 1; i < key.length(); i++) {
91                  char c = key.charAt(i);
92                  if (Character.isWhitespace(c)) {
93                      continue;
94                  }
95  
96                  if (isKey) {
97                      if (c == '=') {
98                          // value start
99                          isKey = false;
100                         continue;
101                     }
102 
103                     ksb.append(Character.toLowerCase(c));
104                 } else {
105                     if (c == ';') {
106                         // next key
107                         isKey = true;
108 
109                         parameter.put(ksb.toString(), vsb.toString());
110 
111                         ksb = new StringBuilder();
112                         vsb = new StringBuilder();
113 
114                         continue;
115                     } else if (c == '"') {
116                         // filter quotes
117                         continue;
118                     }
119 
120                     vsb.append(Character.toLowerCase(c));
121                 }
122             }
123 
124             // add last parameter
125             if (ksb.length() > 0) {
126                 parameter.put(ksb.toString(), vsb.toString());
127             }
128 
129             // write parameters sorted by key
130             for (Map.Entry<String, String> entry : parameter.entrySet()) {
131                 sb.append(";");
132                 sb.append(entry.getKey());
133                 sb.append("=");
134                 sb.append(entry.getValue());
135             }
136         }
137 
138         return sb.toString();
139     }
140 }