Skip to content

Commit

Permalink
Issue#316 : ParserFileHelperFactory Class Refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
kapilgahlot1998 authored and apupier committed Jan 24, 2024
1 parent 37ef630 commit 1d2faaa
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package com.github.cameltooling.lsp.internal.parser;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import com.github.cameltooling.lsp.internal.parser.fileparserhelper.ParserChainOfResponsibility;
import org.eclipse.lsp4j.TextDocumentItem;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class ParserFileHelperFactory {

private static final String KUBERNETES_CRD_API_VERSION_CAMEL = "apiVersion: camel.apache.org/";
Expand All @@ -37,37 +37,7 @@ public class ParserFileHelperFactory {
private static final String SHEBANG_CAMEL_K = "#!/usr/bin/env camel-k";

public ParserFileHelper getCorrespondingParserFileHelper(TextDocumentItem textDocumentItem, int line) {
ParserXMLFileHelper xmlParser = new ParserXMLFileHelper();
String uri = textDocumentItem.getUri();
if (uri.endsWith(".xml") && xmlParser.getCorrespondingCamelNodeForCompletion(textDocumentItem, line) != null) {
return xmlParser;
} else if(isPotentiallyCamelJavaDSL(textDocumentItem, uri)) {
ParserJavaFileHelper javaParser = new ParserJavaFileHelper();
if (javaParser.getCorrespondingMethodName(textDocumentItem, line) != null) {
return javaParser;
}
} else if(isCamelKGroovyDSL(textDocumentItem, uri)) {
CamelKGroovyDSLParser camelKGroovyDSLParser = new CamelKGroovyDSLParser();
if (camelKGroovyDSLParser.getCorrespondingMethodName(textDocumentItem, line) != null) {
return camelKGroovyDSLParser;
}
} else if(isCamelYamlDSL(textDocumentItem, uri)) {
CamelYamlDSLParser camelKYamlDSLParser = new CamelYamlDSLParser();
if (camelKYamlDSLParser.getCorrespondingType(textDocumentItem, line) != null) {
return camelKYamlDSLParser;
}
} else if(isCamelKKotlinDSL(textDocumentItem, uri)) {
CamelKKotlinDSLParser camelKKotlinDSLParser = new CamelKKotlinDSLParser();
if (camelKKotlinDSLParser.getCorrespondingMethodName(textDocumentItem, line) != null) {
return camelKKotlinDSLParser;
}
} else if(isCamelKJSDSL(textDocumentItem, uri)) {
CamelKJSDSLParser camelKJSDSLParser = new CamelKJSDSLParser();
if (camelKJSDSLParser.getCorrespondingMethodName(textDocumentItem, line) != null) {
return camelKJSDSLParser;
}
}
return null;
return ParserChainOfResponsibility.getMatchedParserFileHelper(textDocumentItem, line, this);
}

/**
Expand All @@ -93,7 +63,7 @@ private boolean isCamelXMLDSL(TextDocumentItem textDocumentItem, String uri) {
}
}

private boolean isCamelKJSDSL(TextDocumentItem textDocumentItem, String uri) {
public boolean isCamelKJSDSL(TextDocumentItem textDocumentItem, String uri) {
//improve this method to provide better heuristic to detect if it is a Camel file or not
return uri.endsWith(CAMELK_JS_FILENAME_SUFFIX)
|| isJSFileWithCamelKModelineLike(textDocumentItem, uri);
Expand All @@ -103,7 +73,7 @@ private boolean isJSFileWithCamelKModelineLike(TextDocumentItem textDocumentItem
return uri.endsWith(".js") && textDocumentItem.getText().startsWith(CamelKModelineParser.MODELINE_LIKE_CAMEL_K);
}

private boolean isCamelKKotlinDSL(TextDocumentItem textDocumentItem, String uri) {
public boolean isCamelKKotlinDSL(TextDocumentItem textDocumentItem, String uri) {
//improve this method to provide better heuristic to detect if it is a Camel file or not
return uri.endsWith(CAMELK_KOTLIN_FILENAME_SUFFIX)
|| isKotlinFileWithCamelKModelineLike(textDocumentItem, uri);
Expand All @@ -113,7 +83,7 @@ private boolean isKotlinFileWithCamelKModelineLike(TextDocumentItem textDocument
return uri.endsWith(".kts") && textDocumentItem.getText().startsWith(CamelKModelineParser.MODELINE_LIKE_CAMEL_K);
}

private boolean isCamelKGroovyDSL(TextDocumentItem textDocumentItem, String uri) {
public boolean isCamelKGroovyDSL(TextDocumentItem textDocumentItem, String uri) {
//improve this method to provide better heuristic to detect if it is a Camel file or not
return uri.endsWith(CAMELK_GROOVY_FILENAME_SUFFIX)
|| isGroovyFileWithCamelKShebang(textDocumentItem, uri)
Expand All @@ -128,7 +98,7 @@ protected boolean isGroovyFileWithCamelKShebang(TextDocumentItem textDocumentIte
return uri.endsWith(".groovy") && textDocumentItem.getText().startsWith(SHEBANG_CAMEL_K);
}

private boolean isCamelYamlDSL(TextDocumentItem textDocumentItem, String uri) {
public boolean isCamelYamlDSL(TextDocumentItem textDocumentItem, String uri) {
//improve this method to provide better heuristic to detect if it is a Camel file or not
return uri.endsWith(CAMELK_YAML_FILENAME_SUFFIX)
|| uri.endsWith(CAMELK_YML_FILENAME_SUFFIX)
Expand Down Expand Up @@ -156,7 +126,7 @@ protected boolean isYamlFileWithCamelKShebang(TextDocumentItem textDocumentItem,
return hasYamlExtension(uri) && textDocumentItem.getText().startsWith(SHEBANG_CAMEL_K);
}

private boolean isPotentiallyCamelJavaDSL(TextDocumentItem textDocumentItem, String uri) {
public boolean isPotentiallyCamelJavaDSL(TextDocumentItem textDocumentItem, String uri) {
//improve this method to provide better heuristic to detect if it is a Camel file or not
return uri.endsWith(".java") && textDocumentItem.getText().contains("camel");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* https://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 com.github.cameltooling.lsp.internal.parser.fileparserhelper;

import com.github.cameltooling.lsp.internal.parser.CamelKGroovyDSLParser;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelper;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelperFactory;
import org.eclipse.lsp4j.TextDocumentItem;

public class CamelKGroovyDSLFileParser extends Parser {

private final Parser nextFileParser;

CamelKGroovyDSLFileParser(Parser parser) {
this.nextFileParser = parser;
}

@Override
public ParserFileHelper getMatchedFileParser(TextDocumentItem textDocumentItem, int line, ParserFileHelperFactory parserFileHelperFactory) {
String uri = textDocumentItem.getUri();
if (parserFileHelperFactory.isCamelKGroovyDSL(textDocumentItem, uri)) {
CamelKGroovyDSLParser camelKGroovyDSLParser = new CamelKGroovyDSLParser();
if (camelKGroovyDSLParser.getCorrespondingMethodName(textDocumentItem, line) != null) {
return camelKGroovyDSLParser;
}
} else if (nextFileParser != null) {
return nextFileParser.getMatchedFileParser(textDocumentItem, line, parserFileHelperFactory);
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* https://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 com.github.cameltooling.lsp.internal.parser.fileparserhelper;

import com.github.cameltooling.lsp.internal.parser.CamelKJSDSLParser;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelper;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelperFactory;
import org.eclipse.lsp4j.TextDocumentItem;

public class CamelKJSDSLFileParser extends Parser {

private final Parser nextFileParser;

CamelKJSDSLFileParser(Parser parser) {
this.nextFileParser = parser;
}

@Override
public ParserFileHelper getMatchedFileParser(TextDocumentItem textDocumentItem, int line, ParserFileHelperFactory parserFileHelperFactory) {
String uri = textDocumentItem.getUri();
if (parserFileHelperFactory.isCamelKJSDSL(textDocumentItem, uri)) {
CamelKJSDSLParser camelKJSDSLParser = new CamelKJSDSLParser();
if (camelKJSDSLParser.getCorrespondingMethodName(textDocumentItem, line) != null) {
return camelKJSDSLParser;
}
} else if (nextFileParser != null) {
return nextFileParser.getMatchedFileParser(textDocumentItem, line, parserFileHelperFactory);
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* https://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 com.github.cameltooling.lsp.internal.parser.fileparserhelper;

import com.github.cameltooling.lsp.internal.parser.CamelKKotlinDSLParser;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelper;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelperFactory;
import org.eclipse.lsp4j.TextDocumentItem;

public class CamelKKotlinDSLFileParser extends Parser {

private final Parser nextFileParser;

CamelKKotlinDSLFileParser(Parser parser) {
this.nextFileParser = parser;
}

@Override
public ParserFileHelper getMatchedFileParser(TextDocumentItem textDocumentItem, int line, ParserFileHelperFactory parserFileHelperFactory) {
String uri = textDocumentItem.getUri();
if (parserFileHelperFactory.isCamelKKotlinDSL(textDocumentItem, uri)) {
CamelKKotlinDSLParser camelKKotlinDSLParser = new CamelKKotlinDSLParser();
if (camelKKotlinDSLParser.getCorrespondingMethodName(textDocumentItem, line) != null) {
return camelKKotlinDSLParser;
}
} else if (nextFileParser != null) {
return nextFileParser.getMatchedFileParser(textDocumentItem, line, parserFileHelperFactory);
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* https://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 com.github.cameltooling.lsp.internal.parser.fileparserhelper;

import com.github.cameltooling.lsp.internal.parser.CamelYamlDSLParser;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelper;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelperFactory;
import org.eclipse.lsp4j.TextDocumentItem;

public class CamelYamlDSLFileParser extends Parser {

private final Parser nextFileParser;

CamelYamlDSLFileParser(Parser parser) {
this.nextFileParser = parser;
}

@Override
public ParserFileHelper getMatchedFileParser(TextDocumentItem textDocumentItem, int line, ParserFileHelperFactory parserFileHelperFactory) {
String uri = textDocumentItem.getUri();
if (parserFileHelperFactory.isCamelYamlDSL(textDocumentItem, uri)) {
CamelYamlDSLParser camelKYamlDSLParser = new CamelYamlDSLParser();
if (camelKYamlDSLParser.getCorrespondingType(textDocumentItem, line) != null) {
return camelKYamlDSLParser;
}
} else if (nextFileParser != null) {
return nextFileParser.getMatchedFileParser(textDocumentItem, line, parserFileHelperFactory);
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* https://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 com.github.cameltooling.lsp.internal.parser.fileparserhelper;

import com.github.cameltooling.lsp.internal.parser.ParserFileHelper;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelperFactory;
import com.github.cameltooling.lsp.internal.parser.ParserJavaFileHelper;
import org.eclipse.lsp4j.TextDocumentItem;

public class JavaFileParser extends Parser {

private final Parser nextFileParser;

JavaFileParser(Parser parser) {
this.nextFileParser = parser;
}

@Override
public ParserFileHelper getMatchedFileParser(TextDocumentItem textDocumentItem, int line, ParserFileHelperFactory parserFileHelperFactory) {
String uri = textDocumentItem.getUri();
if (parserFileHelperFactory.isPotentiallyCamelJavaDSL(textDocumentItem, uri)) {
ParserJavaFileHelper javaParser = new ParserJavaFileHelper();
if (javaParser.getCorrespondingMethodName(textDocumentItem, line) != null) {
return javaParser;
}
} else if (nextFileParser != null) {
return nextFileParser.getMatchedFileParser(textDocumentItem, line, parserFileHelperFactory);
}

return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* https://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 com.github.cameltooling.lsp.internal.parser.fileparserhelper;

import com.github.cameltooling.lsp.internal.parser.ParserFileHelper;
import com.github.cameltooling.lsp.internal.parser.ParserFileHelperFactory;
import org.eclipse.lsp4j.TextDocumentItem;

public abstract class Parser {

public abstract ParserFileHelper getMatchedFileParser(TextDocumentItem textDocumentItem, int line,
ParserFileHelperFactory parserFileHelperFactory);

}
Loading

0 comments on commit 1d2faaa

Please sign in to comment.