KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > publishing > HarmoniseOutput


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.publishing;
20
21 import java.util.logging.*;
22
23 import org.openharmonise.commons.dsi.*;
24 import org.openharmonise.commons.xml.*;
25 import org.openharmonise.rm.factory.*;
26 import org.openharmonise.rm.resources.*;
27 import org.openharmonise.rm.resources.publishing.*;
28 import org.openharmonise.rm.resources.users.*;
29 import org.openharmonise.rm.security.authorization.*;
30 import org.w3c.dom.*;
31
32
33
34 /**
35  * An extension to <code>XMLDocument</code> which can hold the output
36  * of the publication of resources specified by input HaRP and provides
37  * some utility methods relevant to HaRP output.
38  *
39  * @author Michael Bell
40  * @version $Revision: 1.2 $
41  *
42  */

43 public class HarmoniseOutput extends XMLDocument {
44     
45     AbstractDataStoreInterface m_dsi = null;
46     
47     /**
48      * Logger for this class
49      */

50     private static final Logger m_logger = Logger.getLogger(HarmoniseOutput.class.getName());
51     
52     public HarmoniseOutput(AbstractDataStoreInterface dsi) {
53         super();
54         m_dsi = dsi;
55     }
56
57     public HarmoniseOutput(org.w3c.dom.Document JavaDoc document, AbstractDataStoreInterface dsi) {
58         super(document);
59         m_dsi = dsi;
60     }
61
62     /**
63      * Utility method to ease processing of Submit buttons.
64      *
65      * @exception Exception
66      * @param submit Submit element from input XML
67      * @param state State XML document
68      * @param output Output XML document, passed because Xerces requires
69      * Nodes to be owned by Document
70      * @return Output Submit element, null if failed FilterConditions
71      */

72     public Element processSubmitTag(Element submit, State state) {
73         Element newSubmit = null;
74
75         newSubmit = (Element) copyNode(submit);
76
77         return newSubmit;
78     }
79
80     /** Adds a Page element under the link node found under el with the given page id.
81      *
82      * @param el Element with descendant Link element
83      * @param output Output document
84      * @param nPageId Page id of page element to be added
85      */

86     public void addPageIdToLinkNode(User usr, Element el, int nPageId) {
87         NodeList linkNodes = el.getElementsByTagName(AbstractObject.TAG_LINK);
88
89         try {
90             WebPage page = (WebPage) HarmoniseObjectFactory.instantiateHarmoniseObject(
91                                                this.m_dsi, WebPage.class.getName(),
92                                                nPageId);
93         
94             boolean bIsVisible = AuthorizationValidator.isVisible(usr, page);
95             
96             for(int i=0;i<linkNodes.getLength();i++) {
97                 Element linkNode = (Element) linkNodes.item(i);
98                 if(bIsVisible == false){
99                     linkNode.setAttribute(AuthorizationValidator.ATTRIB_IS_VIEWABLE, "false");
100                 }
101                 
102                     NodeList pageNodes = linkNode.getElementsByTagName(
103                                                  WebPage.TAG_PAGE);
104     
105                     if (pageNodes.getLength() == 0) {
106                         Node pageNode = createElement(WebPage.TAG_PAGE);
107                         ((Element) pageNode).setAttribute(AbstractObject.ATTRIB_ID,
108                                                           Integer.toString(nPageId));
109     
110                         linkNode.appendChild(pageNode);
111                     }
112                 
113             }
114         } catch (Exception JavaDoc e) {
115             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
116         }
117     }
118
119     /** Create an error XML element in the document.
120     *
121     * @param error_code Code of the error to create
122     * @param form that has set the error, if one exists
123     */

124     public Element createErrorElement(String JavaDoc error_code, Element form_element) {
125         NodeList nodes = form_element.getElementsByTagName(
126         XMLFormErrorCodes.TAG_ERRORTEXT);
127         Element error_element = createElement(AbstractObject.TAG_ERROR);
128
129         String JavaDoc error_text = null;
130
131         for (int i = 0; i < nodes.getLength(); i++) {
132             Element form_error_element = (Element) nodes.item(i);
133
134             if (((String JavaDoc) form_error_element.getAttribute(
135             XMLFormErrorCodes.ATTRIB_ERRORCODE)).equals(error_code)) {
136                 Text txt = (Text) form_error_element.getFirstChild();
137                 error_text = txt.getNodeValue();
138             }
139         }
140
141         
142         if (error_text == null)
143         {
144             //wasn't set from form
145
error_text = XMLFormErrorCodes.getInstance()
146                                           .getErrorText(error_code);
147         }
148
149         Node txt;
150
151         if (error_text != null) {
152             txt = createTextNode(error_text);
153         } else {
154             throw new RuntimeException JavaDoc("Error code unknown :" + error_code);
155         }
156
157         error_element.appendChild(txt);
158         error_element.setAttribute(XMLFormErrorCodes.ATTRIB_ERRORCODE, error_code);
159
160         return error_element;
161     }
162 }
Popular Tags