/*
 * Copyright 2014 - 2015 SATO taichi
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package io.gige.compiler.internal;

import java.io.File;
import java.util.Locale;

import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;

import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.apt.util.EclipseFileObject;

/**
 * @author taichi
 */
public class DiagnosticAdapter implements Diagnostic<JavaFileObject> {

	final Kind kind;
	final IProblem problem;

	public DiagnosticAdapter(Kind kind, IProblem problem) {
		this.kind = kind;
		this.problem = problem;
	}

	@Override
	public javax.tools.Diagnostic.Kind getKind() {
		return this.kind;
	}

	@Override
	public JavaFileObject getSource() {
		char[] filename = this.problem.getOriginatingFileName();
		if (filename == null) {
			return null;
		}
		File file = new File(new String(filename));
		if (file.exists()) {
			return new EclipseFileObject(null, file.toURI(),
					JavaFileObject.Kind.SOURCE, null);
		}
		return null;
	}

	@Override
	public long getPosition() {
		return problem.getSourceStart();
	}

	@Override
	public long getStartPosition() {
		return getPosition();
	}

	@Override
	public long getEndPosition() {
		return problem.getSourceEnd();
	}

	@Override
	public long getLineNumber() {
		return this.problem.getSourceLineNumber();
	}

	@Override
	public long getColumnNumber() {
		return NOPOS;
	}

	@Override
	public String getCode() {
		return null;
	}

	@Override
	public String getMessage(Locale locale) {
		return this.problem.getMessage();
	}

	@Override
	public String toString() {
		StringBuilder stb = new StringBuilder();
		stb.append(this.problem.toString());
		stb.append(" at ");
		stb.append(new String(this.problem.getOriginatingFileName()));
		stb.append(" line:[");
		stb.append(this.problem.getSourceLineNumber());
		stb.append("]");
		return stb.toString();
	}
}

@@ -2191,24 +2191,110 @@
 oString());%0A
+%09%09char%5B%5D filename = this.problem.getOriginatingFileName();%0A%09%09if (filename != null) %7B%0A%09
 %09%09stb.append
@@ -2299,24 +2299,25 @@
 nd(%22 at %22);%0A
+%09
 %09%09stb.append
@@ -2332,51 +2332,23 @@
 ing(
-this.problem.getOriginatingFileName()
+filename
 ));%0A%09%09
+%09
 stb.
@@ -2366,16 +2366,17 @@
 ne:%5B%22);%0A
+%09
 %09%09stb.ap
@@ -2415,24 +2415,25 @@
 umber());%0A%09%09
+%09
 stb.append(%22
@@ -2437,16 +2437,20 @@
 d(%22%5D%22);%0A
+%09%09%7D%0A
 %09%09return

fix filename problem.