001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.javadoc.search;
043:
044: import java.io.BufferedReader;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.InputStreamReader;
048: import org.openide.util.Exceptions;
049: import org.openide.filesystems.FileObject;
050:
051: /* Base class providing search for JDK1.2/1.3 documentation
052: * Jdk12SearchType.java
053: *
054: * @author Petr Hrebejk, Petr Suchomel
055: */
056: public final class Jdk12SearchType_japan extends Jdk12SearchType {
057:
058: private String japanEncoding;
059:
060: /** generated Serialized Version UID */
061: private static final long serialVersionUID = -2453877778724454324L;
062:
063: private static final String JDK12_ALLCLASSES_JA = "\u3059\u3079\u3066\u306e\u30af\u30e9\u30b9"; // NOI18N
064:
065: /** Getter for property encoding.
066: * @return Value of property encoding.
067: */
068: public java.lang.String getJapanEncoding() {
069: return (japanEncoding != null) ? japanEncoding
070: : "JISAutoDetect"; //NOI18N
071: }
072:
073: /** Setter for property encoding.
074: * @param encoding New value of property encoding.
075: */
076: public void setJapanEncoding(java.lang.String japanEncoding) {
077: String old = this .japanEncoding;
078: this .japanEncoding = japanEncoding;
079: // firePropertyChange("japanEncoding", old, japanEncoding); //NOI18N
080: }
081:
082: /** Returns Java doc search thread for doument
083: * @param toFind String to find
084: * @param fo File object containing index-files
085: * @param diiConsumer consumer for parse events
086: * @return IndexSearchThread
087: * @see IndexSearchThread
088: */
089: @Override
090: public IndexSearchThread getSearchThread(String toFind,
091: FileObject fo,
092: IndexSearchThread.DocIndexItemConsumer diiConsumer) {
093: //here you can send one more parameter .. getJapanEncoding
094: return new SearchThreadJdk12_japan(toFind, fo, diiConsumer,
095: isCaseSensitive(), getJapanEncoding());
096: }
097:
098: @Override
099: public boolean accepts(org.openide.filesystems.FileObject root,
100: String encoding) {
101: if (encoding == null) {
102: return false;
103: }
104: encoding = encoding.toLowerCase();
105:
106: // if Japanese encoding, return true quickly
107: if ("iso-2022-jp".equals(encoding) // NOI18N
108: || "sjis".equals(encoding) // NOI18N
109: || "euc-jp".equals(encoding)) { // NOI18N
110:
111: setJapanEncoding(encoding);
112: return true;
113: }
114:
115: if ("utf-8".equals(encoding)) { // NOI18N
116: try {
117: FileObject fo = root
118: .getFileObject("allclasses-frame.html"); // NOI18N
119: if (fo == null) {
120: return false;
121: }
122: InputStream is = fo.getInputStream();
123: boolean jazip = false;
124: try {
125: BufferedReader r = new BufferedReader(
126: new InputStreamReader(is, encoding));
127: String line;
128: while ((line = r.readLine()) != null) {
129: if (line.contains(JDK12_ALLCLASSES_JA)) {
130: jazip = true;
131: }
132: if (line.toLowerCase().contains("</title>")) { // NOI18N
133: break;
134: }
135: }
136: } finally {
137: is.close();
138: }
139: if (jazip) {
140: setJapanEncoding(encoding);
141: }
142: return jazip;
143: } catch (IOException ioe) {
144: Exceptions.printStackTrace(ioe);
145: }
146: }
147:
148: return false;
149: }
150:
151: }
|