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