/** * @param context in the form of /foo * @param parent in the form of /foo/bar/ or /foo/bar/dar.jss * @param scriptPath in the form of /foo/bar/mar.jss or bar/mar.jss * @return String[] with keys */ public static String[] getKeys(String context, String parent, String scriptPath) { String path; String normalizedScriptPath; context = context.equals("") ? "/" : context; normalizedScriptPath = scriptPath.startsWith("/") ? FilenameUtils.normalize(scriptPath, true) : FilenameUtils.normalize(FilenameUtils.getFullPath(parent) + scriptPath, true); path = FilenameUtils.getFullPath(normalizedScriptPath); // remove trailing "/" path = path.equals("/") ? path : path.substring(0, path.length() - 1); normalizedScriptPath = "/" + FilenameUtils.getName(normalizedScriptPath); return new String[] {context, path, normalizedScriptPath}; }
private Properties makeProperties() { Properties pluginProperties = new Properties(); try { File currentJarFile = new File( GraphvizPlugin.class .getProtectionDomain() .getCodeSource() .getLocation() .toURI() .getPath()); String currentJarPath = FilenameUtils.getFullPath(currentJarFile.getAbsolutePath()); File pluginPropertiesFile = new File(currentJarPath + "fitnesse-graphviz-plugin.properties"); if (pluginPropertiesFile.exists()) { InputStream is = new FileInputStream(pluginPropertiesFile); pluginProperties.load(is); is.close(); } } catch (Exception e) { // do nothing } if (pluginProperties.getProperty("dotExecutable") == null) { GraphvizDotFinder finder = new GraphvizDotFinder(); String dotExecutable = finder.findDot(); if (dotExecutable != null) { pluginProperties.setProperty("dotExecutable", dotExecutable); } } return pluginProperties; }
// public static void copyTifFiles(String origName, String tempUserTifDir, // String userName, LayerInfo info) { // String SLDFileName = origName + ".sld"; // File fileSLD = new File(tempUserTifDir, SLDFileName); // if (fileSLD.exists()) { // File filePublished = PublishUtility.copyFile(fileSLD, // tempUserTifDir, userName + "_" + info.name + ".sld", true); // fileSLD.delete(); // info.sld = this.publishSLD(filePublished, info.name); // } else { // info.sld = "default_raster"; // } // String TFWFileName = origName + ".tfw"; // File fileTFW = new File(tempUserTifDir, TFWFileName); // if (fileTFW.exists()) { // PublishUtility.copyFile(fileTFW, // tempUserTifDir, userName + "_" + info.name + ".tfw", true); // fileTFW.delete(); // } // String PRJFileName = origName + ".prj"; // File filePRJ = new File(tempUserTifDir, PRJFileName); // if (filePRJ.exists()) { // PublishUtility.copyFile(filePRJ, // tempUserTifDir, userName + "_" + info.name + ".prj", true); // filePRJ.delete(); // } // } public static File getFileNameToLowerCase(File file) { File fileToReturn = new File(FilenameUtils.getFullPath(file.getAbsolutePath()) + file.getName().toLowerCase()); try { FileUtils.moveFile(file, fileToReturn); } catch (IOException ex) { logger.error("Error renaming file: " + ex); } return fileToReturn; }
/** * Make the file, this is a temporary file which will be changed when the script is validated. * * @param script script. * @return OriginalFile tempfile.. * @throws ServerError */ private OriginalFile makeFile(final String path, final String script, Ice.Current current) throws ServerError { OriginalFile file = new OriginalFile(); file.setName(FilenameUtils.getName(path)); file.setPath(FilenameUtils.getFullPath(path)); file.setSize((long) script.getBytes().length); file.setHasher(new ChecksumAlgorithm("SHA1-160")); file.setHash(cpf.getProvider(ChecksumType.SHA1).putBytes(script.getBytes()).checksumAsString()); scripts.setMimetype(file); return updateFile(file, current); }
public static File convertRpmToZip(File file) throws Exception { LOG.info("File: " + file.getAbsolutePath()); FileInputStream fis = new FileInputStream(file); ReadableChannelWrapper in = new ReadableChannelWrapper(Channels.newChannel(fis)); InputStream uncompressed = new GZIPInputStream(fis); in = new ReadableChannelWrapper(Channels.newChannel(uncompressed)); String rpmZipName = file.getName(); rpmZipName = StringUtils.replace(rpmZipName, ".", "-"); rpmZipName = rpmZipName + ".zip"; String rpmZipPath = FilenameUtils.getFullPath(file.getAbsolutePath()); File rpmZipOutput = new File(rpmZipPath + File.separator + rpmZipName); LOG.info("Converting RPM: " + file.getName() + " to ZIP: " + rpmZipOutput.getName()); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(rpmZipOutput)); String rpmName = file.getName(); rpmName = StringUtils.replace(rpmName, ".", "-"); CpioHeader header; int total = 0; do { header = new CpioHeader(); total = header.read(in, total); if (header.getFileSize() > 0) { BoundedInputStream bis = new BoundedInputStream(uncompressed, header.getFileSize()); String relPath = FilenameUtils.separatorsToSystem(header.getName()); relPath = StringUtils.removeStart(relPath, "."); relPath = StringUtils.removeStart(relPath, "/"); relPath = rpmName + File.separator + relPath; relPath = StringUtils.replace(relPath, "\\", "/"); ZipEntry zipEntry = new ZipEntry(relPath); zos.putNextEntry(zipEntry); IOUtils.copy(bis, zos); } else { final int skip = header.getFileSize(); if (uncompressed.skip(skip) != skip) throw new RuntimeException("Skip failed."); } total += header.getFileSize(); } while (!header.isLast()); zos.flush(); zos.close(); return rpmZipOutput; }
/** {@inheritDoc} */ public InputStream locate(final String uri) throws IOException { Validate.notNull(uri, "URI cannot be NULL!"); LOG.debug("locate resource: {}", uri); try { if (getWildcardStreamLocator().hasWildcard(uri)) { final ServletContext servletContext = context.getServletContext(); final String fullPath = FilenameUtils.getFullPath(uri); final String realPath = servletContext.getRealPath(fullPath); if (realPath == null) { final String message = "[FAIL] determine realPath for resource: " + uri; LOG.debug(message); throw new IOException(message); } return getWildcardStreamLocator().locateStream(uri, new File(realPath)); } } catch (final IOException e) { /** * This is a special case when no more attempts are required, since the required computation * was achieved successfully. This solves the following <a * href="http://code.google.com/p/wro4j/issues/detail?id=321">issue</a>. * * <p>The problem was that in some situations, when the dispatcherStreamLocator was used to * locate resources containing wildcard, the following message was printed to the console: * <code>SEVERE: Servlet.service() for servlet default threw exception * java.io.FileNotFoundException.</code> */ if (e instanceof NoMoreAttemptsIOException) { throw e; } LOG.warn( "[FAIL] localize the stream containing wildcard. Original error message: '{}'", e.getMessage() + "\".\n Trying to locate the stream without the wildcard."); } InputStream inputStream = null; try { if (locatorStrategy.equals(LocatorStrategy.DISPATCHER_FIRST)) { inputStream = dispatcherFirstStreamLocator(uri); } else { inputStream = servletContextFirstStreamLocator(uri); } validateInputStreamIsNotNull(inputStream, uri); return inputStream; } catch (final IOException e) { LOG.debug("Wrong or empty resource with location: {}", uri); throw e; } }
/** * 将css文件里的图片相对路径修改为绝对路径 * * @return */ private static String repairCss(String content, String path) { Pattern p = Pattern.compile( "url\\([\\s]*['\"]?((?!['\"]?https?://|['\"]?data:|['\"]?/).*?)['\"]?[\\s]*\\)"); // 感谢Code Life(程式人生)的正则 Matcher m = p.matcher(content); StringBuffer sb = new StringBuffer(); while (m.find()) { String url = m.group(1).trim(); StringBuffer cssPath = new StringBuffer("url(").append(FilenameUtils.getFullPath(path)).append(url).append(")"); m.appendReplacement(sb, cssPath.toString()); } m.appendTail(sb); content = sb.toString(); return content; }
/** * Return supportFiles (if found) for the specified file * * @param filePath * @return */ public List<File> getSupportFiles(String filePath) { List<File> supportFiles = null; String parent = FilenameUtils.getFullPath(filePath); String mainName = FilenameUtils.getName(filePath); String baseName = FilenameUtils.removeExtension(mainName); for (String extension : supportingExtensions) { String newFilePath = parent + baseName + extension; File file = new File(newFilePath); if (file.exists()) { if (supportFiles == null) { supportFiles = new ArrayList<File>(); } supportFiles.add(file); } } return supportFiles; }
public void createDatabaseBackupOnUpgrade(String currentDbFile, int oldVersion) throws IOException { File in = new File(currentDbFile); String backupFileNameWithExtension = in.getName(); String backupName = FilenameUtils.getBaseName(backupFileNameWithExtension); String backupExtension = FilenameUtils.getExtension(backupFileNameWithExtension); // append last db version backupName += "_v" + Integer.toString(oldVersion); backupFileNameWithExtension = backupName + "." + backupExtension; String outPath = FilenameUtils.getFullPath(currentDbFile) + backupFileNameWithExtension; File out = new File(outPath); FileUtils.copyFile(in, out); }
/** * @return the {@link ClassLoader} for accessing gwt-dev classes mixed with design-time support * lib. */ private ClassLoader getDevClassLoader0() throws Exception { // prepare gwt-dev.jar location String devLibLocation = Utils.getDevLibLocation(moduleDescription); if (devLibLocation == null) { throw new HostedModeException(HostedModeException.NO_DEV_LIB); } String gwtLocation = FilenameUtils.getFullPath(devLibLocation); // add 'dev' & 'dev-designtime' ClassLoader devClassLoader = devClassLoaders.get(gwtLocation); if (devClassLoader == null) { URL resolvedDevLibUrl = new File(devLibLocation).toURI().toURL(); Bundle bundle = Activator.getDefault().getBundle(); URL devDesignUrl = FileLocator.resolve(bundle.getEntry("/gwt-dev-designtime.jar")); if (devDesignUrl != null) { // workaround for Issue 258 // (https://code.google.com/p/google-plugin-for-eclipse/issues/detail?id=258) devDesignUrl = new URL(StringUtils.replace(devDesignUrl.toString(), " ", "%20")); } devClassLoader = new URLClassLoader(new URL[] {devDesignUrl, resolvedDevLibUrl}, null); devClassLoaders.put(gwtLocation, devClassLoader); } return devClassLoader; }
/** * Reads in a file of HTML, primarily from the Help directory and fixes up the images to have an * absolute path. Plus it strip everything before and including the 'body' tag and strips the * '</body>' to the end. * * @param file the html file to be read. * @return the file as a string */ @SuppressWarnings("deprecation") // $NON-NLS-1$ public static String fixUpHTML(final File file) { String path = FilenameUtils.getFullPath(file.getAbsolutePath()); StringBuilder sb = new StringBuilder(); try { List<?> lines = FileUtils.readLines(file); boolean fndBegin = false; for (Object lineObj : lines) { String line = (String) lineObj; if (!fndBegin) { if (line.indexOf("<body") > -1) // $NON-NLS-1$ { fndBegin = true; } continue; } int inx = line.indexOf("<img"); // $NON-NLS-1$ if (inx > -1) { inx = line.indexOf("src=\"", inx); // $NON-NLS-1$ sb.append(line.substring(0, inx + 5)); File f = new File(path); sb.append(f.toURL()); // needed for 1.5 sb.append(line.substring(inx + 5, line.length())); } else { sb.append(line + "\n"); // $NON-NLS-1$ } } } catch (IOException ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(XMLHelper.class, ex); log.error(ex); } return sb.toString(); }
/* * The filename is the ProducID with either ".pdf" or ".epub" suffix. * Tje content files live in a parallel directory * <base>/<year>/Content/ * The XML file represented by the current cu would be something like: * <base>/<year>/DataFeed/EBSCOhostGKB_20160205_DELTA.zip!/EBSCOhostGKB_20160205_DELTA.xml * and the pdf would be * <base>/<year>/Content/123456.pdf */ @Override protected List<String> getFilenamesAssociatedWithRecord( SourceXmlSchemaHelper helper, CachedUrl cu, ArticleMetadata oneAM) { // this has been set to be the "ProductID" value String filenameValue = oneAM.getRaw(helper.getFilenameXPathKey()); String cuBase = FilenameUtils.getFullPath(cu.getUrl()); int datafeed_dir_start = cuBase.lastIndexOf("/DataFeed/"); // This will leave the "/", so just add back on the sibling_dir and filename String contentPath; if (datafeed_dir_start < 0) { // can't return null because that would make it okay to emit // this will fail to emit, as it should - we don't know how to verify the PDF existence log.siteWarning("The XML file lives at an unexpected location: " + cuBase); contentPath = CONTENT_DIR; // invalid but will force failure } else { contentPath = cuBase.substring(0, datafeed_dir_start) + CONTENT_DIR; } List<String> returnList = new ArrayList<String>(); returnList.add(contentPath + filenameValue + ".pdf"); returnList.add(contentPath + filenameValue + ".epub"); return returnList; }
private String configFilesPath() { return FilenameUtils.getFullPath(((AbstractFileConfiguration) config).getPath()); }
public String dirname() { return FilenameUtils.getFullPath(rel); }
/** * Perform the split transcripts mode. * * @throws IOException error reading / writing */ @Override public void execute() throws IOException { // Load the gene to transcripts file if (!config.validate()) { throw new IOException("Invalid SplitTranscripts configuration"); } final GeneTranscriptRelationships gtr = new GeneTranscriptRelationships(); final IndexedIdentifier transcriptIdents = new IndexedIdentifier(); final Int2ObjectMap<MutableString> transcriptIndexToIdMap = new Int2ObjectOpenHashMap<MutableString>(); final List<FastXEntry> fastxEntries = new LinkedList<FastXEntry>(); // // Pass through the file once to collect the transcript - gene relationships // int entryCount = 0; try { for (final FastXEntry entry : new FastXReader(config.getInputFile())) { entryCount++; parseHeader(entry.getEntryHeader()); final MutableString transcriptId = transcriptHeader.get("transcriptId"); final MutableString geneId = transcriptHeader.get("geneId"); final int transcriptIndex = transcriptIdents.registerIdentifier(transcriptId); gtr.addRelationship(geneId, transcriptIndex); transcriptIndexToIdMap.put(transcriptIndex, transcriptId); fastxEntries.add(entry.clone()); } } catch (CloneNotSupportedException e) { LOG.error("Couldn't clone for some reason", e); throw new GobyRuntimeException("Couldn't clone for some reason", e); } LOG.info("Loading map of genes-transcripts complete."); // // Scan through the transcript-gene relationships to determine which // transcript id goes into which file // final Int2IntMap transcriptIndex2FileIndex = new Int2IntOpenHashMap(); final String configOutputFilename = config.getOutputBase() + ".config"; final String configOutputPath = FilenameUtils.getFullPath(configOutputFilename); if (StringUtils.isNotBlank(configOutputPath)) { LOG.info("Creating output directory: " + configOutputPath); FileUtils.forceMkdir(new File(configOutputPath)); } PrintWriter configOutput = null; try { configOutput = new PrintWriter(configOutputFilename); configOutput.println("Ensembl Gene ID\tEnsembl Transcript ID"); final Int2IntMap fileIndex2NumberOfEntries = new Int2IntOpenHashMap(); fileIndex2NumberOfEntries.defaultReturnValue(0); transcriptIndex2FileIndex.defaultReturnValue(-1); final int initialNumberOfFiles = getNumberOfFiles(gtr, transcriptIndex2FileIndex); for (int geneIndex = 0; geneIndex < gtr.getNumberOfGenes(); geneIndex++) { final MutableString geneId = gtr.getGeneId(geneIndex); final IntSet transcriptIndices = gtr.getTranscriptSet(geneIndex); int fileNum = 0; for (final int transcriptIndex : transcriptIndices) { if (transcriptIndex2FileIndex.get(transcriptIndex) != -1) { LOG.warn("Skipping repeated transcriptIndex: " + transcriptIndex); continue; } final int maxEntriesPerFile = config.getMaxEntriesPerFile(); final int numberOfEntriesInOriginalBucket = fileIndex2NumberOfEntries.get(fileNum); final int adjustedFileIndex = fileNum + initialNumberOfFiles * (numberOfEntriesInOriginalBucket / maxEntriesPerFile); transcriptIndex2FileIndex.put(transcriptIndex, adjustedFileIndex); fileIndex2NumberOfEntries.put(fileNum, fileIndex2NumberOfEntries.get(fileNum) + 1); final MutableString transcriptId = transcriptIndexToIdMap.get(transcriptIndex); configOutput.printf("%s\t%s%n", geneId, transcriptId); fileNum++; } } } finally { IOUtils.closeQuietly(configOutput); } final int numFiles = getFileIndices(transcriptIndex2FileIndex).size(); if (LOG.isInfoEnabled()) { LOG.info( NumberFormat.getInstance().format(entryCount) + " entries will be written to " + numFiles + " files"); final int maxEntriesPerFile = config.getMaxEntriesPerFile(); if (maxEntriesPerFile < Integer.MAX_VALUE) { LOG.info("Each file will contain at most " + maxEntriesPerFile + " entries"); } } // formatter for uniquely numbering files each with the same number of digits final NumberFormat fileNumberFormatter = getNumberFormatter(numFiles - 1); final ProgressLogger progressLogger = new ProgressLogger(); progressLogger.expectedUpdates = entryCount; progressLogger.itemsName = "entries"; progressLogger.start(); // Write each file one at a time rather than in the order they appear in the input file // to avoid the issue of having too many streams open at the same or continually opening // and closing streams which is quite costly. We could store the gene/transcripts in // memory and then just write the files at the end but that could be worse. for (final int fileIndex : getFileIndices(transcriptIndex2FileIndex)) { final String filename = config.getOutputBase() + "." + fileNumberFormatter.format(fileIndex) + ".fa.gz"; PrintStream printStream = null; try { // each file is compressed printStream = new PrintStream(new GZIPOutputStream(new FileOutputStream(filename))); // // Read through the input file get the actual sequence information // final Iterator<FastXEntry> entries = fastxEntries.iterator(); while (entries.hasNext()) { final FastXEntry entry = entries.next(); parseHeader(entry.getEntryHeader()); final MutableString transcriptId = transcriptHeader.get("transcriptId"); final MutableString geneId = transcriptHeader.get("geneId"); final int transcriptIndex = transcriptIdents.getInt(transcriptId); final int transcriptFileIndex = transcriptIndex2FileIndex.get(transcriptIndex); if (transcriptFileIndex == fileIndex) { printStream.print(entry.getHeaderSymbol()); printStream.print(transcriptId); printStream.print(" gene:"); printStream.println(geneId); printStream.println(entry.getEntrySansHeader()); entries.remove(); progressLogger.lightUpdate(); } } } finally { IOUtils.closeQuietly(printStream); } } assert progressLogger.count == entryCount : "Some entries were not processed!"; progressLogger.done(); }
/** * Create the xml file for the set of collections. * * @param xmlFile - file to write the xml to * @param contributor types - set of contributor types to export * @throws IOException - if writing to the file fails. */ public void createXmlFile(File xmlFile, Collection<ContributorType> contributorTypes) throws IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; String path = FilenameUtils.getPath(xmlFile.getCanonicalPath()); if (!path.equals("")) { File pathOnly = new File(FilenameUtils.getFullPath(xmlFile.getCanonicalPath())); FileUtils.forceMkdir(pathOnly); } if (!xmlFile.exists()) { if (!xmlFile.createNewFile()) { throw new IllegalStateException("could not create file"); } } try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } DOMImplementation impl = builder.getDOMImplementation(); DOMImplementationLS domLs = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer serializer = domLs.createLSSerializer(); LSOutput lsOut = domLs.createLSOutput(); Document doc = impl.createDocument(null, "contributor_types", null); Element root = doc.getDocumentElement(); FileOutputStream fos; OutputStreamWriter outputStreamWriter; BufferedWriter writer; try { fos = new FileOutputStream(xmlFile); try { outputStreamWriter = new OutputStreamWriter(fos, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } writer = new BufferedWriter(outputStreamWriter); lsOut.setCharacterStream(writer); } catch (FileNotFoundException e) { throw new IllegalStateException(e); } // create XML for the child collections for (ContributorType ct : contributorTypes) { Element contributorType = doc.createElement("contributor_type"); this.addIdElement(contributorType, ct.getId().toString(), doc); this.addNameElement(contributorType, ct.getName(), doc); this.addDescription(contributorType, ct.getDescription(), doc); this.addSystemCode(contributorType, ct.getUniqueSystemCode(), doc); } serializer.write(root, lsOut); try { fos.close(); writer.close(); outputStreamWriter.close(); } catch (Exception e) { throw new IllegalStateException(e); } }
public static String getPath(File f) { return FilenameUtils.getFullPath(f.getAbsolutePath()) + FilenameUtils.getName(f.getName()); }