View Javadoc

1   /**
2    * Copyright 2005 Steve Molloy
3    * 
4    * This file is part of OV4J.
5    * 
6    * OV4J is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
7    * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
8    * 
9    * OV4J is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10   * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11   * 
12   * You should have received a copy of the GNU General Public License along with OV4J; if not, write to the Free Software
13   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
14   * 
15   */
16  package org.ov4j.comp;
17  
18  import java.net.URI;
19  import java.net.URISyntaxException;
20  import java.net.UnknownHostException;
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  import org.ov4j.Config;
25  
26  /**
27   * This class is used to compare URIs.
28   * 
29   * @author smolloy
30   * 
31   */
32  public class URIComparisonResult extends ComparisonResult<URI> {
33  	/**
34  	 * Logger for this class
35  	 */
36  	private static final Logger	logger	= Logger.getLogger(URIComparisonResult.class.getName());
37  
38  	/**
39  	 * Normalize the URI so it can be sorted and compared. This will try to add missing ports according to protocol and
40  	 * replace hostname with IP address.
41  	 * 
42  	 * @param uri
43  	 *            URI to normalize.
44  	 * @return Normalized URI.
45  	 * @throws UnknownHostException
46  	 * @throws URISyntaxException
47  	 */
48  	public static URI normalizeURI(final URI uri) throws UnknownHostException, URISyntaxException {
49  		if (URIComparisonResult.logger.isLoggable(Level.FINEST)) {
50  			URIComparisonResult.logger.entering("URIComparisonResult", "normalizeURI(URI=" + uri + ")", "start");
51  		}
52  
53  		int port = uri.getPort();
54  		if (port < 1 && uri.getScheme() != null) {
55  			final String defaultPort = Config.getString("OV4J.ports." + uri.getScheme());
56  			if (defaultPort != null) {
57  				port = Integer.parseInt(defaultPort);
58  			}
59  		}
60  		final URI res =
61  			new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), port, uri.getPath(), uri.getQuery(), uri
62  				.getFragment());
63  
64  		if (URIComparisonResult.logger.isLoggable(Level.FINEST)) {
65  			URIComparisonResult.logger.exiting("URIComparisonResult", "normalizeURI(URI=" + uri + ")",
66  				"end - return value=" + res);
67  		}
68  		return res;
69  	}
70  
71  	/**
72  	 * @see org.ov4j.comp.ComparisonResult#compute()
73  	 */
74  	@Override
75  	public void compute() {
76  		if (URIComparisonResult.logger.isLoggable(Level.FINER)) {
77  			URIComparisonResult.logger.entering("URIComparisonResult", "compute()", "start");
78  		}
79  
80  		if (getOriginal() != null && getChanged() != null) {
81  			URI u1 = null;
82  			URI u2 = null;
83  			try {
84  				u1 = URIComparisonResult.normalizeURI(getOriginal());
85  				u2 = URIComparisonResult.normalizeURI(getChanged());
86  			} catch (final Exception e) {
87  				if (URIComparisonResult.logger.isLoggable(Level.FINE)) {
88  					URIComparisonResult.logger.logp(Level.FINE, "URIComparisonResult", "compute()", "Exception caught",
89  						e);
90  				}
91  
92  				u1 = getOriginal();
93  				u2 = getChanged();
94  			}
95  			final SentenceComparisonResult hostComp = new SentenceComparisonResult();
96  			hostComp.setOriginal(u1.getHost());
97  			hostComp.setChanged(u2.getHost());
98  			hostComp.compute();
99  			final SentenceComparisonResult pathComp = new SentenceComparisonResult();
100 			pathComp.setOriginal(u1.getPath());
101 			pathComp.setChanged(u2.getPath());
102 			pathComp.compute();
103 			final StringComparisonResult schemeComp = new StringComparisonResult();
104 			schemeComp.setOriginal(u1.getScheme());
105 			schemeComp.setChanged(u2.getScheme());
106 			schemeComp.compute();
107 			final NumericComparisonResult<Integer> portComp = new NumericComparisonResult<Integer>();
108 			portComp.setOriginal(Integer.valueOf(u1.getPort()));
109 			portComp.setChanged(Integer.valueOf(u2.getPort()));
110 			portComp.compute();
111 			setPrecision(0.4 * hostComp.getPrecision() + 0.4 * pathComp.getPrecision() + 0.1
112 				* schemeComp.getPrecision() + 0.1 * portComp.getPrecision());
113 			setRecall(0.4 * hostComp.getRecall() + 0.4 * pathComp.getRecall() + 0.1 * schemeComp.getRecall() + 0.1
114 				* portComp.getRecall());
115 		}
116 
117 		if (URIComparisonResult.logger.isLoggable(Level.FINER)) {
118 			URIComparisonResult.logger.exiting("URIComparisonResult", "compute()", "end");
119 		}
120 	}
121 
122 	/**
123 	 * @see org.ov4j.comp.ComparisonResult#fastCompute()
124 	 */
125 	@Override
126 	public void fastCompute() {
127 		if (URIComparisonResult.logger.isLoggable(Level.FINER)) {
128 			URIComparisonResult.logger.entering("URIComparisonResult", "fastCompute()", "start");
129 		}
130 
131 		if (getOriginal() != null && getChanged() != null && getOriginal().equals(getChanged())) {
132 			setPrecision(1.0);
133 		}
134 
135 		if (URIComparisonResult.logger.isLoggable(Level.FINER)) {
136 			URIComparisonResult.logger.exiting("URIComparisonResult", "fastCompute()", "end");
137 		}
138 	}
139 
140 }