001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.publishing;
020:
021: import java.util.logging.*;
022:
023: import org.openharmonise.commons.dsi.*;
024: import org.openharmonise.commons.xml.*;
025: import org.openharmonise.rm.factory.*;
026: import org.openharmonise.rm.resources.*;
027: import org.openharmonise.rm.resources.publishing.*;
028: import org.openharmonise.rm.resources.users.*;
029: import org.openharmonise.rm.security.authorization.*;
030: import org.w3c.dom.*;
031:
032: /**
033: * An extension to <code>XMLDocument</code> which can hold the output
034: * of the publication of resources specified by input HaRP and provides
035: * some utility methods relevant to HaRP output.
036: *
037: * @author Michael Bell
038: * @version $Revision: 1.2 $
039: *
040: */
041: public class HarmoniseOutput extends XMLDocument {
042:
043: AbstractDataStoreInterface m_dsi = null;
044:
045: /**
046: * Logger for this class
047: */
048: private static final Logger m_logger = Logger
049: .getLogger(HarmoniseOutput.class.getName());
050:
051: public HarmoniseOutput(AbstractDataStoreInterface dsi) {
052: super ();
053: m_dsi = dsi;
054: }
055:
056: public HarmoniseOutput(org.w3c.dom.Document document,
057: AbstractDataStoreInterface dsi) {
058: super (document);
059: m_dsi = dsi;
060: }
061:
062: /**
063: * Utility method to ease processing of Submit buttons.
064: *
065: * @exception Exception
066: * @param submit Submit element from input XML
067: * @param state State XML document
068: * @param output Output XML document, passed because Xerces requires
069: * Nodes to be owned by Document
070: * @return Output Submit element, null if failed FilterConditions
071: */
072: public Element processSubmitTag(Element submit, State state) {
073: Element newSubmit = null;
074:
075: newSubmit = (Element) copyNode(submit);
076:
077: return newSubmit;
078: }
079:
080: /** Adds a Page element under the link node found under el with the given page id.
081: *
082: * @param el Element with descendant Link element
083: * @param output Output document
084: * @param nPageId Page id of page element to be added
085: */
086: public void addPageIdToLinkNode(User usr, Element el, int nPageId) {
087: NodeList linkNodes = el
088: .getElementsByTagName(AbstractObject.TAG_LINK);
089:
090: try {
091: WebPage page = (WebPage) HarmoniseObjectFactory
092: .instantiateHarmoniseObject(this .m_dsi,
093: WebPage.class.getName(), nPageId);
094:
095: boolean bIsVisible = AuthorizationValidator.isVisible(usr,
096: page);
097:
098: for (int i = 0; i < linkNodes.getLength(); i++) {
099: Element linkNode = (Element) linkNodes.item(i);
100: if (bIsVisible == false) {
101: linkNode.setAttribute(
102: AuthorizationValidator.ATTRIB_IS_VIEWABLE,
103: "false");
104: }
105:
106: NodeList pageNodes = linkNode
107: .getElementsByTagName(WebPage.TAG_PAGE);
108:
109: if (pageNodes.getLength() == 0) {
110: Node pageNode = createElement(WebPage.TAG_PAGE);
111: ((Element) pageNode).setAttribute(
112: AbstractObject.ATTRIB_ID, Integer
113: .toString(nPageId));
114:
115: linkNode.appendChild(pageNode);
116: }
117:
118: }
119: } catch (Exception e) {
120: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
121: }
122: }
123:
124: /** Create an error XML element in the document.
125: *
126: * @param error_code Code of the error to create
127: * @param form that has set the error, if one exists
128: */
129: public Element createErrorElement(String error_code,
130: Element form_element) {
131: NodeList nodes = form_element
132: .getElementsByTagName(XMLFormErrorCodes.TAG_ERRORTEXT);
133: Element error_element = createElement(AbstractObject.TAG_ERROR);
134:
135: String error_text = null;
136:
137: for (int i = 0; i < nodes.getLength(); i++) {
138: Element form_error_element = (Element) nodes.item(i);
139:
140: if (((String) form_error_element
141: .getAttribute(XMLFormErrorCodes.ATTRIB_ERRORCODE))
142: .equals(error_code)) {
143: Text txt = (Text) form_error_element.getFirstChild();
144: error_text = txt.getNodeValue();
145: }
146: }
147:
148: if (error_text == null) {
149: //wasn't set from form
150: error_text = XMLFormErrorCodes.getInstance().getErrorText(
151: error_code);
152: }
153:
154: Node txt;
155:
156: if (error_text != null) {
157: txt = createTextNode(error_text);
158: } else {
159: throw new RuntimeException("Error code unknown :"
160: + error_code);
161: }
162:
163: error_element.appendChild(txt);
164: error_element.setAttribute(XMLFormErrorCodes.ATTRIB_ERRORCODE,
165: error_code);
166:
167: return error_element;
168: }
169: }
|