001: //$HeadURL$
002: /*---------------- FILE HEADER ------------------------------------------
003: This file is part of deegree.
004: Copyright (C) 2001-2008 by:
005: Department of Geography, University of Bonn
006: http://www.giub.uni-bonn.de/deegree/
007: lat/lon GmbH
008: http://www.lat-lon.de
009:
010: This library is free software; you can redistribute it and/or
011: modify it under the terms of the GNU Lesser General Public
012: License as published by the Free Software Foundation; either
013: version 2.1 of the License, or (at your option) any later version.
014: This library is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: Contact:
022:
023: Andreas Poth
024: lat/lon GmbH
025: Aennchenstr. 19
026: 53177 Bonn
027: Germany
028: E-Mail: poth@lat-lon.de
029:
030: Prof. Dr. Klaus Greve
031: Department of Geography
032: University of Bonn
033: Meckenheimer Allee 166
034: 53115 Bonn
035: Germany
036: E-Mail: greve@giub.uni-bonn.de
037: ---------------------------------------------------------------------------*/
038:
039: package org.deegree.model.filterencoding;
040:
041: import java.net.URI;
042: import java.net.URL;
043: import java.util.List;
044:
045: import org.deegree.framework.log.ILogger;
046: import org.deegree.framework.log.LoggerFactory;
047: import org.deegree.framework.util.StringTools;
048: import org.deegree.framework.xml.NamespaceContext;
049: import org.deegree.framework.xml.XMLFragment;
050: import org.deegree.framework.xml.XMLParsingException;
051: import org.deegree.framework.xml.XMLTools;
052: import org.deegree.model.feature.Feature;
053: import org.deegree.ogcbase.CommonNamespaces;
054: import org.w3c.dom.Node;
055:
056: /**
057: *
058: *
059: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
060: * @author last edited by: $Author: poth $
061: *
062: * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
063: */
064: public class FunctionNormalize extends Function {
065:
066: private static final ILogger LOG = LoggerFactory
067: .getLogger(FunctionNormalize.class);
068:
069: private static XMLFragment rules;
070:
071: private static NamespaceContext nsContext;
072:
073: private static void initialize() {
074: if (rules == null) {
075: URL url = FunctionNormalize.class
076: .getResource("normalization_rules.xml");
077: try {
078: rules = new XMLFragment(url);
079: nsContext = CommonNamespaces.getNamespaceContext();
080: nsContext.addNamespace("dgfunct", new URI(
081: "http://www.deegree.org/filter/function"));
082: } catch (Exception e) {
083: LOG.logError(e.getMessage(), e);
084: }
085:
086: }
087: }
088:
089: /**
090: * default constructor initializing rules if not already done
091: *
092: */
093: FunctionNormalize() {
094: initialize();
095: }
096:
097: /*
098: * (non-Javadoc)
099: *
100: * @see org.deegree.model.filterencoding.Function#evaluate(org.deegree.model.feature.Feature)
101: */
102: @Override
103: public Object evaluate(Feature feature)
104: throws FilterEvaluationException {
105: Literal literal = (Literal) args.get(0);
106: String s = literal.getValue().toUpperCase();
107: try {
108: List<Node> nodes = XMLTools
109: .getNodes(rules.getRootElement(), "//dgfunct:Rule",
110: nsContext);
111: for (Node node : nodes) {
112: String regex = XMLTools.getRequiredNodeAsString(node,
113: "dgfunct:RegExpr", nsContext);
114: String replacement = XMLTools.getRequiredNodeAsString(
115: node, "dgfunct:Replacement", nsContext);
116: if (LOG.getLevel() == ILogger.LOG_DEBUG) {
117: LOG.logDebug(StringTools.concat(500,
118: "Rule: Match '", "' replace with '", "'"));
119: }
120: s = s.replaceAll(regex, replacement);
121: LOG.logDebug("Result: ", s);
122: }
123: } catch (XMLParsingException e) {
124: LOG.logError(e.getMessage(), e);
125: throw new FilterEvaluationException(e.getMessage());
126: }
127:
128: return s;
129: }
130:
131: }
|