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.docsearch.xml;
018:
019: import java.io.BufferedReader;
020: import java.io.StringReader;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import javax.xml.xpath.XPath;
028: import javax.xml.xpath.XPathConstants;
029: import javax.xml.xpath.XPathExpressionException;
030:
031: import org.w3c.dom.Element;
032: import org.w3c.dom.NamedNodeMap;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.NodeList;
035: import org.xml.sax.InputSource;
036:
037: import edu.iu.uis.eden.docsearch.StandardDocumentSearchResultProcessor;
038: import edu.iu.uis.eden.lookupable.Column;
039: import edu.iu.uis.eden.routetemplate.RuleAttribute;
040: import edu.iu.uis.eden.routetemplate.xmlrouting.XPathHelper;
041:
042: /**
043: *
044: * @author delyea
045: */
046: public class DocumentSearchXMLResultProcessorImpl extends
047: StandardDocumentSearchResultProcessor implements
048: DocumentSearchXMLResultProcessor {
049: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
050: .getLogger(DocumentSearchXMLResultProcessorImpl.class);
051:
052: private RuleAttribute ruleAttribute;
053: private List<Column> customDisplayColumns = new ArrayList<Column>();
054:
055: public void setRuleAttribute(RuleAttribute ruleAttribute) {
056: this .ruleAttribute = ruleAttribute;
057: }
058:
059: @Override
060: public List<Column> getCustomDisplayColumns() {
061: List<Column> displayColumns = new ArrayList<Column>();
062: if (customDisplayColumns.isEmpty()) {
063: XPath xpath = XPathHelper.newXPath();
064: String xPathExpression = "//searchResultConfig/column";
065: try {
066: NodeList nodes = (NodeList) xpath.evaluate(
067: xPathExpression, getConfigXML(),
068: XPathConstants.NODESET);
069: if (nodes == null) {
070: LOG
071: .error("Could not find searching configuration columns (<searchResultConfig><column>) for this DocumentSearchXMLResultProcessor");
072: } else {
073: for (int i = 0; i < nodes.getLength(); i++) {
074: Node field = nodes.item(i);
075: NamedNodeMap fieldAttributes = field
076: .getAttributes();
077: String key = (fieldAttributes
078: .getNamedItem("name") != null) ? fieldAttributes
079: .getNamedItem("name").getNodeValue()
080: .trim()
081: : null;
082: String title = (fieldAttributes
083: .getNamedItem("title") != null) ? fieldAttributes
084: .getNamedItem("title").getNodeValue()
085: .trim()
086: : null;
087: String sortable = (fieldAttributes
088: .getNamedItem("sortable") != null) ? fieldAttributes
089: .getNamedItem("sortable")
090: .getNodeValue().trim()
091: : null;
092: Column currentColumn = new Column(title,
093: sortable, "", "", key,
094: new HashMap<String, String>());
095: displayColumns.add(currentColumn);
096: }
097: customDisplayColumns = displayColumns;
098: }
099: } catch (XPathExpressionException e) {
100: LOG.error("error in getCustomDisplayColumns ", e);
101: throw new RuntimeException(
102: "Error trying to find xml content with xpath expression: "
103: + xPathExpression, e);
104: } catch (Exception e) {
105: LOG
106: .error(
107: "error in getCustomDisplayColumns attempting to find xml custon columns",
108: e);
109: throw new RuntimeException(
110: "Error trying to get xml custom columns.", e);
111: }
112: }
113: return customDisplayColumns;
114: }
115:
116: @Override
117: public boolean getShowAllStandardFields() {
118: boolean returnValue = DEFAULT_SHOW_ALL_STANDARD_FIELDS_VALUE;
119: XPath xpath = XPathHelper.newXPath();
120: String findXpathExpressionPrefix = "//searchResultConfig";
121: Node searchResultConfig;
122: try {
123: searchResultConfig = (Node) xpath.evaluate(
124: findXpathExpressionPrefix, getConfigXML(),
125: XPathConstants.NODE);
126: if (searchResultConfig != null) {
127: NamedNodeMap fieldAttributes = searchResultConfig
128: .getAttributes();
129: if (fieldAttributes
130: .getNamedItem("showStandardSearchFields") != null) {
131: returnValue = Boolean.valueOf(
132: fieldAttributes.getNamedItem(
133: "showStandardSearchFields")
134: .getNodeValue()).booleanValue();
135: }
136: }
137: } catch (XPathExpressionException e) {
138: LOG.error("error in getSearchContent ", e);
139: throw new RuntimeException(
140: "Error trying to find xml content with xpath expression",
141: e);
142: }
143: return returnValue;
144: }
145:
146: @Override
147: public boolean getOverrideSearchableAttributes() {
148: boolean returnValue = DEFAULT_OVERRIDE_SEARCHABLE_ATTRIBUTES_VALUE;
149: XPath xpath = XPathHelper.newXPath();
150: String findXpathExpressionPrefix = "//searchResultConfig";
151: Node searchResultConfig;
152: try {
153: searchResultConfig = (Node) xpath.evaluate(
154: findXpathExpressionPrefix, getConfigXML(),
155: XPathConstants.NODE);
156: if (searchResultConfig != null) {
157: NamedNodeMap fieldAttributes = searchResultConfig
158: .getAttributes();
159: if (fieldAttributes
160: .getNamedItem("overrideSearchableAttributes") != null) {
161: returnValue = Boolean.valueOf(
162: fieldAttributes.getNamedItem(
163: "overrideSearchableAttributes")
164: .getNodeValue()).booleanValue();
165: }
166: }
167: } catch (XPathExpressionException e) {
168: LOG.error("error in getSearchContent ", e);
169: throw new RuntimeException(
170: "Error trying to find xml content with xpath expression",
171: e);
172: }
173: return returnValue;
174: }
175:
176: public Element getConfigXML() {
177: try {
178: return DocumentBuilderFactory.newInstance()
179: .newDocumentBuilder().parse(
180: new InputSource(new BufferedReader(
181: new StringReader(ruleAttribute
182: .getXmlConfigData()))))
183: .getDocumentElement();
184: } catch (Exception e) {
185: String ruleAttrStr = (ruleAttribute == null ? null
186: : ruleAttribute.getName());
187: LOG.error(
188: "error parsing xml data from search processor attribute: "
189: + ruleAttrStr, e);
190: throw new RuntimeException(
191: "error parsing xml data from search processor attribute: "
192: + ruleAttrStr, e);
193: }
194: }
195: }
|