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.applicationconstants.xml;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.FactoryConfigurationError;
027: import javax.xml.parsers.ParserConfigurationException;
028:
029: import org.apache.log4j.Logger;
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.xml.sax.SAXException;
036:
037: import edu.iu.uis.eden.KEWServiceLocator;
038: import edu.iu.uis.eden.applicationconstants.ApplicationConstant;
039:
040: /*
041: * A parser for application constant data. Schema is ApplicationConstants.xsd
042: *
043: * @author ewestfal
044: * @author ahamid
045: */
046: public class ApplicationConstantsXmlParser {
047:
048: private static final Logger LOG = Logger
049: .getLogger(ApplicationConstantsXmlParser.class);
050:
051: private static final Namespace NAMESPACE = Namespace.getNamespace(
052: "", "ns:workflow/ApplicationConstants");
053: private static final String APP_CONSTANTS_ELEMENT = "ApplicationConstants";
054: private static final String APP_CONSTANT_ELEMENT = "ApplicationConstant";
055: private static final String CONSTANT_NAME_ELEMENT = "ConstantName";
056: private static final String CONSTANT_VALUE_ELEMENT = "ConstantValue";
057:
058: public List parseAppConstEntries(InputStream file)
059: throws JDOMException, SAXException, IOException,
060: ParserConfigurationException, FactoryConfigurationError {
061: List constEntries = new ArrayList();
062:
063: org.w3c.dom.Document w3cDocument = DocumentBuilderFactory
064: .newInstance().newDocumentBuilder().parse(file);
065: Document document = new DOMBuilder().build(w3cDocument);
066: Element root = document.getRootElement();
067:
068: for (Iterator constantsIt = root.getChildren(
069: APP_CONSTANTS_ELEMENT, NAMESPACE).iterator(); constantsIt
070: .hasNext();) {
071: Element constantsElement = (Element) constantsIt.next();
072: for (Iterator iterator = constantsElement.getChildren(
073: APP_CONSTANT_ELEMENT, NAMESPACE).iterator(); iterator
074: .hasNext();) {
075: Element constElement = (Element) iterator.next();
076:
077: String name = constElement.getChildTextTrim(
078: CONSTANT_NAME_ELEMENT, NAMESPACE);
079: String value = constElement.getChildTextTrim(
080: CONSTANT_VALUE_ELEMENT, NAMESPACE);
081:
082: LOG.info("Processing constant: " + name);
083:
084: ApplicationConstant constant = null;
085: ;
086: try {
087: LOG.debug("Looking up Application Constant: "
088: + name);
089: constant = KEWServiceLocator
090: .getApplicationConstantsService()
091: .findByName(name);
092: if (constant != null) {
093: LOG
094: .debug("Found existing Application Constant: "
095: + name);
096: constant.setApplicationConstantValue(value);
097: } else {
098: constant = new ApplicationConstant();
099: constant.setApplicationConstantName(name);
100: constant.setApplicationConstantValue(value);
101: }
102: LOG.debug("Saving Application Constant: "
103: + constant.getApplicationConstantName());
104: // TODO we're not flusing the cache here to get around problem with testing, this
105: // is typically only used in a test situation but we need to fix this!
106: KEWServiceLocator.getApplicationConstantsService()
107: .save(constant);
108: } catch (Exception e) {
109: LOG.error("error saving Application Constant", e);
110: }
111:
112: // NOTE: should we move this into the try/catch block, or do we want to preserve a constant
113: // in memory even when it has not been persisted successfully?
114: constEntries.add(constant);
115: }
116: }
117:
118: // ((ApplicationConstantsCache) SpringServiceLocator.getCache().getCachedObjectById(ApplicationConstantsCache.APPLICATION_CONSTANTS_CACHE_ID)).reload();
119: return constEntries;
120: }
121: }
|