001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.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/ecl1.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 edu.iu.uis.eden.xml.help;
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.xml.sax.SAXException;
037:
038: import edu.iu.uis.eden.KEWServiceLocator;
039: import edu.iu.uis.eden.help.HelpEntry;
040: import edu.iu.uis.eden.help.HelpService;
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 shenl
064: */
065: public class HelpEntryXmlParser {
066: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
067: .getLogger(HelpEntryXmlParser.class);
068:
069: // Namespace for JDom
070: private static final Namespace NAMESPACE = Namespace.getNamespace(
071: "", "ns:workflow/Help");
072: private static final Namespace NAMESPACE1 = Namespace.getNamespace(
073: "xsi", "ns:workflow");
074: private static final String DATA_ELEMENT = "data";
075: private static final String HELP_ENTRIES_ELEMENT = "helpEntries";
076: private static final String HELP_ENTRY_ELEMENT = "helpEntry";
077: private static final String HELP_NAME_ELEMENT = "helpName";
078: private static final String HELP_KEY_ELEMENT = "helpKey";
079: private static final String HELP_TEXT_ELEMENT = "helpText";
080:
081: public List parseHelpEntries(InputStream file)
082: throws JDOMException, SAXException, IOException,
083: ParserConfigurationException, FactoryConfigurationError {
084: List helpEntries = new ArrayList();
085: LOG.debug("Enter parseHelpEntries(..)");
086: org.w3c.dom.Document w3cDocument = DocumentBuilderFactory
087: .newInstance().newDocumentBuilder().parse(file);
088: Document document = new DOMBuilder().build(w3cDocument);
089: Element root = document.getRootElement();
090: LOG.debug("Begin parsing(..)");
091: LOG.debug(root.getName());
092:
093: for (Iterator helpEntriesIt = root.getChildren(
094: HELP_ENTRIES_ELEMENT, NAMESPACE).iterator(); helpEntriesIt
095: .hasNext();) {
096: Element helpEntriesE = (Element) helpEntriesIt.next();
097: for (Iterator iterator = helpEntriesE.getChildren(
098: HELP_ENTRY_ELEMENT, NAMESPACE).iterator(); iterator
099: .hasNext();) {
100: Element helpEntryElement = (Element) iterator.next();
101: HelpEntry helpEntry = new HelpEntry();
102: LOG.debug(helpEntryElement.getChildText(
103: HELP_NAME_ELEMENT, NAMESPACE));
104: helpEntry
105: .setHelpName(helpEntryElement.getChildTextTrim(
106: HELP_NAME_ELEMENT, NAMESPACE));
107: LOG.debug(helpEntryElement.getChildText(
108: HELP_TEXT_ELEMENT, NAMESPACE));
109: String text = helpEntryElement.getChildTextTrim(
110: HELP_TEXT_ELEMENT, NAMESPACE);
111: int start = text.indexOf("<![CDATA[");
112: int end = text.indexOf("]]>");
113: if (start != -1 && end != -1) {
114: start += 9;
115: text = text.substring(start, end);
116: }
117: helpEntry.setHelpText(text);
118: LOG.debug(helpEntryElement.getChildText(
119: HELP_KEY_ELEMENT, NAMESPACE));
120: helpEntry.setHelpKey(helpEntryElement.getChildTextTrim(
121: HELP_KEY_ELEMENT, NAMESPACE));
122:
123: try {
124: LOG.debug("Saving help entry "
125: + helpEntry.getHelpName());
126: getHelpService().saveXmlEntry(helpEntry);
127: } catch (Exception e) {
128: LOG.error("error saving help entry", e);
129: LOG.debug(helpEntry.getHelpKey());
130: }
131:
132: helpEntries.add(helpEntry);
133: }
134: }
135: LOG.debug("Exit parseHelpEntries(..)");
136: return helpEntries;
137: }
138:
139: public FileOutputStream getEntriesToXml(List list)
140: throws JDOMException, SAXException, IOException,
141: ParserConfigurationException, FactoryConfigurationError {
142: FileOutputStream out = null;
143: Element root = new Element(DATA_ELEMENT, NAMESPACE1);
144: Element entries = new Element(HELP_ENTRIES_ELEMENT, NAMESPACE);
145: Iterator iEntry = list.iterator();
146: while (iEntry.hasNext()) {
147: HelpEntry entry = (HelpEntry) iEntry.next();
148: Element helpentry = new Element(HELP_ENTRY_ELEMENT,
149: NAMESPACE);
150: Element helpname = new Element(HELP_NAME_ELEMENT, NAMESPACE);
151: helpname.setText(entry.getHelpName());
152: Element helptext = new Element(HELP_TEXT_ELEMENT, NAMESPACE);
153: helptext.setText("<![CDATA[" + entry.getHelpText() + "]]>");
154: Element helpkey = new Element(HELP_KEY_ELEMENT, NAMESPACE);
155: helpkey.setText(entry.getHelpKey());
156: helpentry.addContent(helpname);
157: helpentry.addContent(helptext);
158: helpentry.addContent(helpkey);
159: entries.addContent(helpentry);
160: }
161: root.addContent(entries);
162: Document doc = new Document(root);
163: out = new FileOutputStream("text.xml");
164: XMLOutputter serializer = new XMLOutputter();
165: serializer.output(doc, out);
166: out.flush();
167: return out;
168: }
169:
170: private HelpService getHelpService() {
171: return (HelpService) KEWServiceLocator
172: .getService(KEWServiceLocator.HELP_SERVICE);
173: }
174:
175: }
|