All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fisco.bcos.web3j.solidity.compiler.SourceArtifact Maven / Gradle / Ivy

/*
 * Copyright (c) [2016] [  ]
 * This file is part of the ethereumJ library.
 *
 * The ethereumJ library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The ethereumJ library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the ethereumJ library. If not, see .
 */
package org.fisco.bcos.web3j.solidity.compiler;

import static java.lang.String.format;
import static java.util.Arrays.asList;
import static org.apache.commons.collections4.CollectionUtils.disjunction;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.substringsBetween;
import static org.fisco.bcos.web3j.solidity.compiler.ContractException.assembleError;

import java.io.File;
import java.util.*;

public class SourceArtifact {

  private String name;
  private List dependencies;
  private String source;

  private final Set injectedDependencies = new HashSet<>();
  private final Set dependentArtifacts = new HashSet<>();

  public SourceArtifact(String name, String source) {
    this.name = name;
    this.dependencies = extractDependencies(source);
    this.source = source.replaceAll("import\\s\"\\.*?\\.sol\";", "");
  }

  public SourceArtifact(File f) {}

  private static List extractDependencies(String source) {
    String[] deps = substringsBetween(source, "import \"", "\";");
    return deps == null ? Collections.emptyList() : asList(deps);
  }

  //    public SourceArtifact(MultipartFile srcFile) throws IOException {
  //        this(srcFile.getOriginalFilename(), new String(srcFile.getBytes(), "UTF-8"));
  //    }

  public void injectDependency(SourceArtifact srcArtifact) {
    injectedDependencies.add(srcArtifact);
    srcArtifact.addDependentArtifact(this);
  }

  private void addDependentArtifact(SourceArtifact srcArtifact) {
    dependentArtifacts.add(srcArtifact);
  }

  public boolean hasDependentArtifacts() {
    return !dependentArtifacts.isEmpty();
  }

  private Collection getUnresolvedDependencies() {
    Set ret = new HashSet<>();
    for (SourceArtifact injectedDependency : injectedDependencies) {
      ret.add(injectedDependency.getName());
    }

    return disjunction(dependencies, ret);
  }

  public String plainSource() {
    Collection unresolvedDeps = getUnresolvedDependencies();
    if (isNotEmpty(unresolvedDeps)) {
      throw assembleError("Followed dependencies aren't resolved: %s", unresolvedDeps);
    }

    String result = this.source;
    for (SourceArtifact dependencyArtifact : injectedDependencies) {
      String importDefinition = format("import \"%s\";", dependencyArtifact.getName());
      String dependencySrc =
          format("// %s\n%s", importDefinition, dependencyArtifact.plainSource());

      result = result.replace(importDefinition, dependencySrc);
    }

    return result;
  }

  public String getName() {
    return name;
  }

  public List getDependencies() {
    return dependencies;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy