Coverage Report - org.kuali.rice.kew.xml.HelpEntryXmlParser
 
Classes in this File Line Coverage Branch Coverage Complexity
HelpEntryXmlParser
0%
0/65
0%
0/10
3
 
 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  0
 public class HelpEntryXmlParser {
 66  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HelpEntryXmlParser.class);
 67  
 
 68  
     // Namespace for JDom
 69  0
     private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/Help");
 70  0
     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  0
         List helpEntries = new ArrayList();
 80  0
         LOG.debug("Enter parseHelpEntries(..)");
 81  0
         org.w3c.dom.Document w3cDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
 82  0
         Document document = new DOMBuilder().build(w3cDocument);
 83  0
         Element root = document.getRootElement();
 84  0
         LOG.debug("Begin parsing(..)");
 85  0
         LOG.debug(root.getName());
 86  
         
 87  0
        for (Iterator helpEntriesIt = root.getChildren(HELP_ENTRIES_ELEMENT, NAMESPACE).iterator(); helpEntriesIt.hasNext();) {
 88  0
             Element helpEntriesE = (Element) helpEntriesIt.next();
 89  0
             for (Iterator iterator = helpEntriesE.getChildren(HELP_ENTRY_ELEMENT, NAMESPACE).iterator(); iterator.hasNext();) {
 90  0
                 Element helpEntryElement = (Element) iterator.next();
 91  0
                 HelpEntry helpEntry = new HelpEntry();
 92  0
                 LOG.debug(helpEntryElement.getChildText(HELP_NAME_ELEMENT, NAMESPACE));
 93  0
                 helpEntry.setHelpName(helpEntryElement.getChildTextTrim(HELP_NAME_ELEMENT, NAMESPACE));
 94  0
                 LOG.debug(helpEntryElement.getChildText(HELP_TEXT_ELEMENT, NAMESPACE));
 95  0
                 String text=helpEntryElement.getChildTextTrim(HELP_TEXT_ELEMENT,NAMESPACE);
 96  0
                 int start=text.indexOf("<![CDATA[");
 97  0
                 int end=text.indexOf("]]>");
 98  0
                 if (start!=-1 && end!=-1){
 99  0
                         start+=9;
 100  0
                         text=text.substring(start,end);
 101  
                 }
 102  0
                 helpEntry.setHelpText(text);                
 103  0
                 LOG.debug(helpEntryElement.getChildText(HELP_KEY_ELEMENT, NAMESPACE));
 104  0
                 helpEntry.setHelpKey(helpEntryElement.getChildTextTrim(HELP_KEY_ELEMENT, NAMESPACE));
 105  
                         
 106  
                 try {
 107  0
                     LOG.debug("Saving help entry " + helpEntry.getHelpName());
 108  0
                     getHelpService().saveXmlEntry(helpEntry);    
 109  0
                 } catch (Exception e) {
 110  0
                     LOG.error("error saving help entry", e); 
 111  0
                     LOG.debug(helpEntry.getHelpKey()); 
 112  0
                 }
 113  
                 
 114  0
                 helpEntries.add(helpEntry);
 115  0
             }
 116  0
        }
 117  0
         LOG.debug("Exit parseHelpEntries(..)"); 
 118  0
         return helpEntries;
 119  
     }
 120  
     
 121  
     public FileOutputStream getEntriesToXml(List list) throws JDOMException, SAXException, IOException, ParserConfigurationException, FactoryConfigurationError{
 122  0
             FileOutputStream out=null;
 123  0
             Element root=new Element(DATA_ELEMENT,NAMESPACE1);
 124  0
             Element entries=new Element(HELP_ENTRIES_ELEMENT,NAMESPACE);
 125  0
             Iterator iEntry=list.iterator();
 126  0
             while(iEntry.hasNext()){
 127  0
                     HelpEntry entry=(HelpEntry)iEntry.next();
 128  0
                     Element helpentry=new Element(HELP_ENTRY_ELEMENT,NAMESPACE);
 129  0
                 Element helpname=new Element(HELP_NAME_ELEMENT,NAMESPACE);
 130  0
                 helpname.setText(entry.getHelpName());
 131  0
                 Element helptext=new Element(HELP_TEXT_ELEMENT,NAMESPACE);
 132  0
                 helptext.setText("<![CDATA["+entry.getHelpText()+"]]>");
 133  0
                 Element helpkey=new Element(HELP_KEY_ELEMENT,NAMESPACE);
 134  0
                 helpkey.setText(entry.getHelpKey());
 135  0
                 helpentry.addContent(helpname);
 136  0
                 helpentry.addContent(helptext);
 137  0
                 helpentry.addContent(helpkey);
 138  0
                 entries.addContent(helpentry);
 139  0
             }
 140  0
             root.addContent(entries);
 141  0
             Document doc = new Document(root);
 142  0
             out = new FileOutputStream("text.xml");
 143  0
         XMLOutputter serializer = new XMLOutputter();
 144  0
         serializer.output(doc, out);
 145  0
         out.flush();
 146  0
         return out;
 147  
     }
 148  
     
 149  
     private HelpService getHelpService(){
 150  0
         return  (HelpService) KEWServiceLocator.getService(KEWServiceLocator.HELP_SERVICE);
 151  
     }   
 152  
     
 153  
 }