| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| * |
| */ |
| |
| package org.eclipse.osbp.xtext.oxtype.imports; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.List; |
| |
| import org.eclipse.osbp.xtext.oxtype.oxtype.OXImportDeclaration; |
| import org.eclipse.osbp.xtext.oxtype.oxtype.OXtypeFactory; |
| import org.eclipse.xtext.conversion.IValueConverter; |
| import org.eclipse.xtext.conversion.ValueConverterException; |
| import org.eclipse.xtext.formatting.IWhitespaceInformationProvider; |
| import org.eclipse.xtext.resource.XtextResource; |
| import org.eclipse.xtext.xbase.conversion.XbaseQualifiedNameValueConverter; |
| import org.eclipse.xtext.xbase.imports.IImportsConfiguration; |
| import org.eclipse.xtext.xbase.imports.ImportSectionRegionUtil; |
| import org.eclipse.xtext.xbase.imports.RewritableImportSection; |
| import org.eclipse.xtext.xtype.XImportDeclaration; |
| import org.eclipse.xtext.xtype.XImportSection; |
| |
| import com.google.inject.Inject; |
| |
| @SuppressWarnings("restriction") |
| public class OXTypeRewritableImportSection extends RewritableImportSection { |
| |
| private IValueConverter<String> nameValueConverter; |
| private String lineSeparator; |
| |
| private List<String> plainImports = new ArrayList<>(); |
| private List<OXImportDeclaration> xaddedImportDeclarations = new ArrayList<>(); |
| |
| public OXTypeRewritableImportSection(XtextResource resource, |
| IImportsConfiguration importsConfiguration, |
| XImportSection originalImportSection, String lineSeparator, |
| ImportSectionRegionUtil regionUtil, |
| IValueConverter<String> nameConverter) { |
| super(resource, importsConfiguration, originalImportSection, |
| lineSeparator, regionUtil, nameConverter); |
| this.nameValueConverter = nameConverter; |
| this.lineSeparator = lineSeparator; |
| } |
| |
| /** |
| * Adds a namespace import. Eg |
| * <code>import ns org.my.datatypes.String</code> |
| * |
| * @param importx |
| */ |
| public boolean addFQNImport(String importx) { |
| if (plainImports.contains(importx)) |
| return false; |
| plainImports.add(importx); |
| OXImportDeclaration importDeclaration = OXtypeFactory.eINSTANCE |
| .createOXImportDeclaration(); |
| importDeclaration.setImportedNamespace(importx); |
| importDeclaration.setFqnImport(true); |
| xaddedImportDeclarations.add(importDeclaration); |
| return true; |
| |
| } |
| |
| @Override |
| protected StringBuilder getImportDeclarationsToAppend() { |
| StringBuilder builder = super.getImportDeclarationsToAppend(); |
| for (XImportDeclaration newImportDeclaration : xaddedImportDeclarations) { |
| appendImport(builder, newImportDeclaration); |
| } |
| return builder; |
| } |
| |
| @Override |
| protected void appendImport(StringBuilder builder, |
| XImportDeclaration newImportDeclaration) { |
| if (newImportDeclaration instanceof OXImportDeclaration) { |
| OXImportDeclaration importDecl = (OXImportDeclaration) newImportDeclaration; |
| if (importDecl.isFqnImport()) { |
| builder.append("import ns "); |
| try { |
| String escapedTypeName = nameValueConverter.toString(importDecl |
| .getImportedNamespace()); |
| builder.append(escapedTypeName); |
| builder.append(lineSeparator); |
| } catch (ValueConverterException e) { |
| e.printStackTrace(); |
| } |
| } |
| } else { |
| super.appendImport(builder, newImportDeclaration); |
| } |
| } |
| |
| @Override |
| protected String serializeImports(List<XImportDeclaration> allDeclarations) { |
| allDeclarations.addAll(xaddedImportDeclarations); |
| return super.serializeImports(allDeclarations); |
| } |
| |
| public static class Factory extends RewritableImportSection.Factory { |
| @Inject |
| private IImportsConfiguration importsConfiguration; |
| |
| @Inject |
| private IWhitespaceInformationProvider whitespaceInformationProvider; |
| |
| @Inject |
| private ImportSectionRegionUtil regionUtil; |
| |
| @Inject |
| private XbaseQualifiedNameValueConverter nameValueConverter; |
| |
| public RewritableImportSection parse(XtextResource resource) { |
| RewritableImportSection rewritableImportSection = new OXTypeRewritableImportSection( |
| resource, importsConfiguration, |
| importsConfiguration.getImportSection(resource), |
| whitespaceInformationProvider.getLineSeparatorInformation( |
| resource.getURI()).getLineSeparator(), regionUtil, |
| nameValueConverter); |
| return rewritableImportSection; |
| } |
| |
| public RewritableImportSection createNewEmpty(XtextResource resource) { |
| RewritableImportSection rewritableImportSection = new OXTypeRewritableImportSection( |
| resource, importsConfiguration, null, |
| whitespaceInformationProvider.getLineSeparatorInformation( |
| resource.getURI()).getLineSeparator(), regionUtil, |
| nameValueConverter); |
| rewritableImportSection.setSort(true); |
| return rewritableImportSection; |
| } |
| } |
| } |