| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 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 | |
|
| 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 | |
} |