c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeErrorInfo.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeErrorInfo.java Wed Jun 07 20:23:29 2017 +0300
1.3 @@ -0,0 +1,147 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * If you wish your version of this file to be governed by only the CDDL
1.31 + * or only the GPL Version 2, indicate your decision by adding
1.32 + * "[Contributor] elects to include this software in this distribution
1.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.34 + * single choice of license, a recipient has the option to distribute
1.35 + * your version of this file under either the CDDL, the GPL Version 2 or
1.36 + * to extend the choice of license to its licensees as provided above.
1.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.38 + * Version 2 license, then the option applies only if the new code is
1.39 + * made subject to such option by the copyright holder.
1.40 + *
1.41 + * Contributor(s): Ilia Gromov
1.42 + */
1.43 +package com.sun.tools.ide.analysis.modernize.impl;
1.44 +
1.45 +import java.util.ArrayList;
1.46 +import java.util.Arrays;
1.47 +import java.util.Collections;
1.48 +import java.util.List;
1.49 +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorInfo;
1.50 +import org.netbeans.modules.cnd.api.project.NativeProject;
1.51 +import org.openide.util.Lookup;
1.52 +
1.53 +public class ModernizeErrorInfo implements CsmErrorInfo, ModernizeErrorProvider.ErrorInfoWithId {
1.54 +
1.55 + private final YamlParser.Diagnostics diag;
1.56 + private final String messagePrefix;
1.57 + private final List<String> messageInfixes;
1.58 + private final String messageBody;
1.59 + private final NativeProject project;
1.60 +
1.61 + private final int startOffset;
1.62 + private final int endOffset;
1.63 +
1.64 + public ModernizeErrorInfo(YamlParser.Diagnostics diag, String messagePrefix, List<String> messageInfixes, String messageBody, Lookup.Provider project) {
1.65 + this.diag = diag;
1.66 + this.messagePrefix = messagePrefix;
1.67 + this.messageInfixes = messageInfixes;
1.68 + this.messageBody = messageBody;
1.69 + this.project = project.getLookup().lookup(NativeProject.class);
1.70 +
1.71 + if (diag.getReplacements().isEmpty()) {
1.72 + startOffset = diag.getMessageFileOffset();
1.73 + endOffset = diag.getMessageFileOffset();
1.74 + } else {
1.75 + YamlParser.Replacement first = diag.getReplacements().get(0);
1.76 + startOffset = first.offset;
1.77 + endOffset = first.offset + first.length;
1.78 + }
1.79 + }
1.80 +
1.81 + public static ModernizeErrorInfo withFixedMessage(YamlParser.Diagnostics diag, String messageBody, Lookup.Provider project) {
1.82 + return new ModernizeErrorInfo(diag, "", new ArrayList<>(), messageBody, project);
1.83 + }
1.84 +
1.85 + public static ModernizeErrorInfo withMutableMessage(YamlParser.Diagnostics diag, String messagePrefix, String messageInfix, String messageBody, Lookup.Provider project) {
1.86 + List<String> list = new ArrayList<>();
1.87 + list.add(messageInfix);
1.88 + return new ModernizeErrorInfo(diag, messagePrefix, list, messageBody, project);
1.89 + }
1.90 +
1.91 + public static ModernizeErrorInfo withMutableMessage(YamlParser.Diagnostics diag, String messagePrefix, List<String> messageInfixes, String messageBody, Lookup.Provider project) {
1.92 + return new ModernizeErrorInfo(diag, messagePrefix, messageInfixes, messageBody, project);
1.93 + }
1.94 +
1.95 + public void addMessageInfixes(List<String> infix) {
1.96 + messageInfixes.addAll(infix);
1.97 + }
1.98 +
1.99 + public List<String> getMessageInfixes() {
1.100 + return Collections.unmodifiableList(messageInfixes);
1.101 + }
1.102 +
1.103 + public NativeProject getProject() {
1.104 + return project;
1.105 + }
1.106 +
1.107 + @Override
1.108 + public String getMessage() {
1.109 + StringBuilder sb = new StringBuilder();
1.110 + sb.append(messagePrefix).append(' ');
1.111 + if (messageInfixes.size() < 4) {
1.112 + sb.append(messageInfixes.toString());
1.113 + } else {
1.114 + ArrayList<String> list = new ArrayList<>(messageInfixes.subList(0, 2));
1.115 + list.add(String.format("... (%s more)", messageInfixes.size() - 2));
1.116 + sb.append(list.toString());
1.117 + }
1.118 + sb.append(": "); //NOI18N
1.119 + sb.append(messageBody);
1.120 + return sb.toString();
1.121 + }
1.122 +
1.123 + @Override
1.124 + public Severity getSeverity() {
1.125 + return Severity.valueOf(diag.getLevel().toString().toUpperCase());
1.126 + }
1.127 +
1.128 + @Override
1.129 + public int getStartOffset() {
1.130 + return startOffset;
1.131 + }
1.132 +
1.133 + @Override
1.134 + public int getEndOffset() {
1.135 + return endOffset;
1.136 + }
1.137 +
1.138 + public YamlParser.Diagnostics getDiagnostics() {
1.139 + return diag;
1.140 + }
1.141 +
1.142 + @Override
1.143 + public String getId() {
1.144 + return diag.getCheckName();
1.145 + }
1.146 +
1.147 + public boolean isNeedConfigureHint() {
1.148 + return true;
1.149 + }
1.150 +}