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.stream.XMLStreamException;
033:
034: public class JSNVPElements {
035:
036: private HashMap myMap = new HashMap();
037:
038: public int size() {
039: return myMap.size();
040:
041: }
042:
043: public JSNVPElements() {
044: }
045:
046: public HashMap getMyMap() {
047: return myMap;
048: }
049:
050: public void add(String key, String value) {
051: myMap.put(key, value);
052: }
053:
054: /**
055: * @param arg0
056: */
057: public JSNVPElements(Preferences preferences) {
058: try {
059: String[] strings = preferences.keys();
060: if ((strings != null) && (strings.length > 0)) {
061: int i = strings.length;
062: for (int j = 0; j < i; j++)
063: myMap.put(strings[j], preferences.get(strings[j],
064: "?????"));
065: }
066:
067: } catch (Exception e) {
068: e.printStackTrace();
069: }
070: }
071:
072: /***************************************************************************
073: * SERIALIZER
074: */
075: private static final XMLFormat XML = new XMLFormat(
076: JSNVPElements.class) {
077:
078: public void write(Object o, OutputElement xml)
079: throws XMLStreamException {
080: try {
081: JSNVPElements g = (JSNVPElements) o;
082: Iterator _it = g.myMap.keySet().iterator();
083: while (_it.hasNext()) {
084: String _key = (String) _it.next();
085: JSNVPElement elem = new JSNVPElement(_key,
086: (String) g.myMap.get(_key));
087: xml.add(elem, "preference", JSNVPElement.class);
088: }
089: } catch (Exception e) {
090: e.printStackTrace();
091: }
092: }
093:
094: public void read(InputElement xml, Object o) {
095:
096: try {
097: JSNVPElements g = (JSNVPElements) o;
098: while (xml.hasNext()) {
099: JSNVPElement elem = (JSNVPElement) xml.get(
100: "preference", JSNVPElement.class);
101: g.myMap.put(elem.getKey(), elem.getValue());
102: }
103: } catch (Exception e) {
104: /**
105: * while annoying invalid entries in the file should be
106: * just disregarded
107: */
108: e.printStackTrace();
109: }
110: }
111: };
112:
113: }
|