private Document getW3cDoc(
      MortalLogger logger,
      DesignTimeUtils designTime,
      ResourceOracle resourceOracle,
      String templatePath)
      throws UnableToCompleteException {

    Resource resource = resourceOracle.getResourceMap().get(templatePath);
    if (null == resource) {
      logger.die("Unable to find resource: " + templatePath);
    }

    Document doc = null;
    try {
      String content = designTime.getTemplateContent(templatePath);
      if (content == null) {
        content = Util.readStreamAsString(resource.openContents());
      }
      doc =
          new W3cDomHelper(logger.getTreeLogger(), resourceOracle)
              .documentFor(content, resource.getPath());
    } catch (IOException iex) {
      logger.die("Error opening resource:" + resource.getLocation(), iex);
    } catch (SAXParseException e) {
      logger.die("Error parsing XML (line " + e.getLineNumber() + "): " + e.getMessage(), e);
    }
    return doc;
  }
Beispiel #2
0
 /**
  * Finds a JDT CUD for a given top-level type, generating it if needed.
  *
  * @param topType top-level JClassType
  * @return CUD instance or null if no source found
  */
 private synchronized CompilationUnitDeclaration getCudForTopLevelType(JClassType topType) {
   CompilationUnitDeclaration cud = null;
   if (cudCache.containsKey(topType)) {
     SoftReference<CompilationUnitDeclaration> cudRef = cudCache.get(topType);
     if (cudRef != null) {
       cud = cudRef.get();
     }
   }
   if (cud == null) {
     Resource classSource = classSources.get(topType);
     String source = null;
     if (classSource != null) {
       try {
         InputStream stream = classSource.openContents();
         source = Util.readStreamAsString(stream);
       } catch (IOException ex) {
         throw new InternalCompilerException(
             "Problem reading resource: " + classSource.getLocation(), ex);
       }
     }
     if (source == null) {
       // cache negative result so we don't try again
       cudCache.put(topType, null);
     } else {
       cud = parseJava(source);
       cudCache.put(topType, new SoftReference<CompilationUnitDeclaration>(cud));
     }
   }
   return cud;
 }
  /**
   * Build a new compilation state from a source oracle. Allow the caller to specify a compiler
   * delegate that will handle undefined names.
   *
   * <p>TODO: maybe use a finer brush than to synchronize the whole thing.
   */
  public synchronized CompilationState doBuildFrom(
      TreeLogger logger,
      CompilerContext compilerContext,
      Set<Resource> resources,
      AdditionalTypeProviderDelegate compilerDelegate)
      throws UnableToCompleteException {
    UnitCache unitCache = compilerContext.getUnitCache();

    // Units we definitely want to build.
    List<CompilationUnitBuilder> builders = new ArrayList<CompilationUnitBuilder>();

    // Units we don't want to rebuild unless we have to.
    Map<CompilationUnitBuilder, CompilationUnit> cachedUnits =
        new IdentityHashMap<CompilationUnitBuilder, CompilationUnit>();

    CompileMoreLater compileMoreLater = new CompileMoreLater(compilerContext, compilerDelegate);

    // For each incoming Java source file...
    for (Resource resource : resources) {
      // Create a builder for all incoming units.
      CompilationUnitBuilder builder = CompilationUnitBuilder.create(resource);

      CompilationUnit cachedUnit = unitCache.find(resource.getPathPrefix() + resource.getPath());

      // Try to rescue cached units from previous sessions where a jar has been
      // recompiled.
      if (cachedUnit != null && cachedUnit.getLastModified() != resource.getLastModified()) {
        unitCache.remove(cachedUnit);
        if (cachedUnit instanceof CachedCompilationUnit
            && cachedUnit.getContentId().equals(builder.getContentId())) {
          CachedCompilationUnit updatedUnit =
              new CachedCompilationUnit(
                  (CachedCompilationUnit) cachedUnit,
                  resource.getLastModified(),
                  resource.getLocation());
          unitCache.add(updatedUnit);
        } else {
          cachedUnit = null;
        }
      }
      if (cachedUnit != null) {
        cachedUnits.put(builder, cachedUnit);
        compileMoreLater.addValidUnit(cachedUnit);
        continue;
      }
      builders.add(builder);
    }
    if (logger.isLoggable(TreeLogger.TRACE)) {
      logger.log(
          TreeLogger.TRACE,
          "Found "
              + cachedUnits.size()
              + " cached/archived units.  Used "
              + cachedUnits.size()
              + " / "
              + resources.size()
              + " units from cache.");
    }

    Collection<CompilationUnit> resultUnits =
        compileMoreLater.compile(
            logger,
            compilerContext,
            builders,
            cachedUnits,
            CompilerEventType.JDT_COMPILER_CSB_FROM_ORACLE);

    boolean compileMonolithic = compilerContext.shouldCompileMonolithic();
    TypeOracle typeOracle = null;
    CompilationUnitTypeOracleUpdater typeOracleUpdater = null;
    if (compileMonolithic) {
      typeOracle = new TypeOracle();
      typeOracleUpdater = new CompilationUnitTypeOracleUpdater(typeOracle);
    } else {
      typeOracle = new LibraryTypeOracle(compilerContext);
      typeOracleUpdater = ((LibraryTypeOracle) typeOracle).getTypeOracleUpdater();
    }

    return new CompilationState(
        logger, compilerContext, typeOracle, typeOracleUpdater, resultUnits, compileMoreLater);
  }