View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.xml;
18  
19  import java.io.FileOutputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.parsers.FactoryConfigurationError;
28  import javax.xml.parsers.ParserConfigurationException;
29  
30  import org.jdom.Document;
31  import org.jdom.Element;
32  import org.jdom.JDOMException;
33  import org.jdom.Namespace;
34  import org.jdom.input.DOMBuilder;
35  import org.jdom.output.XMLOutputter;
36  import org.kuali.rice.kew.help.HelpEntry;
37  import org.kuali.rice.kew.help.service.HelpService;
38  import org.kuali.rice.kew.service.KEWServiceLocator;
39  import org.xml.sax.SAXException;
40  
41  
42  /*
43   * A parser for help entry data.  The underlying xml file format is:
44   * 
45   * <pre>
46   *	    <helpEntries>  
47   *	      <helpEntry>
48   *     	    <helpName>name 1</helpName>
49   * 			<helpText>text 1</helpText>
50   * 		    <helpKey>key 1</helpKey>
51   *    	  </helpEntry>
52   * 
53   *   	  <helpEntry>
54   *    		<helpName>name 2</helpName>
55   * 			<helpText>text 2</helpText>
56   *			<helpKey>key 2</helpKey>
57   * 	  	  </helpEntry>
58   *	   </helpEntries>
59   * </pre>
60   * 
61   * <p>The xml file can contain html tags as long as these tags are wrapped between "&lt;![CDATA[" and "]]&rt;"
62   * 
63   * @author Kuali Rice Team (rice.collab@kuali.org)
64   */
65  public class HelpEntryXmlParser {
66      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HelpEntryXmlParser.class);
67  
68      // Namespace for JDom
69      private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/Help");
70      private static final Namespace NAMESPACE1 = Namespace.getNamespace("xsi", "ns:workflow");
71      private static final String DATA_ELEMENT="data";
72      private static final String HELP_ENTRIES_ELEMENT = "helpEntries";
73      private static final String HELP_ENTRY_ELEMENT = "helpEntry";
74      private static final String HELP_NAME_ELEMENT = "helpName";
75      private static final String HELP_KEY_ELEMENT = "helpKey";
76      private static final String HELP_TEXT_ELEMENT = "helpText";
77      
78      public List parseHelpEntries(InputStream file) throws JDOMException, SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {
79          List helpEntries = new ArrayList();
80          LOG.debug("Enter parseHelpEntries(..)");
81          org.w3c.dom.Document w3cDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
82          Document document = new DOMBuilder().build(w3cDocument);
83          Element root = document.getRootElement();
84          LOG.debug("Begin parsing(..)");
85          LOG.debug(root.getName());
86          
87         for (Iterator helpEntriesIt = root.getChildren(HELP_ENTRIES_ELEMENT, NAMESPACE).iterator(); helpEntriesIt.hasNext();) {
88              Element helpEntriesE = (Element) helpEntriesIt.next();
89              for (Iterator iterator = helpEntriesE.getChildren(HELP_ENTRY_ELEMENT, NAMESPACE).iterator(); iterator.hasNext();) {
90                  Element helpEntryElement = (Element) iterator.next();
91                  HelpEntry helpEntry = new HelpEntry();
92                  LOG.debug(helpEntryElement.getChildText(HELP_NAME_ELEMENT, NAMESPACE));
93                  helpEntry.setHelpName(helpEntryElement.getChildTextTrim(HELP_NAME_ELEMENT, NAMESPACE));
94                  LOG.debug(helpEntryElement.getChildText(HELP_TEXT_ELEMENT, NAMESPACE));
95                  String text=helpEntryElement.getChildTextTrim(HELP_TEXT_ELEMENT,NAMESPACE);
96                  int start=text.indexOf("<![CDATA[");
97                  int end=text.indexOf("]]>");
98                  if (start!=-1 && end!=-1){
99                  	start+=9;
100                 	text=text.substring(start,end);
101                 }
102                 helpEntry.setHelpText(text);                
103                 LOG.debug(helpEntryElement.getChildText(HELP_KEY_ELEMENT, NAMESPACE));
104                 helpEntry.setHelpKey(helpEntryElement.getChildTextTrim(HELP_KEY_ELEMENT, NAMESPACE));
105                         
106                 try {
107                     LOG.debug("Saving help entry " + helpEntry.getHelpName());
108                     getHelpService().saveXmlEntry(helpEntry);    
109                 } catch (Exception e) {
110                     LOG.error("error saving help entry", e); 
111                     LOG.debug(helpEntry.getHelpKey()); 
112                 }
113                 
114                 helpEntries.add(helpEntry);
115             }
116        }
117         LOG.debug("Exit parseHelpEntries(..)"); 
118         return helpEntries;
119     }
120     
121     public FileOutputStream getEntriesToXml(List list) throws JDOMException, SAXException, IOException, ParserConfigurationException, FactoryConfigurationError{
122     	FileOutputStream out=null;
123     	Element root=new Element(DATA_ELEMENT,NAMESPACE1);
124     	Element entries=new Element(HELP_ENTRIES_ELEMENT,NAMESPACE);
125     	Iterator iEntry=list.iterator();
126     	while(iEntry.hasNext()){
127     		HelpEntry entry=(HelpEntry)iEntry.next();
128     		Element helpentry=new Element(HELP_ENTRY_ELEMENT,NAMESPACE);
129     	    Element helpname=new Element(HELP_NAME_ELEMENT,NAMESPACE);
130     	    helpname.setText(entry.getHelpName());
131     	    Element helptext=new Element(HELP_TEXT_ELEMENT,NAMESPACE);
132     	    helptext.setText("<![CDATA["+entry.getHelpText()+"]]>");
133     	    Element helpkey=new Element(HELP_KEY_ELEMENT,NAMESPACE);
134     	    helpkey.setText(entry.getHelpKey());
135     	    helpentry.addContent(helpname);
136     	    helpentry.addContent(helptext);
137     	    helpentry.addContent(helpkey);
138     	    entries.addContent(helpentry);
139     	}
140     	root.addContent(entries);
141     	Document doc = new Document(root);
142     	out = new FileOutputStream("text.xml");
143         XMLOutputter serializer = new XMLOutputter();
144         serializer.output(doc, out);
145         out.flush();
146         return out;
147     }
148     
149     private HelpService getHelpService(){
150         return  (HelpService) KEWServiceLocator.getService(KEWServiceLocator.HELP_SERVICE);
151     }   
152     
153 }