001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.search.filters;
019:
020: import java.io.IOException;
021: import java.io.StringWriter;
022: import java.util.Stack;
023:
024: import de.finix.contelligent.CallData;
025: import de.finix.contelligent.Component;
026: import de.finix.contelligent.ComponentCreationException;
027: import de.finix.contelligent.ComponentNotFoundException;
028: import de.finix.contelligent.ComponentPath;
029: import de.finix.contelligent.exception.ContelligentException;
030: import de.finix.contelligent.render.MissingParameterException;
031: import de.finix.contelligent.render.Renderable;
032: import de.finix.contelligent.search.engine.CrawlerFilter;
033: import de.finix.contelligent.search.engine.FilterException;
034: import de.finix.contelligent.xml.elements.IndexBuilderFilterElement;
035:
036: public class ParentHasSubcomponentValue implements CrawlerFilter {
037:
038: /**
039: *
040: */
041: public ParentHasSubcomponentValue() {
042: super ();
043: // TODO Auto-generated constructor stub
044: }
045:
046: /*
047: * (non-Javadoc)
048: *
049: * @see de.finix.contelligent.search.engine.CrawlerFilter#filter(de.finix.contelligent.Component,
050: * de.finix.contelligent.xml.elements.IndexBuilderFilterElement,
051: * de.finix.contelligent.CallData)
052: */
053: public boolean filter(ComponentPath componentPath,
054: IndexBuilderFilterElement configuration, CallData callData)
055: throws FilterException {
056:
057: String[] args = configuration.getStringArgs();
058:
059: if (args.length != 4)
060: throw new FilterException(
061: "ParentHasSubcomponentValue Filter needs 4 arguments: [subcomponentName, value, toppath, supertyp]");
062:
063: // the subcomponent Name to use to test a parent
064: String subComponentName = args[0];
065: String value = args[1];
066: String top = args[2];
067: String super Type = args[3];
068: ComponentPath topPath = new ComponentPath(top);
069:
070: Stack stack = new Stack();
071:
072: ComponentPath currentPath = componentPath.parentPath();
073:
074: do {
075: Component current = null;
076: try {
077: current = callData.getActualManager().getComponent(
078: currentPath, callData);
079: } catch (ComponentCreationException e1) {
080: } catch (ComponentNotFoundException e1) {
081: // TODO Auto-generated catch block
082: e1.printStackTrace();
083: }
084: // if it is the right supertype, just resolve and render the
085: // subcomponent resolved by subComponentName
086: if (current.getComponentContext().getType().isSubTypeOf(
087: super Type)) {
088: ComponentPath subPath = currentPath
089: .append(subComponentName);
090:
091: // if there is such a component test it .. otherwise go on!
092: if (callData.getActualManager()
093: .componentExists(subPath)) {
094: Component tmp;
095: try {
096: tmp = callData.getActualManager().getComponent(
097: subPath, callData);
098: } catch (ComponentCreationException e) {
099: throw new FilterException(
100: "ERROR: Component exists but cannot be created - ",
101: e);
102: } catch (ComponentNotFoundException e) {
103: throw new FilterException(
104: "ERROR: Component exists but cannot be resolved - ",
105: e);
106: }
107: if (tmp instanceof Renderable) {
108: StringWriter writer = new StringWriter();
109: try {
110: ((Renderable) tmp).getRenderer().render(
111: writer, callData.getParameters(),
112: callData);
113: } catch (MissingParameterException e) {
114: throw new FilterException(
115: "ERROR: Component needs unknown parameters - ",
116: e);
117: } catch (IOException e) {
118: throw new FilterException(
119: "ERROR: IOException while rendering component - ",
120: e);
121: } catch (ContelligentException e) {
122: throw new FilterException(
123: "ERROR: Internal Error during filtering - ",
124: e);
125: }
126:
127: if (writer.toString().equals(value)) {
128: return true;
129: }
130: } else {
131: throw new FilterException(
132: "ERROR: Filter matches component '"
133: + subPath
134: + "' that is not renderable. Stopping here!");
135: }
136: }
137: }
138:
139: // set the new currentPath and go on if this is still a subPathOf
140: // the specified topPath
141: currentPath = currentPath.parentPath();
142: } while (currentPath.isSubPathOf(topPath));
143:
144: return false;
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see de.finix.contelligent.search.engine.CrawlerFilter#supportsNestedFilters()
151: */
152: public boolean supportsNestedFilters() {
153: return false;
154: }
155:
156: }
|