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:
018: package org.apache.jetspeed.serializer.objects;
019:
020: /**
021: * Serialized Name Value Pairs <info> <name>user.first.name</name> <value>Paul</value>
022: * </info>
023: *
024: * @author <a href="mailto:hajo@bluesunrsie.com">Hajo Birthelmer</a>
025: * @version $Id: $
026: */
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.prefs.Preferences;
030:
031: import javolution.xml.XMLFormat;
032: import javolution.xml.sax.Attributes;
033: import javolution.xml.stream.XMLStreamException;
034:
035: import org.apache.commons.lang.StringEscapeUtils;
036:
037: public class JSNameValuePairs {
038:
039: private HashMap myMap = new HashMap();
040:
041: public int size() {
042: return myMap.size();
043:
044: }
045:
046: public JSNameValuePairs() {
047: }
048:
049: public HashMap getMyMap() {
050: return myMap;
051: }
052:
053: public void add(String key, String value) {
054: myMap.put(key, value);
055: }
056:
057: /**
058: * @param arg0
059: */
060: public JSNameValuePairs(Preferences preferences) {
061: try {
062: String[] strings = preferences.keys();
063: if ((strings != null) && (strings.length > 0)) {
064: int i = strings.length;
065: for (int j = 0; j < i; j++)
066: myMap.put(strings[j], preferences.get(strings[j],
067: "?????"));
068: }
069:
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073: }
074:
075: /***************************************************************************
076: * SERIALIZER
077: */
078: private static final XMLFormat XML = new XMLFormat(
079: JSNameValuePairs.class) {
080:
081: public void write(Object o, OutputElement xml)
082: throws XMLStreamException {
083: try {
084: JSNameValuePairs g = (JSNameValuePairs) o;
085: Iterator _it = g.myMap.keySet().iterator();
086: while (_it.hasNext()) {
087: String _key = (String) _it.next();
088: // xml.add((String) g.get(_key), _key, String.class);
089: xml.setAttribute(_key, (String) g.myMap.get(_key));
090: }
091: } catch (Exception e) {
092: e.printStackTrace();
093: }
094: }
095:
096: public void read(InputElement xml, Object o) {
097:
098: try {
099: JSNameValuePairs g = (JSNameValuePairs) o;
100: Attributes attribs = xml.getAttributes();
101: int len = attribs.getLength();
102:
103: for (int i = 0; i < len; i++) {
104: try {
105: String _key = StringEscapeUtils
106: .unescapeHtml(attribs.getLocalName(i)
107: .toString());
108: String _value = StringEscapeUtils
109: .unescapeHtml(attribs.getValue(i)
110: .toString());
111: g.myMap.put(_key, _value);
112: } catch (Exception e) {
113: /**
114: * while annoying invalid entries in the file should be
115: * just disregarded
116: */
117: e.printStackTrace();
118: }
119: }
120: } catch (Exception e) {
121: e.printStackTrace();
122: }
123: }
124: };
125:
126: }
|