Lines 11-28
Link Here
|
11 |
package org.eclipse.pde.internal.core.builders; |
11 |
package org.eclipse.pde.internal.core.builders; |
12 |
|
12 |
|
13 |
import java.util.*; |
13 |
import java.util.*; |
|
|
14 |
import java.util.Map.Entry; |
14 |
import org.eclipse.core.resources.*; |
15 |
import org.eclipse.core.resources.*; |
15 |
import org.eclipse.core.runtime.IPath; |
16 |
import org.eclipse.core.runtime.*; |
16 |
import org.eclipse.core.runtime.Path; |
|
|
17 |
import org.eclipse.jdt.core.*; |
17 |
import org.eclipse.jdt.core.*; |
18 |
import org.eclipse.osgi.util.NLS; |
18 |
import org.eclipse.osgi.util.NLS; |
|
|
19 |
import org.eclipse.pde.core.build.IBuild; |
19 |
import org.eclipse.pde.core.build.IBuildEntry; |
20 |
import org.eclipse.pde.core.build.IBuildEntry; |
|
|
21 |
import org.eclipse.pde.internal.core.PDECore; |
20 |
import org.eclipse.pde.internal.core.PDECoreMessages; |
22 |
import org.eclipse.pde.internal.core.PDECoreMessages; |
|
|
23 |
import org.eclipse.pde.internal.core.project.PDEProject; |
21 |
|
24 |
|
22 |
public class SourceEntryErrorReporter extends BuildErrorReporter { |
25 |
public class SourceEntryErrorReporter extends BuildErrorReporter { |
23 |
|
26 |
|
24 |
public SourceEntryErrorReporter(IFile file) { |
27 |
public SourceEntryErrorReporter(IFile file, IBuild model) { |
25 |
super(file); |
28 |
super(file); |
|
|
29 |
fBuild = model; |
26 |
} |
30 |
} |
27 |
|
31 |
|
28 |
class ProjectFolder { |
32 |
class ProjectFolder { |
Lines 131-138
Link Here
|
131 |
|
135 |
|
132 |
} |
136 |
} |
133 |
|
137 |
|
|
|
138 |
/** |
139 |
* Represents a default or custom encoding property for a resource |
140 |
* within a library. |
141 |
*/ |
142 |
class EncodingEntry { |
143 |
|
144 |
private String fEncoding; |
145 |
private IResource fResource; |
146 |
|
147 |
/** |
148 |
* Constructs an encoding entry for the given resource. |
149 |
* |
150 |
* @param resource resource |
151 |
* @param encoding the encoding identifier |
152 |
*/ |
153 |
EncodingEntry(IResource resource, String encoding) { |
154 |
fEncoding = encoding; |
155 |
fResource = resource; |
156 |
} |
157 |
|
158 |
/** |
159 |
* Returns the explicit encoding for this entry. |
160 |
* |
161 |
* @return explicit encoding |
162 |
*/ |
163 |
public String getEncoding() { |
164 |
return fEncoding; |
165 |
} |
166 |
|
167 |
/** |
168 |
* Returns the resource this encoding is associated with. |
169 |
* |
170 |
* @return associated resource |
171 |
*/ |
172 |
public IResource getResource() { |
173 |
return fResource; |
174 |
} |
175 |
|
176 |
/* (non-Javadoc) |
177 |
* @see java.lang.Object#toString() |
178 |
*/ |
179 |
public String toString() { |
180 |
return getValue(); |
181 |
} |
182 |
|
183 |
/* (non-Javadoc) |
184 |
* @see java.lang.Object#equals(java.lang.Object) |
185 |
*/ |
186 |
public boolean equals(Object obj) { |
187 |
if (obj instanceof EncodingEntry) { |
188 |
EncodingEntry other = (EncodingEntry) obj; |
189 |
return other.fEncoding.equals(fEncoding) && other.fResource.equals(fResource); |
190 |
} |
191 |
return false; |
192 |
} |
193 |
|
194 |
/* (non-Javadoc) |
195 |
* @see java.lang.Object#hashCode() |
196 |
*/ |
197 |
public int hashCode() { |
198 |
return fEncoding.hashCode() + fResource.hashCode(); |
199 |
} |
200 |
|
201 |
/** |
202 |
* Returns the generated value of this entry for the build.properties file. |
203 |
* |
204 |
* @return value to enter into build.properties |
205 |
*/ |
206 |
String getValue() { |
207 |
StringBuffer buf = new StringBuffer(); |
208 |
IContainer root = PDEProject.getBundleRoot(fResource.getProject()); |
209 |
buf.append(fResource.getFullPath().makeRelativeTo(root.getFullPath()).makeAbsolute()); |
210 |
buf.append('['); |
211 |
buf.append(fEncoding); |
212 |
buf.append(']'); |
213 |
return buf.toString(); |
214 |
} |
215 |
|
216 |
} |
217 |
|
218 |
/** |
219 |
* Visits a source folder gathering encodings. |
220 |
*/ |
221 |
class Visitor implements IResourceVisitor { |
222 |
|
223 |
String[] fLibs = null; |
224 |
|
225 |
Visitor(SourceFolder folder) { |
226 |
ArrayList list = folder.getLibs(); |
227 |
fLibs = (String[]) list.toArray(new String[list.size()]); |
228 |
} |
229 |
|
230 |
/* (non-Javadoc) |
231 |
* @see org.eclipse.core.resources.IResourceVisitor#visit(org.eclipse.core.resources.IResource) |
232 |
*/ |
233 |
public boolean visit(IResource resource) throws CoreException { |
234 |
String encoding = null; |
235 |
switch (resource.getType()) { |
236 |
case IResource.FOLDER : |
237 |
encoding = ((IFolder) resource).getDefaultCharset(false); |
238 |
break; |
239 |
case IResource.FILE : |
240 |
IFile file = (IFile) resource; |
241 |
// only worry about .java files |
242 |
if (file.getFileExtension().equals("java")) { //$NON-NLS-1$ |
243 |
encoding = file.getCharset(false); |
244 |
} |
245 |
break; |
246 |
} |
247 |
if (encoding != null) { |
248 |
EncodingEntry entry = new EncodingEntry(resource, encoding); |
249 |
for (int i = 0; i < fLibs.length; i++) { |
250 |
String lib = fLibs[i]; |
251 |
List encodings = (List) fCustomEncodings.get(lib); |
252 |
if (encodings == null) { |
253 |
encodings = new ArrayList(); |
254 |
fCustomEncodings.put(lib, encodings); |
255 |
} |
256 |
encodings.add(entry); |
257 |
} |
258 |
} |
259 |
return true; |
260 |
} |
261 |
|
262 |
} |
263 |
|
134 |
private HashMap fSourceFolderMap = new HashMap(4); |
264 |
private HashMap fSourceFolderMap = new HashMap(4); |
135 |
private HashMap fOutputFolderMap = new HashMap(4); |
265 |
private HashMap fOutputFolderMap = new HashMap(4); |
|
|
266 |
private IBuild fBuild = null; |
267 |
|
268 |
/** |
269 |
* Maps library name to default encoding for that library (or not present if there is no |
270 |
* explicit default encoding specified). |
271 |
*/ |
272 |
Map fDefaultLibraryEncodings = new HashMap(); |
273 |
|
274 |
/** |
275 |
* Maps library name to custom {@link EncodingEntry}'s for this library. |
276 |
*/ |
277 |
Map fCustomEncodings = new HashMap(); |
136 |
|
278 |
|
137 |
public void initialize(ArrayList sourceEntries, ArrayList outputEntries, IClasspathEntry[] cpes, IProject project) { |
279 |
public void initialize(ArrayList sourceEntries, ArrayList outputEntries, IClasspathEntry[] cpes, IProject project) { |
138 |
|
280 |
|
Lines 297-302
Link Here
|
297 |
} |
439 |
} |
298 |
} |
440 |
} |
299 |
|
441 |
|
|
|
442 |
List toValidate = new ArrayList(); // list of source folders to perform encoding validation on |
300 |
for (Iterator iterator = fSourceFolderMap.keySet().iterator(); iterator.hasNext();) { |
443 |
for (Iterator iterator = fSourceFolderMap.keySet().iterator(); iterator.hasNext();) { |
301 |
IPath sourcePath = (IPath) iterator.next(); |
444 |
IPath sourcePath = (IPath) iterator.next(); |
302 |
SourceFolder sourceFolder = (SourceFolder) fSourceFolderMap.get(sourcePath); |
445 |
SourceFolder sourceFolder = (SourceFolder) fSourceFolderMap.get(sourcePath); |
Lines 339-344
Link Here
|
339 |
String message = NLS.bind(PDECoreMessages.SourceEntryErrorReporter_DupeSourceFolder, sourcePath.toString(), PROPERTY_SOURCE_PREFIX + sourceFolder.getDupeLibName()); |
482 |
String message = NLS.bind(PDECoreMessages.SourceEntryErrorReporter_DupeSourceFolder, sourcePath.toString(), PROPERTY_SOURCE_PREFIX + sourceFolder.getDupeLibName()); |
340 |
prepareError(PROPERTY_SOURCE_PREFIX + sourceFolder.getDupeLibName(), sourceFolder.getToken(), message, PDEMarkerFactory.NO_RESOLUTION, fSrcLibSeverity, PDEMarkerFactory.CAT_OTHER); |
483 |
prepareError(PROPERTY_SOURCE_PREFIX + sourceFolder.getDupeLibName(), sourceFolder.getToken(), message, PDEMarkerFactory.NO_RESOLUTION, fSrcLibSeverity, PDEMarkerFactory.CAT_OTHER); |
341 |
} |
484 |
} |
|
|
485 |
|
486 |
toValidate.add(sourceFolder); |
342 |
} |
487 |
} |
343 |
} |
488 |
} |
344 |
|
489 |
|
Lines 348-353
Link Here
|
348 |
String message = NLS.bind(PDECoreMessages.SourceEntryErrorReporter_MissingOutputEntry, errorEntry.get(errorEntry.fSsrcFolders), PROPERTY_OUTPUT_PREFIX + libName); |
493 |
String message = NLS.bind(PDECoreMessages.SourceEntryErrorReporter_MissingOutputEntry, errorEntry.get(errorEntry.fSsrcFolders), PROPERTY_OUTPUT_PREFIX + libName); |
349 |
prepareError(PROPERTY_OUTPUT_PREFIX + libName, errorEntry.get(errorEntry.fOutputFolders), message, PDEMarkerFactory.B_ADDITION, fMissingOutputLibSeverity, PDEMarkerFactory.CAT_OTHER); |
494 |
prepareError(PROPERTY_OUTPUT_PREFIX + libName, errorEntry.get(errorEntry.fOutputFolders), message, PDEMarkerFactory.B_ADDITION, fMissingOutputLibSeverity, PDEMarkerFactory.CAT_OTHER); |
350 |
} |
495 |
} |
|
|
496 |
|
497 |
// validate workspace encodings with those specified in build.properties |
498 |
|
499 |
if (fEncodingSeverity == CompilerFlags.ERROR || fEncodingSeverity == CompilerFlags.WARNING) { |
500 |
// build map of expected encodings |
501 |
Iterator iterator = toValidate.iterator(); |
502 |
while (iterator.hasNext()) { |
503 |
SourceFolder sourceFolder = (SourceFolder) iterator.next(); |
504 |
IPath sourcePath = sourceFolder.getPath(); |
505 |
IFolder folder = fProject.getFolder(sourcePath); |
506 |
try { |
507 |
ArrayList list = sourceFolder.getLibs(); |
508 |
String[] libs = (String[]) list.toArray(new String[list.size()]); |
509 |
String encoding = getExplicitEncoding(folder); |
510 |
if (encoding != null) { |
511 |
for (int i = 0; i < libs.length; i++) { |
512 |
fDefaultLibraryEncodings.put(libs[i], encoding); |
513 |
} |
514 |
} |
515 |
folder.accept(new Visitor(sourceFolder)); |
516 |
} catch (CoreException e) { |
517 |
// Can't validate if unable to retrieve encoding |
518 |
PDECore.log(e); |
519 |
} |
520 |
|
521 |
} |
522 |
|
523 |
// Compare to encodings specified in build.properties (if any) |
524 |
IBuildEntry[] entries = fBuild.getBuildEntries(); |
525 |
for (int i = 0; i < entries.length; i++) { |
526 |
IBuildEntry entry = entries[i]; |
527 |
String name = entry.getName(); |
528 |
if (name.startsWith(PROPERTY_JAVAC_DEFAULT_ENCODING_PREFIX)) { |
529 |
String lib = name.substring(PROPERTY_JAVAC_DEFAULT_ENCODING_PREFIX.length()); |
530 |
String[] tokens = entry.getTokens(); |
531 |
if (tokens.length > 0) { |
532 |
if (tokens.length == 1) { |
533 |
// compare |
534 |
String specified = tokens[0]; |
535 |
String expected = (String) fDefaultLibraryEncodings.remove(lib); |
536 |
if (expected != null) { |
537 |
if (!specified.equals(expected)) { |
538 |
prepareError(name, specified, NLS.bind(PDECoreMessages.SourceEntryErrorReporter_0, new String[] {expected, specified, lib}), PDEMarkerFactory.NO_RESOLUTION, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
539 |
} |
540 |
} else { |
541 |
// encoding is specified, but workspace does not specify one |
542 |
prepareError(name, null, NLS.bind(PDECoreMessages.SourceEntryErrorReporter_1, new String[] {specified, lib}), PDEMarkerFactory.B_REMOVAL, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
543 |
} |
544 |
} else { |
545 |
// syntax error |
546 |
fDefaultLibraryEncodings.remove(lib); |
547 |
prepareError(name, null, NLS.bind(PDECoreMessages.SourceEntryErrorReporter_2, lib), PDEMarkerFactory.NO_RESOLUTION, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
548 |
} |
549 |
} |
550 |
} else if (name.startsWith(PROPERTY_JAVAC_CUSTOM_ENCODINGS_PREFIX)) { |
551 |
IContainer bundleRoot = PDEProject.getBundleRoot(fProject); |
552 |
String lib = name.substring(PROPERTY_JAVAC_CUSTOM_ENCODINGS_PREFIX.length()); |
553 |
String[] tokens = entry.getTokens(); |
554 |
if (tokens.length > 0) { |
555 |
List encodings = new ArrayList(); |
556 |
for (int j = 0; j < tokens.length; j++) { |
557 |
String special = tokens[j]; |
558 |
int index = special.indexOf('['); |
559 |
if (index >= 0 && special.endsWith("]")) { //$NON-NLS-1$ |
560 |
String path = special.substring(0, index); |
561 |
String encoding = special.substring(index + 1, special.length() - 1); |
562 |
IResource member = bundleRoot.findMember(path); |
563 |
if (member == null) { |
564 |
// error - missing resource |
565 |
String message = NLS.bind(PDECoreMessages.SourceEntryErrorReporter_3, new String[] {encoding, path}); |
566 |
prepareError(name, special, message, PDEMarkerFactory.B_REMOVAL, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
567 |
} else { |
568 |
encodings.add(new EncodingEntry(member, encoding)); |
569 |
} |
570 |
} else { |
571 |
// syntax error - invalid |
572 |
String message = PDECoreMessages.SourceEntryErrorReporter_4; |
573 |
prepareError(name, special, message, PDEMarkerFactory.NO_RESOLUTION, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
574 |
} |
575 |
} |
576 |
// compare with workspace encodings |
577 |
List workspace = (List) fCustomEncodings.remove(lib); |
578 |
if (workspace == null) { |
579 |
prepareError(name, null, NLS.bind(PDECoreMessages.SourceEntryErrorReporter_5, lib), PDEMarkerFactory.B_REMOVAL, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
580 |
} else { |
581 |
Map map = new HashMap(); |
582 |
Iterator iter = workspace.iterator(); |
583 |
while (iter.hasNext()) { |
584 |
EncodingEntry ee = (EncodingEntry) iter.next(); |
585 |
map.put(ee.getResource(), ee.getEncoding()); |
586 |
} |
587 |
iter = encodings.iterator(); |
588 |
while (iter.hasNext()) { |
589 |
EncodingEntry ee = (EncodingEntry) iter.next(); |
590 |
String specified = ee.getEncoding(); |
591 |
String expected = (String) map.remove(ee.getResource()); |
592 |
if (expected == null) { |
593 |
prepareError(name, ee.getValue(), NLS.bind(PDECoreMessages.SourceEntryErrorReporter_6, new String[] {expected, ee.getResource().getProjectRelativePath().toString()}), PDEMarkerFactory.B_REMOVAL, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
594 |
} else { |
595 |
if (!specified.equals(expected)) { |
596 |
prepareError(name, ee.getValue(), NLS.bind(PDECoreMessages.SourceEntryErrorReporter_7, new String[] {expected, ee.getResource().getProjectRelativePath().toString(), specified}), PDEMarkerFactory.NO_RESOLUTION, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
597 |
} |
598 |
} |
599 |
} |
600 |
// anything left in the workspace map? |
601 |
if (!map.isEmpty()) { |
602 |
iter = map.entrySet().iterator(); |
603 |
while (iter.hasNext()) { |
604 |
Entry en = (Entry) iter.next(); |
605 |
IResource res = (IResource) en.getKey(); |
606 |
String expected = (String) en.getValue(); |
607 |
EncodingEntry missing = new EncodingEntry(res, expected); |
608 |
String m = NLS.bind(PDECoreMessages.SourceEntryErrorReporter_8, new String[] {expected, res.getProjectRelativePath().toString()}); |
609 |
prepareError(name, missing.getValue(), m, PDEMarkerFactory.B_ADDITION, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
610 |
} |
611 |
} |
612 |
} |
613 |
} |
614 |
|
615 |
} |
616 |
} |
617 |
|
618 |
// check for unspecified default encodings |
619 |
Iterator iter = fDefaultLibraryEncodings.entrySet().iterator(); |
620 |
while (iter.hasNext()) { |
621 |
Entry entry = (Entry) iter.next(); |
622 |
String lib = (String) entry.getKey(); |
623 |
String expected = (String) entry.getValue(); |
624 |
prepareError(PROPERTY_JAVAC_DEFAULT_ENCODING_PREFIX + lib, expected, NLS.bind(PDECoreMessages.SourceEntryErrorReporter_9, new String[] {expected, lib}), PDEMarkerFactory.B_ADDITION, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
625 |
} |
626 |
|
627 |
// check for unspecified custom encodings |
628 |
iter = fCustomEncodings.entrySet().iterator(); |
629 |
while (iter.hasNext()) { |
630 |
Entry entry = (Entry) iter.next(); |
631 |
String lib = (String) entry.getKey(); |
632 |
List encodings = (List) entry.getValue(); |
633 |
Iterator iterator2 = encodings.iterator(); |
634 |
while (iterator2.hasNext()) { |
635 |
EncodingEntry encoding = (EncodingEntry) iterator2.next(); |
636 |
String m = NLS.bind(PDECoreMessages.SourceEntryErrorReporter_10, new String[] {encoding.getEncoding(), encoding.getResource().getProjectRelativePath().toString()}); |
637 |
prepareError(PROPERTY_JAVAC_CUSTOM_ENCODINGS_PREFIX + lib, encoding.getValue(), m, PDEMarkerFactory.B_ADDITION, fEncodingSeverity, PDEMarkerFactory.CAT_OTHER); |
638 |
} |
639 |
} |
640 |
} |
641 |
} |
642 |
|
643 |
/** |
644 |
* Returns any explicit encoding set on the given container or one of its parents |
645 |
* up to and including its project. |
646 |
* |
647 |
* @param container container |
648 |
* @return any explicit encoding or <code>null</code> if none |
649 |
* @throws CoreException |
650 |
*/ |
651 |
private String getExplicitEncoding(IContainer container) throws CoreException { |
652 |
String encoding = container.getDefaultCharset(false); |
653 |
if (encoding == null) { |
654 |
IContainer parent = container.getParent(); |
655 |
if (parent != null) { |
656 |
switch (parent.getType()) { |
657 |
case IResource.FOLDER : |
658 |
return getExplicitEncoding(parent); |
659 |
case IResource.PROJECT : |
660 |
return getExplicitEncoding(parent); |
661 |
default : |
662 |
// don't consider workspace encoding |
663 |
return null; |
664 |
} |
665 |
} |
666 |
} |
667 |
return encoding; |
351 |
} |
668 |
} |
352 |
|
669 |
|
353 |
private String join(ProjectFolder[] folders) { |
670 |
private String join(ProjectFolder[] folders) { |