001: /*
002: * Copyright 2001-2007 Hippo (www.hippo.nl)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.cms.editor.flow;
017:
018: import org.apache.avalon.framework.service.ServiceException;
019: import org.apache.avalon.framework.service.ServiceManager;
020: import org.apache.avalon.framework.service.Serviceable;
021: import java.util.HashMap;
022: import java.io.IOException;
023: import org.w3c.dom.Document;
024: import org.w3c.dom.NodeList;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.Element;
027: import org.xml.sax.InputSource;
028: import org.xml.sax.SAXException;
029: import org.apache.excalibur.source.SourceResolver;
030: import org.apache.excalibur.source.Source;
031: import org.apache.cocoon.forms.util.DomHelper;
032: import org.apache.avalon.framework.logger.AbstractLogEnabled;
033:
034: public class FlowHelperImpl extends AbstractLogEnabled implements
035: FlowHelper, Serviceable {
036: private ServiceManager m_manager;
037:
038: public void service(ServiceManager arg0) throws ServiceException {
039: m_manager = arg0;
040: }
041:
042: /**
043: * Load a document from source identified by sourceId
044: *
045: * @param sourceId
046: * The location of the document to return
047: * @throws UIDGeneratorException
048: * @throws ServiceException
049: * @throws IOException
050: * @throws SAXException
051: * @throws ParserConfigurationException
052: */
053: public Document loadDocument(String sourceId) throws IOException,
054: ServiceException, SAXException {
055: Source source = null;
056: SourceResolver resolver = null;
057:
058: try {
059: resolver = (SourceResolver) m_manager
060: .lookup(SourceResolver.ROLE);
061: source = resolver.resolveURI(sourceId);
062:
063: InputSource is = new InputSource(source.getInputStream());
064:
065: return DomHelper.parse(is, m_manager);
066: } finally {
067:
068: if (source != null)
069: resolver.release(source);
070:
071: m_manager.release(resolver);
072: }
073: }
074:
075: public static String getPropertyHashKey(String name,
076: String namespace) {
077: return namespace + ":" + name;
078: }
079:
080: public HashMap getDocumentProperties(String type, String resourceId) {
081: HashMap hm = new HashMap();
082:
083: Document d = null;
084: try {
085: d = loadDocument("cocoon://editing/cf2/documentProperties/"
086: + type + resourceId);
087: } catch (Exception e) {
088: getLogger().error("Error loading document properties.", e);
089: }
090:
091: if (d == null)
092: return hm;
093:
094: NodeList props = d.getElementsByTagName("property");
095:
096: for (int i = 0; i < props.getLength(); i++) {
097: if (props.item(i).getNodeType() == Node.ELEMENT_NODE) {
098: Element prop = (Element) props.item(i);
099: if (prop.getAttribute("name") != null
100: && prop.getAttribute("namespace") != null) {
101: String propertyName = prop.getAttribute("name");
102: String propertyNamespace = prop
103: .getAttribute("namespace");
104: String propertyValue = (prop.getAttribute("value") != null) ? prop
105: .getAttribute("value")
106: : "";
107: hm.put(Property.getHashKey(propertyName,
108: propertyNamespace), propertyValue);
109: }
110: }
111: }
112:
113: return hm;
114: }
115:
116: public void log(String s, Exception e) {
117: getLogger().debug(s, e);
118: }
119:
120: public void log(String s) {
121: getLogger().debug(s);
122: }
123:
124: }
|