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.lenya.cms.repository;
019:
020: import java.io.OutputStream;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.lenya.cms.metadata.MetaDataException;
028: import org.apache.lenya.xml.DocumentHelper;
029: import org.apache.lenya.xml.NamespaceHelper;
030: import org.w3c.dom.Element;
031:
032: /**
033: * Modifiable meta data handler.
034: */
035: public class ModifiableMetaDataHandler extends
036: SourceNodeMetaDataHandler implements Persistable {
037:
038: private MetaSourceWrapper sourceWrapper;
039: private boolean changed = false;
040:
041: /**
042: * @param manager The service manager.
043: * @param sourceWrapper The source wrapper.
044: */
045: public ModifiableMetaDataHandler(ServiceManager manager,
046: MetaSourceWrapper sourceWrapper) {
047: super (manager, sourceWrapper.getRealSourceUri());
048: this .sourceWrapper = sourceWrapper;
049: try {
050: this .sourceWrapper.getNode().setPersistable(this );
051: } catch (RepositoryException e) {
052: throw new RuntimeException(e);
053: }
054: }
055:
056: public void save() throws RepositoryException {
057: if (!changed) {
058: return;
059: }
060: try {
061: NamespaceHelper helper = new NamespaceHelper(
062: META_DATA_NAMESPACE, "", ELEMENT_METADATA);
063: Collection namespaces = this .namespace2metamap.keySet();
064: for (Iterator i = namespaces.iterator(); i.hasNext();) {
065: String namespace = (String) i.next();
066:
067: Element setElement = helper.createElement(ELEMENT_SET);
068: setElement.setAttribute(ATTRIBUTE_NAMESPACE, namespace);
069: helper.getDocument().getDocumentElement().appendChild(
070: setElement);
071:
072: Map map = getMetaDataMap(namespace);
073: Collection keys = map.keySet();
074: for (Iterator keyIterator = keys.iterator(); keyIterator
075: .hasNext();) {
076: String key = (String) keyIterator.next();
077:
078: Element elementElement = helper
079: .createElement(ELEMENT_ELEMENT);
080: elementElement.setAttribute(ATTRIBUTE_KEY, key);
081:
082: List values = (List) map.get(key);
083: for (Iterator valueIterator = values.iterator(); valueIterator
084: .hasNext();) {
085: String value = (String) valueIterator.next();
086: if (!value.equals("")) {
087: Element valueElement = helper
088: .createElement(ELEMENT_VALUE, value);
089: elementElement.appendChild(valueElement);
090: }
091: }
092: if (elementElement.hasChildNodes()) {
093: setElement.appendChild(elementElement);
094: }
095: }
096: }
097: OutputStream oStream = this .sourceWrapper.getOutputStream();
098: DocumentHelper.writeDocument(helper.getDocument(), oStream);
099: if (oStream != null) {
100: oStream.flush();
101: try {
102: oStream.close();
103: } catch (Throwable t) {
104: throw new RuntimeException(
105: "Could not write meta XML: ", t);
106: }
107: }
108: } catch (Exception e) {
109: throw new RepositoryException(e);
110: }
111: }
112:
113: protected void addValue(String namespaceUri, String key,
114: String value) throws MetaDataException {
115: List values = getValueList(namespaceUri, key);
116: values.add(value);
117: changed();
118: }
119:
120: protected void removeAllValues(String namespaceUri, String key)
121: throws MetaDataException {
122: List values = getValueList(namespaceUri, key);
123: values.clear();
124: changed();
125: }
126:
127: protected void setValue(String namespaceUri, String key,
128: String value) throws MetaDataException {
129: List values = getValueList(namespaceUri, key);
130: values.clear();
131: values.add(value);
132: changed();
133: }
134:
135: private void changed() {
136: this .changed = true;
137: }
138:
139: public boolean isModified() {
140: return this.changed;
141: }
142:
143: }
|