/** * Check Map Entry duplicate keys. * * @param resource Marker target resource. * @param dfmodel DFProp Models. */ private void checkDuplicateKeys(IResource resource, DFPropModel dfmodel) { if (dfmodel.isReferences()) { return; } if (dfmodel instanceof BlockModel && ((BlockModel) dfmodel).isMissingEndBrace()) { createMissingBraceMarker(resource, (BlockModel) dfmodel); } Set<String> keyNames = new HashSet<String>(); List<DFPropModel> cache = new ArrayList<DFPropModel>(); for (DFPropModel element : dfmodel.getChild()) { if (element instanceof NamedModel) { NamedModel entry = (NamedModel) element; if (keyNames.contains(entry.getNameText())) { createDuplicateKeyMarker(resource, entry); } else { keyNames.add(entry.getNameText()); } } if (dfmodel instanceof MapModel) { if (element instanceof MultiLineCommentModel || element instanceof CommentModel || element instanceof NamedModel) { if (cache.size() > 0) { DFPropModel first = cache.get(0); DFPropModel last = cache.get(cache.size() - 1); createStatementErrorMarker( resource, first.getLineNumber(), first.getOffset(), last.getOffset() + last.getLength()); cache = new ArrayList<DFPropModel>(); } } else { cache.add(element); } } checkDuplicateKeys(resource, element); } if (cache.size() > 0) { DFPropModel first = cache.get(0); DFPropModel last = cache.get(cache.size() - 1); createStatementErrorMarker( resource, first.getLineNumber(), first.getOffset(), last.getOffset() + last.getLength()); } }
/** * Create marker of duplicate Map Entry error. * * @param resource Marker target resource. * @param model Map Entry model. */ private void createDuplicateKeyMarker(IResource resource, NamedModel model) { try { IMarker marker = resource.createMarker(PROBLEM_MARKER_KEY); Map<String, Object> attribute = new HashMap<String, Object>(); attribute.put(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); attribute.put( IMarker.MESSAGE, "The value of the key " + model.getNameText() + " is duplicated."); attribute.put(IMarker.LINE_NUMBER, model.getLineNumber()); attribute.put(IMarker.CHAR_START, model.getOffset()); attribute.put(IMarker.CHAR_END, model.getOffset() + model.getNameText().length()); marker.setAttributes(attribute); } catch (CoreException e) { // log.error(e.getMessage(), e); } }
protected IFile getReferencesFile(NamedModel element) { IFile file = ((IFileEditorInput) getEditorInput()).getFile(); IProject project = file.getProject(); IPath projectRelativePath = file.getProjectRelativePath(); String fileExtension = projectRelativePath.getFileExtension(); IPath prefix = projectRelativePath.removeLastSegments(1); String nameSegment = projectRelativePath.removeFileExtension().lastSegment(); IPath referencesPath = prefix.append(nameSegment + "_" + element.getNameText()).addFileExtension(fileExtension); IFile refFile = project.getFile(referencesPath.toString()); return refFile; }