001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.container.namespace;
018:
019: import org.apache.pluto.om.common.ObjectID;
020:
021: /**
022: * Jetspeed implementation of Name space mapping for creating named attributes.
023: *
024: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
025: * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
026: * @version $Id: JetspeedNamespaceMapperImpl.java 551860 2007-06-29 11:56:23Z ate $
027: */
028: public class JetspeedNamespaceMapperImpl implements
029: JetspeedNamespaceMapper {
030: private String prefix;
031:
032: public JetspeedNamespaceMapperImpl(String prefix) {
033: this .prefix = prefix;
034: if (this .prefix == null) {
035: this .prefix = DEFAULT_PREFIX;
036: }
037: }
038:
039: public JetspeedNamespaceMapperImpl() {
040: this (null);
041: }
042:
043: public String getPrefix() {
044: return prefix;
045: }
046:
047: public String encode(String ns, String name) {
048: return join(prefix, ns, "_", name, null, null);
049: }
050:
051: public String encode(String ns1, String ns2, String name) {
052: return join(prefix, ns1, "_", ns2, "_", name);
053: }
054:
055: public String decode(String ns, String name) {
056: if (!name.startsWith(prefix))
057: return null;
058: String tmp = join(prefix, ns, "_", null, null, null);
059: if (!name.startsWith(tmp))
060: return null;
061: return name.substring(tmp.length());
062: }
063:
064: public String encode(long id, String name) {
065: return encode(new Long(id).toString(), name);
066: }
067:
068: /* (non-Javadoc)
069: * @see org.apache.pluto.util.NamespaceMapper#encode(org.apache.pluto.om.common.ObjectID, java.lang.String)
070: */
071: public String encode(ObjectID ns, String name) {
072: return encode(ns.toString(), name);
073: }
074:
075: /* (non-Javadoc)
076: * @see org.apache.pluto.util.NamespaceMapper#encode(org.apache.pluto.om.common.ObjectID, org.apache.pluto.om.common.ObjectID, java.lang.String)
077: */
078: public String encode(ObjectID ns1, ObjectID ns2, String name) {
079: return encode(ns1.toString(), ns2.toString(), name);
080: }
081:
082: /* (non-Javadoc)
083: * @see org.apache.pluto.util.NamespaceMapper#decode(org.apache.pluto.om.common.ObjectID, java.lang.String)
084: */
085: public String decode(ObjectID ns, String name) {
086: return decode(ns.toString(), name);
087: }
088:
089: private static String join(String s1, String s2, String s3,
090: String s4, String s5, String s6) {
091: int len = 0;
092: if (s1 != null) {
093: len += s1.length();
094: if (s2 != null) {
095: len += s2.length();
096: if (s3 != null) {
097: len += s3.length();
098: if (s4 != null) {
099: len += s4.length();
100: if (s5 != null) {
101: len += s5.length();
102: if (s6 != null) {
103: len += s6.length();
104: }
105: }
106: }
107: }
108: }
109: }
110: char[] buffer = new char[len];
111: int index = 0;
112: if (s1 != null) {
113: len = s1.length();
114: s1.getChars(0, len, buffer, index);
115: index += len;
116: if (s2 != null) {
117: len = s2.length();
118: s2.getChars(0, len, buffer, index);
119: index += len;
120: if (s3 != null) {
121: len = s3.length();
122: s3.getChars(0, len, buffer, index);
123: index += len;
124: if (s4 != null) {
125: len = s4.length();
126: s4.getChars(0, len, buffer, index);
127: index += len;
128: if (s5 != null) {
129: len = s5.length();
130: s5.getChars(0, len, buffer, index);
131: index += len;
132: if (s6 != null) {
133: len = s6.length();
134: s6.getChars(0, len, buffer, index);
135: }
136: }
137: }
138: }
139: }
140: }
141: return new String(buffer);
142: }
143: }
|