KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > usages > ResultConvertor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.source.usages;
20
21 import javax.lang.model.element.ElementKind;
22 import javax.lang.model.element.TypeElement;
23 import org.netbeans.api.java.source.ElementHandle;
24 import org.netbeans.modules.java.source.ElementHandleAccessor;
25 import org.netbeans.modules.java.source.parsing.FileObjects;
26 import org.openide.filesystems.FileObject;
27
28 /**
29  *
30  * @author Tomas Zezula
31  */

32 public abstract class ResultConvertor<T> {
33
34     public abstract T convert (ElementKind kind, String JavaDoc value);
35
36
37     public static ResultConvertor<FileObject> fileObjectConvertor (final FileObject... roots) {
38         assert roots != null;
39         return new FileObjectConvertor (roots);
40     }
41     
42     public static ResultConvertor<ElementHandle<TypeElement>> elementHandleConvertor () {
43         return new ElementHandleConvertor ();
44     }
45     
46     public static ResultConvertor<String JavaDoc> identityConvertor () {
47         return new IdentityConvertor ();
48     }
49     
50     
51     private static class FileObjectConvertor extends ResultConvertor<FileObject> {
52         
53         private FileObject[] roots;
54         
55         private FileObjectConvertor (final FileObject... roots) {
56             this.roots = roots;
57         }
58         
59         public FileObject convert (ElementKind kind, String JavaDoc value) {
60             for (int i=0; i<roots.length; i++) {
61                 FileObject result = resolveFile (roots[i], value);
62                 if (result != null) {
63                     return result;
64                 }
65             }
66             return null;
67         }
68         
69         private static FileObject resolveFile (final FileObject root, String JavaDoc classBinaryName) {
70             assert classBinaryName != null;
71             classBinaryName = classBinaryName.replace('.', '/'); //NOI18N
72
int index = classBinaryName.lastIndexOf('/'); //NOI18N
73
FileObject folder;
74             String JavaDoc name;
75             if (index<0) {
76                 folder = root;
77                 name = classBinaryName;
78             }
79             else {
80                 assert index>0 : classBinaryName;
81                 assert index<classBinaryName.length() - 1 : classBinaryName;
82                 folder = root.getFileObject(classBinaryName.substring(0,index));
83                 name = classBinaryName.substring(index+1);
84             }
85             if (folder == null) {
86                 return null;
87             }
88             index = name.indexOf('$'); //NOI18N
89
if (index>0) {
90                 name = name.substring(0, index);
91             }
92             for (FileObject child : folder.getChildren()) {
93                 if (FileObjects.JAVA.equalsIgnoreCase(child.getExt()) && name.equals(child.getName())) {
94                     return child;
95                 }
96             }
97             return null;
98         }
99     }
100     
101     private static class ElementHandleConvertor extends ResultConvertor<ElementHandle<TypeElement>> {
102         
103         public ElementHandle<TypeElement> convert (final ElementKind kind, final String JavaDoc value) {
104             return createTypeHandle(kind, value);
105         }
106         
107         @SuppressWarnings JavaDoc ("unchecked") // NOI18N
108
private static ElementHandle<TypeElement> createTypeHandle (final ElementKind kind, final String JavaDoc binaryName) {
109             assert binaryName != null;
110             return ElementHandleAccessor.INSTANCE.create(kind, binaryName);
111         }
112         
113     }
114     
115     private static class IdentityConvertor extends ResultConvertor<String JavaDoc> {
116         
117         public String JavaDoc convert (ElementKind kind, String JavaDoc value) {
118             return value;
119         }
120     }
121 }
122
Popular Tags