001 /* 002 * Copyright 2005-2007 The Kuali Foundation 003 * 004 * 005 * Licensed under the Educational Community License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.opensource.org/licenses/ecl2.php 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.kuali.rice.kew.xml; 018 019 import java.io.FileOutputStream; 020 import java.io.IOException; 021 import java.io.InputStream; 022 import java.util.ArrayList; 023 import java.util.Iterator; 024 import java.util.List; 025 026 import javax.xml.parsers.DocumentBuilderFactory; 027 import javax.xml.parsers.FactoryConfigurationError; 028 import javax.xml.parsers.ParserConfigurationException; 029 030 import org.jdom.Document; 031 import org.jdom.Element; 032 import org.jdom.JDOMException; 033 import org.jdom.Namespace; 034 import org.jdom.input.DOMBuilder; 035 import org.jdom.output.XMLOutputter; 036 import org.kuali.rice.kew.help.HelpEntry; 037 import org.kuali.rice.kew.help.service.HelpService; 038 import org.kuali.rice.kew.service.KEWServiceLocator; 039 import org.xml.sax.SAXException; 040 041 042 /* 043 * A parser for help entry data. The underlying xml file format is: 044 * 045 * <pre> 046 * <helpEntries> 047 * <helpEntry> 048 * <helpName>name 1</helpName> 049 * <helpText>text 1</helpText> 050 * <helpKey>key 1</helpKey> 051 * </helpEntry> 052 * 053 * <helpEntry> 054 * <helpName>name 2</helpName> 055 * <helpText>text 2</helpText> 056 * <helpKey>key 2</helpKey> 057 * </helpEntry> 058 * </helpEntries> 059 * </pre> 060 * 061 * <p>The xml file can contain html tags as long as these tags are wrapped between "<![CDATA[" and "]]&rt;" 062 * 063 * @author Kuali Rice Team (rice.collab@kuali.org) 064 */ 065 public class HelpEntryXmlParser { 066 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(HelpEntryXmlParser.class); 067 068 // Namespace for JDom 069 private static final Namespace NAMESPACE = Namespace.getNamespace("", "ns:workflow/Help"); 070 private static final Namespace NAMESPACE1 = Namespace.getNamespace("xsi", "ns:workflow"); 071 private static final String DATA_ELEMENT="data"; 072 private static final String HELP_ENTRIES_ELEMENT = "helpEntries"; 073 private static final String HELP_ENTRY_ELEMENT = "helpEntry"; 074 private static final String HELP_NAME_ELEMENT = "helpName"; 075 private static final String HELP_KEY_ELEMENT = "helpKey"; 076 private static final String HELP_TEXT_ELEMENT = "helpText"; 077 078 public List parseHelpEntries(InputStream file) throws JDOMException, SAXException, IOException, ParserConfigurationException, FactoryConfigurationError { 079 List helpEntries = new ArrayList(); 080 LOG.debug("Enter parseHelpEntries(..)"); 081 org.w3c.dom.Document w3cDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file); 082 Document document = new DOMBuilder().build(w3cDocument); 083 Element root = document.getRootElement(); 084 LOG.debug("Begin parsing(..)"); 085 LOG.debug(root.getName()); 086 087 for (Iterator helpEntriesIt = root.getChildren(HELP_ENTRIES_ELEMENT, NAMESPACE).iterator(); helpEntriesIt.hasNext();) { 088 Element helpEntriesE = (Element) helpEntriesIt.next(); 089 for (Iterator iterator = helpEntriesE.getChildren(HELP_ENTRY_ELEMENT, NAMESPACE).iterator(); iterator.hasNext();) { 090 Element helpEntryElement = (Element) iterator.next(); 091 HelpEntry helpEntry = new HelpEntry(); 092 LOG.debug(helpEntryElement.getChildText(HELP_NAME_ELEMENT, NAMESPACE)); 093 helpEntry.setHelpName(helpEntryElement.getChildTextTrim(HELP_NAME_ELEMENT, NAMESPACE)); 094 LOG.debug(helpEntryElement.getChildText(HELP_TEXT_ELEMENT, NAMESPACE)); 095 String text=helpEntryElement.getChildTextTrim(HELP_TEXT_ELEMENT,NAMESPACE); 096 int start=text.indexOf("<![CDATA["); 097 int end=text.indexOf("]]>"); 098 if (start!=-1 && end!=-1){ 099 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 }