Some log output for xml resolver extension

This commit is contained in:
Kris De Volder
2020-11-16 13:25:28 -08:00
parent 17482b6316
commit 0411e6990d
2 changed files with 134 additions and 42 deletions

View File

@@ -29,6 +29,8 @@ import org.springframework.ide.vscode.xml.namespaces.util.DocumentAccessor;
import org.springframework.ide.vscode.xml.namespaces.util.DocumentAccessor.SchemaLocations;
import org.w3c.dom.Document;
import org.springframework.ide.vscode.xml.namespaces.util.Logger;
public class ProjectClasspathUriResolverExtension implements URIResolverExtension {
private IJavaProjectProvider javaProjectProvider;
@@ -69,50 +71,54 @@ public class ProjectClasspathUriResolverExtension implements URIResolverExtensio
@Override
public String resolve(String file, String publicId, String systemId) {
System.out.println("BaseLocation=" + file + " publicId=" + publicId + " systemId=" + systemId);
// systemId is already resolved; so don't touch
if (systemId != null && systemId.startsWith("jar:")) {
return null;
Logger.DEFAULT.log("Resolve XML from classpath.");
Logger.DEFAULT.log("BaseLocation=" + file + " publicId=" + publicId + " systemId=" + systemId);
// systemId is already resolved; so don't touch
if (systemId != null && systemId.startsWith("jar:")) {
return null;
}
// identify the correct project
IJavaProjectData project = null;
if (file != null) {
if (file.startsWith(ProjectAwareUrlStreamHandlerFactory.PROJECT_AWARE_PROTOCOL_HEADER)) {
String nameAndLocation = file
.substring(ProjectAwareUrlStreamHandlerFactory.PROJECT_AWARE_PROTOCOL_HEADER
.length());
String projectName = nameAndLocation.substring(0, nameAndLocation.indexOf('/'));
project = javaProjectProvider.get(projectName);
} else {
project = getBestMatchingProject(file);
}
// identify the correct project
IJavaProjectData project = null;
if (file != null) {
if (file.startsWith(ProjectAwareUrlStreamHandlerFactory.PROJECT_AWARE_PROTOCOL_HEADER)) {
String nameAndLocation = file
.substring(ProjectAwareUrlStreamHandlerFactory.PROJECT_AWARE_PROTOCOL_HEADER
.length());
String projectName = nameAndLocation.substring(0, nameAndLocation.indexOf('/'));
project = javaProjectProvider.get(projectName);
} else {
project = getBestMatchingProject(file);
}
}
if (project == null) {
return null;
}
if (systemId == null && file != null) {
systemId = findSystemIdFromFile(file, publicId);
}
if (systemId == null && publicId == null) {
return null;
}
ProjectClasspathUriResolver resolver = getProjectResolver(file, project);
if (resolver != null) {
String resolved = resolver.resolveOnClasspath(publicId, systemId);
if (resolved != null) {
resolved = ProjectAwareUrlStreamHandlerFactory.createProjectAwareUrl(project.getName(), resolved);
}
return resolved;
}
}
if (project == null) {
Logger.DEFAULT.log("Resolve XML from classpath failed. No project.");
return null;
}
if (systemId == null && file != null) {
systemId = findSystemIdFromFile(file, publicId);
}
if (systemId == null && publicId == null) {
Logger.DEFAULT.log("Resolve XML from classpath failed. No systemId && publicId.");
return null;
}
ProjectClasspathUriResolver resolver = getProjectResolver(file, project);
if (resolver != null) {
String resolved = resolver.resolveOnClasspath(publicId, systemId);
if (resolved != null) {
resolved = ProjectAwareUrlStreamHandlerFactory.createProjectAwareUrl(project.getName(), resolved);
}
Logger.DEFAULT.log("Resolve XML from classpath => "+resolved);
return resolved;
}
Logger.DEFAULT.log("Resolve XML from classpath failed. End of method");
return null;
}

View File

@@ -0,0 +1,86 @@
package org.springframework.ide.vscode.xml.namespaces.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Date;
/**
* Poor man's logger with a default implementation writes log output for jdt.ls extension into a predictable location.
*/
public interface Logger {
public static class NullLogger implements Logger {
@Override
public void log(String message) {
}
@Override
public void log(Exception e) {
}
}
public static Logger DEFAULT //pick one of the two below. Probably should use NullLogger in 'production'.
= new NullLogger();
//= new DefaultLogger(false);
public static class DefaultLogger implements Logger {
private PrintWriter printwriter;
public DefaultLogger(boolean USE_SYS_ERR) {
if (USE_SYS_ERR) {
printwriter = new PrintWriter(System.err);
} else {
File file = new File(System.getProperty("java.io.tmpdir"));
file = new File(file, "stsxmlls.log");
try {
printwriter = new PrintWriter(new FileOutputStream(file), true);
log("======== "+new Date()+" =======");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void log(String message) {
printwriter.println(message);
printwriter.flush();
}
@Override
public void log(Exception e) {
e.printStackTrace(printwriter);
}
}
public static class TestLogger extends DefaultLogger {
private Exception firstError;
public TestLogger() {
super(true);
}
@Override
public void log(Exception e) {
super.log(e);
if (firstError!=null) {
firstError = e;
}
}
public void assertNoErrors() throws Exception {
if (firstError!=null) {
throw firstError;
}
}
}
void log(String message);
void log(Exception e);
}