Author: norman
Date: Sun Jun 5 18:13:34 2011
New Revision: 1132468
URL: http://svn.apache.org/viewvc?rev=1132468&view=rev
Log:
Add BaseSubjectComparotor for Subject sort. See MAILBOX-78
Added:
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SearchUtil.java
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java
Modified:
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
Added:
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SearchUtil.java
URL:
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SearchUtil.java?rev=1132468&view=auto
==============================================================================
---
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SearchUtil.java
(added)
+++
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/SearchUtil.java
Sun Jun 5 18:13:34 2011
@@ -0,0 +1,74 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.mailbox.store.search;
+
+import java.io.UnsupportedEncodingException;
+
+import org.apache.james.mime4j.codec.DecoderUtil;
+import org.apache.james.mime4j.util.MimeUtil;
+
+public class SearchUtil {
+
+ private final static String FWD_PARENS = "(fwd)";
+
+ /**
+ * Extract the base subject from the given subject.
+ *
+ * See rfc5256 2.1 Base Subject
+ *
+ * TODO: FIX ME
+ *
+ * @param subject
+ * @return baseSubject
+ */
+ public static String getBaseSubject(String subject) {
+ try {
+ String decodedSubject = new
String(MimeUtil.unfold(DecoderUtil.decodeEncodedWords(subject)).getBytes("UTF-8"),
"UTF-8");
+ // replace all tabs with spaces and replace multiple spaces with
one space
+ decodedSubject = decodedSubject.replaceAll("\t", "
").replaceAll("( ){2,}", " ");
+
+ while (true) {
+ boolean changed = false;
+ if (decodedSubject.startsWith(FWD_PARENS)) {
+ decodedSubject =
decodedSubject.substring(FWD_PARENS.length(), decodedSubject.length());
+ changed = true;
+ }
+ if (decodedSubject.startsWith(" ")) {
+ // remove all leading spaces
+ decodedSubject = decodedSubject.replaceAll("^( )+", "");
+ changed = true;
+ }
+ int length = decodedSubject.length();
+ if (!changed) {
+ changed = length != decodedSubject.length();
+ }
+
+ if(!changed) {
+ break;
+ }
+ }
+ return decodedSubject;
+ } catch (UnsupportedEncodingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+}
Added:
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java
URL:
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java?rev=1132468&view=auto
==============================================================================
---
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java
(added)
+++
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/BaseSubjectComparator.java
Sun Jun 5 18:13:34 2011
@@ -0,0 +1,53 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one *
+ * or more contributor license agreements. See the NOTICE file *
+ * distributed with this work for additional information *
+ * regarding copyright ownership. The ASF licenses this file *
+ * to you under the Apache License, Version 2.0 (the *
+ * "License"); you may not use this file except in compliance *
+ * with the License. You may obtain a copy of the License at *
+ * *
+ * http://www.apache.org/licenses/LICENSE-2.0 *
+ * *
+ * Unless required by applicable law or agreed to in writing, *
+ * software distributed under the License is distributed on an *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
+ * KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations *
+ * under the License. *
+ ****************************************************************/
+package org.apache.james.mailbox.store.search.comparator;
+
+import java.util.Comparator;
+
+import org.apache.james.mailbox.store.mail.model.Message;
+import org.apache.james.mailbox.store.search.SearchUtil;
+
+public class BaseSubjectComparator extends AbstractHeaderComparator{
+
+
+
+ private final static Comparator<Message<?>> BASESUBJECT = new
BaseSubjectComparator();;
+ private final static Comparator<Message<?>> REVERSE_BASESUBJECT = new
ReverseComparator(BASESUBJECT);
+
+
+
+ private final static String SUBJECT = "subject";
+
+ @Override
+ public int compare(Message<?> o1, Message<?> o2) {
+ String baseSubject1 =
SearchUtil.getBaseSubject(getHeaderValue(SUBJECT, o1));
+ String baseSubject2 =
SearchUtil.getBaseSubject(getHeaderValue(SUBJECT, o2));
+
+ return baseSubject1.compareToIgnoreCase(baseSubject2);
+ }
+
+
+ public static Comparator<Message<?>> baseSubject(boolean reverse){
+ if (reverse) {
+ return REVERSE_BASESUBJECT;
+ } else {
+ return BASESUBJECT;
+ }
+ }
+}
Modified:
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
URL:
http://svn.apache.org/viewvc/james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java?rev=1132468&r1=1132467&r2=1132468&view=diff
==============================================================================
---
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
(original)
+++
james/mailbox/trunk/store/src/main/java/org/apache/james/mailbox/store/search/comparator/CombinedComparator.java
Sun Jun 5 18:13:34 2011
@@ -73,7 +73,7 @@ public class CombinedComparator implemen
comparator = SizeComparator.size(reverse);
break;
case Subject:
- // TODO: fix me
+ comparator = BaseSubjectComparator.baseSubject(reverse);
break;
case To:
comparator = HeaderMailboxComparator.to(reverse);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]