diff --git a/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java b/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java index e4cdc5b4a705..7dfb070819e0 100644 --- a/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java +++ b/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java @@ -42,6 +42,8 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.Charset; import java.util.*; @@ -497,6 +499,26 @@ public class VfsUtil extends VfsUtilCore { return url; } + /** + * @return correct URI, must be used only for external communication + */ + @NotNull + public static URI toUri(@NotNull VirtualFile file) { + String path = file.getPath(); + try { + if (file.isInLocalFileSystem()) { + if (SystemInfo.isWindows && path.charAt(0) != '/') { + path = '/' + path; + } + return new URI(file.getFileSystem().getProtocol(), "", path, null, null); + } + return new URI(file.getFileSystem().getProtocol(), path, null); + } + catch (URISyntaxException e) { + throw new IllegalArgumentException(e); + } + } + /** * Returns the relative path from one virtual file to another. * diff --git a/xml/impl/src/com/intellij/ide/browsers/impl/WebBrowserServiceImpl.java b/xml/impl/src/com/intellij/ide/browsers/impl/WebBrowserServiceImpl.java index 0744e91c96aa..4cfdc0c3f573 100644 --- a/xml/impl/src/com/intellij/ide/browsers/impl/WebBrowserServiceImpl.java +++ b/xml/impl/src/com/intellij/ide/browsers/impl/WebBrowserServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2013 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package com.intellij.ide.browsers.impl; import com.intellij.ide.browsers.WebBrowserService; import com.intellij.ide.browsers.WebBrowserUrlProvider; import com.intellij.openapi.project.DumbService; +import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.impl.http.HttpVirtualFile; import com.intellij.psi.PsiElement; @@ -47,32 +48,28 @@ public class WebBrowserServiceImpl extends WebBrowserService { if (psiFile == null) { return null; } - final VirtualFile virtualFile = psiFile.getVirtualFile(); + VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile == null) { return null; } - final String localUrl = virtualFile.getUrl(); if (virtualFile instanceof HttpVirtualFile) { - return localUrl; + return virtualFile.getUrl(); } - if (preferLocalUrl && HtmlUtil.isHtmlFile(psiFile)) { - return localUrl; - } - - final WebBrowserUrlProvider provider = getProvider(psiElement); - if (provider == null) { - return localUrl; - } - try { - return provider.getUrl(psiElement); - } - catch (WebBrowserUrlProvider.BrowserException e) { - if (HtmlUtil.isHtmlFile(psiFile)) { - return localUrl; + if (!(preferLocalUrl && HtmlUtil.isHtmlFile(psiFile))) { + WebBrowserUrlProvider provider = getProvider(psiElement); + if (provider != null) { + try { + return provider.getUrl(psiElement, psiFile, virtualFile); + } + catch (WebBrowserUrlProvider.BrowserException e) { + if (!HtmlUtil.isHtmlFile(psiFile)) { + throw e; + } + } } - throw e; } + return VfsUtil.toUri(virtualFile).toASCIIString(); } @Nullable diff --git a/xml/impl/src/com/intellij/xml/util/HtmlUtil.java b/xml/impl/src/com/intellij/xml/util/HtmlUtil.java index 608d7d80fd0c..21a36c739c97 100644 --- a/xml/impl/src/com/intellij/xml/util/HtmlUtil.java +++ b/xml/impl/src/com/intellij/xml/util/HtmlUtil.java @@ -591,17 +591,12 @@ public class HtmlUtil { } public static boolean hasHtml(PsiFile file) { - if (isHtmlFile(file)) return true; - if (file.getViewProvider() instanceof TemplateLanguageFileViewProvider) return true; - return false; + return isHtmlFile(file) || file.getViewProvider() instanceof TemplateLanguageFileViewProvider; } public static boolean isHtmlFile(final PsiFile file) { final Language language = file.getLanguage(); - if (language == HTMLLanguage.INSTANCE || language == XHTMLLanguage.INSTANCE) { - return true; - } - return false; + return language == HTMLLanguage.INSTANCE || language == XHTMLLanguage.INSTANCE; } public static boolean isHtmlTagContainingFile(PsiElement element) { diff --git a/xml/openapi/src/com/intellij/ide/browsers/WebBrowserUrlProvider.java b/xml/openapi/src/com/intellij/ide/browsers/WebBrowserUrlProvider.java index 117f0ecc70d7..fba5b3ff873c 100644 --- a/xml/openapi/src/com/intellij/ide/browsers/WebBrowserUrlProvider.java +++ b/xml/openapi/src/com/intellij/ide/browsers/WebBrowserUrlProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2013 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package com.intellij.ide.browsers; import com.intellij.openapi.extensions.ExtensionPointName; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import org.jetbrains.annotations.NotNull; @@ -37,27 +38,17 @@ public abstract class WebBrowserUrlProvider { } /** - * Invariant: element has not null containing psi file with not null virtual file + * Invariant: element has not null containing psi file with not null virtual file + * @deprecated */ @NotNull public String getUrl(@NotNull PsiElement element) throws BrowserException { - try { - return getUrl(element, false); - } - catch (BrowserException e) { - throw e; - } - catch (Exception e) { - throw new RuntimeException(e); - } + throw new AbstractMethodError(); } - /** - * @deprecated override {@link #getUrl(com.intellij.psi.PsiElement)} instead - */ @NotNull - public String getUrl(@NotNull PsiElement element, boolean shiftDown) throws Exception { - throw new UnsupportedOperationException(); + public String getUrl(@NotNull PsiElement element, @NotNull PsiFile psiFile, @NotNull VirtualFile virtualFile) throws BrowserException { + return getUrl(element); } /**