01: /*
02: *
03: * Copyright (c) 2004 SourceTap - www.sourcetap.com
04: *
05: * The contents of this file are subject to the SourceTap Public License
06: * ("License"); You may not use this file except in compliance with the
07: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
08: * Software distributed under the License is distributed on an "AS IS" basis,
09: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
10: * the specific language governing rights and limitations under the License.
11: *
12: * The above copyright notice and this permission notice shall be included
13: * in all copies or substantial portions of the Software.
14: *
15: */
16:
17: package com.sourcetap.sfa.util;
18:
19: import org.ofbiz.entity.GenericEntityException;
20:
21: import java.util.HashMap;
22: import java.util.StringTokenizer;
23:
24: /**
25: * DOCUMENT ME!
26: *
27: */
28: public class DelimitedPairDecoder {
29: protected String stringToDecode = "";
30:
31: public DelimitedPairDecoder(String inputString) {
32: stringToDecode = inputString;
33: }
34:
35: /**
36: * DOCUMENT ME!
37: *
38: * @return
39: *
40: * @throws GenericEntityException
41: */
42: public HashMap decode() throws GenericEntityException {
43: // Split the string into pieces. Example:
44: // * string to decode:
45: // * sectionId:sectionId;partyId:"-1"
46: // * Resulting map:
47: // * sectionId:43
48: // * partyId:"-1"
49: HashMap resultMap = new HashMap();
50:
51: if ((stringToDecode == null) || (stringToDecode.length() < 1)
52: || (stringToDecode.equals("null")))
53: return resultMap;
54:
55: StringTokenizer tokSemicolon = new StringTokenizer(
56: stringToDecode, ";");
57:
58: while (tokSemicolon.hasMoreTokens()) {
59: String pair = tokSemicolon.nextToken();
60: StringTokenizer tokColon = new StringTokenizer(pair, ":");
61:
62: if (tokColon.countTokens() != 2) {
63: // There must be 2 items in the pair separated by colons.
64: throw new GenericEntityException(
65: "Each pair to be decoded must have a colon ("
66: + stringToDecode + ")");
67: } else {
68: String mapKey = tokColon.nextToken();
69: String mapValue = tokColon.nextToken();
70: resultMap.put(mapKey, mapValue);
71: }
72:
73: tokColon = null;
74: }
75:
76: tokSemicolon = null;
77:
78: return resultMap;
79: }
80: }
|