1 19 20 package org.netbeans.modules.javacore; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 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 37 public final class FileGuardedResolver implements GuardedResolver { 38 39 private static final String GEN_BEGIN = "//GEN-BEGIN"; private static final String GEN_END = "//GEN-END"; private static final String GEN_HEADER = "//GEN-HEADER"; private static final String GEN_HEADEREND = "//GEN-HEADEREND"; private static final String GEN_LINE = "//GEN-LINE"; private static final String GEN_FIRST = "//GEN-FIRST"; private static final String GEN_LAST = "//GEN-LAST"; 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 borders = new ArrayList (); 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 text = provider.getText(pads[y]); 93 int gid = guardedId(text); 94 switch (gid) { 95 case GUARDED_BEGIN: 96 borders.add(new Integer (lineStart)); 97 break; 98 case GUARDED_END: 99 borders.add(new Integer (pads[y].getEndOffset())); 100 break; 101 case GUARDED_LINE: 102 borders.add(new Integer (lineStart)); 103 borders.add(new Integer (pads[y].getEndOffset())); 104 } } } } int[] result = new int[borders.size()]; 109 Iterator iter = borders.iterator(); 110 for (int x = 0; iter.hasNext(); x++) { 111 result[x] = ((Integer )iter.next()).intValue(); 112 } 113 return result.length > 0 ? provider.getDocumentOffsets(result) : result; 114 } 115 116 private int guardedId(String 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 |