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; }
/** * 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; }