KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > FileGuardedResolver


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
20 package org.netbeans.modules.javacore;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.jmi.javamodel.Resource;
26 import org.netbeans.lib.java.parser.ASTree;
27 import org.netbeans.lib.java.parser.ParserTokens;
28 import org.netbeans.lib.java.parser.Token;
29 import org.netbeans.modules.javacore.internalapi.GuardedResolver;
30 import org.netbeans.modules.javacore.jmiimpl.javamodel.ResourceImpl;
31 import org.netbeans.modules.javacore.parser.MDRParser;
32
33 /**
34  *
35  * @author Jan Becicka
36  */

37 public final class FileGuardedResolver implements GuardedResolver {
38     
39     private static final String JavaDoc GEN_BEGIN = "//GEN-BEGIN"; // NOI18N
40
private static final String JavaDoc GEN_END = "//GEN-END"; // NOI18N
41
private static final String JavaDoc GEN_HEADER = "//GEN-HEADER"; // NOI18N
42
private static final String JavaDoc GEN_HEADEREND = "//GEN-HEADEREND"; // NOI18N
43
private static final String JavaDoc GEN_LINE = "//GEN-LINE"; // NOI18N
44
private static final String JavaDoc GEN_FIRST = "//GEN-FIRST"; // NOI18N
45
private static final String JavaDoc GEN_LAST = "//GEN-LAST"; // NOI18N
46

47     private static final int GUARDED_BEGIN = 0;
48     private static final int GUARDED_END = 1;
49     private static final int GUARDED_LINE = 2;
50     private static final int GUARDED_NONE = -1;
51     
52     private static FileGuardedResolver instance = null;
53     
54     private FileGuardedResolver() {
55     }
56     
57     public static synchronized FileGuardedResolver getDefault() {
58         if (instance == null) {
59             instance = new FileGuardedResolver();
60         }
61         return instance;
62     }
63     
64     public final boolean isSectionGuarded(org.netbeans.jmi.javamodel.Resource resource, org.openide.text.PositionBounds bounds) {
65         MDRParser parser = ((ResourceImpl) resource).getParser();
66         if (parser.guardedBlocksBorders == null) {
67             parser.guardedBlocksBorders = findGuardedBlocks(resource);
68         }
69         return isGuarded(parser.guardedBlocksBorders, bounds.getBegin().getOffset()) ||
70                 isGuarded(parser.guardedBlocksBorders, bounds.getEnd().getOffset());
71     }
72     
73     private int[] findGuardedBlocks(Resource resource) {
74         MDRParser provider = ((ResourceImpl)resource).getParser();
75         int lastTokenIndex = 0;
76         ASTree tree = provider.getASTree();
77         if (tree != null) {
78             lastTokenIndex = tree.getLastToken();
79         }
80         List JavaDoc borders = new ArrayList JavaDoc();
81         int lineStart = 0;
82         for (int x = 0; x <= lastTokenIndex; x++) {
83             Token token = provider.getToken(x);
84             Token[] pads = token.getPadding();
85             if (pads == null)
86                 continue;
87             for (int y = 0; y < pads.length; y++) {
88                 int padType = pads[y].getType();
89                 if (padType == ParserTokens.EOL) {
90                     lineStart = pads[y].getEndOffset();
91                 } else if (padType == ParserTokens.EOL_COMMENT) {
92                     String JavaDoc text = provider.getText(pads[y]);
93                     int gid = guardedId(text);
94                     switch (gid) {
95                         case GUARDED_BEGIN:
96                             borders.add(new Integer JavaDoc(lineStart));
97                             break;
98                         case GUARDED_END:
99                             borders.add(new Integer JavaDoc(pads[y].getEndOffset()));
100                             break;
101                         case GUARDED_LINE:
102                             borders.add(new Integer JavaDoc(lineStart));
103                             borders.add(new Integer JavaDoc(pads[y].getEndOffset()));
104                     } // switch
105
} // if
106
} // for
107
} // for
108
int[] result = new int[borders.size()];
109         Iterator JavaDoc iter = borders.iterator();
110         for (int x = 0; iter.hasNext(); x++) {
111             result[x] = ((Integer JavaDoc)iter.next()).intValue();
112         }
113         return result.length > 0 ? provider.getDocumentOffsets(result) : result;
114     }
115     
116     private int guardedId(String JavaDoc text) {
117         if (text == null)
118             return GUARDED_NONE;
119         if (text.indexOf(GEN_BEGIN) > -1 || text.indexOf(GEN_HEADER) > -1)
120             return GUARDED_BEGIN;
121         if (text.indexOf(GEN_END) > -1 || text.indexOf(GEN_HEADEREND) > -1)
122             return GUARDED_END;
123         if (text.indexOf(GEN_LINE) > -1 || text.indexOf(GEN_FIRST) > -1 || text.indexOf(GEN_LAST) > -1)
124             return GUARDED_LINE;
125         return GUARDED_NONE;
126     }
127     
128     private boolean isGuarded(int[] borders, int start, int end) {
129         if (borders.length == 0) {
130             return false;
131         }
132         int index = 0;
133         if (start < borders[0]) {
134             return end < borders[0];
135         }
136         while (index < borders.length && borders[index] < start)
137             index++;
138         if (index == borders.length)
139             return false;
140         if (borders[index] == start)
141             return true;
142         if (index % 2 == 0) {
143             return end < borders[index];
144         } else {
145             return true;
146         }
147     }
148     
149     private boolean isGuarded(int[] borders, int offset) {
150         if (borders.length == 0 || offset < borders[0]) {
151             return false;
152         }
153         for (int x = 0; x < borders.length / 2; x++) {
154             if (offset >= borders[2*x] && offset <= borders[2*x+1])
155                 return true;
156         }
157         return false;
158     }
159     
160 }
161
Popular Tags