Skip to content

Commit

Permalink
Improve schema generation from an xml doc whose name is also an
Browse files Browse the repository at this point in the history
extension

Fixes #805

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr authored and fbricon committed Jun 24, 2020
1 parent da67a76 commit 1c9d3a3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ public void doCodeAction(Diagnostic diagnostic, Range range, DOMDocument documen
*
* @return the unique grammar URI file.
*/
private static String getGrammarURI(String documentURI, String fileExtension) {
static String getGrammarURI(String documentURI, String fileExtension) {
int index = documentURI.lastIndexOf('.');
if (index > 1 && documentURI.charAt(index - 1) == '/') {
// case with file which starts with '.' (ex .project, .classpath).
index = -1;
}
String grammarWithoutExtension = index != -1 ? documentURI.substring(0, index) : documentURI;
String grammarURI = grammarWithoutExtension + "." + fileExtension;
int i = 1;
Expand All @@ -146,7 +150,7 @@ private static String getGrammarURI(String documentURI, String fileExtension) {
return grammarURI;
}

private static String getFileName(String schemaURI) {
static String getFileName(String schemaURI) {
return new File(schemaURI).getName();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.contentmodel.participants.codeactions;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
* Generate grammar URI tests.
*
*/
public class NoGrammarConstraintsCodeActionTest {

@Test
public void generateGrammarURI() {
String actual = NoGrammarConstraintsCodeAction.getGrammarURI("file:///C:/test.xml", "xsd");
assertEquals("file:///C:/test.xsd", actual);
}

@Test
public void generateGrammarURIWithDot() {
String actual = NoGrammarConstraintsCodeAction.getGrammarURI("file:///C:/.project", "xsd");
assertEquals("file:///C:/.project.xsd", actual);
}

}

0 comments on commit 1c9d3a3

Please sign in to comment.