protected void determineProjectName() throws ASExternalImporterException { String exceptionMessage = null; if (projectType == ProjectType.IPR) { // We are sure that determineProjectType() didn't throw exception, so iprFile is not null and // it represents *.ipr file projectName = iprFile.getName().replaceFirst("\\.ipr$", ""); } else if (projectType == ProjectType.IDEA) { File nameFile = new File(ideaDir.getPath(), ".name"); try { BufferedReader reader = new BufferedReader(new FileReader(nameFile)); String name = reader.readLine(); if (StringUtils.isWhitespace(name)) { exceptionMessage = ".name file in path " + nameFile.getPath() + " is empty"; } projectName = name.trim(); } catch (FileNotFoundException e) { exceptionMessage = "Cannot find .name file in path " + nameFile.getPath(); } catch (IOException e) { exceptionMessage = "Cannot read .name file in path " + nameFile.getPath(); } } else { // Actually unreachable (see setProject()) exceptionMessage = "Project type was not determined"; } if (exceptionMessage != null) { resetStateAndThrowException(exceptionMessage); } }
@Override protected void addRunConfigurations() { runConfigurations.clear(); // Shared configurations if (projectType == ProjectType.IDEA) { File runConfigDir = new File(ideaDir.getPath() + "/runConfigurations"); if (runConfigDir.isDirectory()) { String[] runConfigurationNames = runConfigDir.list( new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".xml"); } }); for (String s : runConfigurationNames) { try { runConfigurations.add(new ASIdeaRunConfiguration(runConfigDir.getPath() + "/" + s)); } catch (ASExternalImporterException e) { // Ignore } } } } else if (projectType == ProjectType.IPR) { Element iprTop; try { iprTop = getTopLevelElement(iprFile); } catch (ASExternalImporterException e) { return; } addRunConfigurationFromXMLTree(iprTop, "ProjectRunConfigurationManager"); } // Non-shared configurations Element workspaceTop; try { if (projectType == ProjectType.IDEA) { workspaceTop = getTopLevelElement(new File(ideaDir.getPath() + File.separator + "workspace.xml")); } else if (projectType == ProjectType.IPR) { workspaceTop = getTopLevelElement(iwsFile); } else { // O_o return; } } catch (ASExternalImporterException e) { return; } addRunConfigurationFromXMLTree(workspaceTop, "RunManager"); }
private void processDelete(Node operation) { String path = absolutePath(operation.getTextContent()); File target = new File(path); if (!target.exists()) { Utils.onError(new Error.FileNotFound(target.getPath())); return; } try { if (target.isDirectory()) { FileUtils.deleteDirectory(target); } else { FileUtils.forceDelete(target); } } catch (IOException ex) { Utils.onError(new Error.FileDelete(target.getPath())); } }
/** * @param sourceFile File to read from * @return List of String objects with the shas */ public static FileRequestFileContent readRequestFile(final File sourceFile) { if (!sourceFile.isFile() || !(sourceFile.length() > 0)) { return null; } Document d = null; try { d = XMLTools.parseXmlFile(sourceFile.getPath()); } catch (final Throwable t) { logger.log(Level.SEVERE, "Exception in readRequestFile, during XML parsing", t); return null; } if (d == null) { logger.log(Level.SEVERE, "Could'nt parse the request file"); return null; } final Element rootNode = d.getDocumentElement(); if (rootNode.getTagName().equals(TAG_FrostFileRequestFile) == false) { logger.severe( "Error: xml request file does not contain the root tag '" + TAG_FrostFileRequestFile + "'"); return null; } final String timeStampStr = XMLTools.getChildElementsTextValue(rootNode, TAG_timestamp); if (timeStampStr == null) { logger.severe("Error: xml file does not contain the tag '" + TAG_timestamp + "'"); return null; } final long timestamp = Long.parseLong(timeStampStr); final List<Element> nodelist = XMLTools.getChildElementsByTagName(rootNode, TAG_shaList); if (nodelist.size() != 1) { logger.severe("Error: xml request files must contain only one element '" + TAG_shaList + "'"); return null; } final Element rootShaNode = nodelist.get(0); final List<String> shaList = new LinkedList<String>(); final List<Element> xmlKeys = XMLTools.getChildElementsByTagName(rootShaNode, TAG_sha); for (final Element el : xmlKeys) { final Text txtname = (Text) el.getFirstChild(); if (txtname == null) { continue; } final String sha = txtname.getData(); shaList.add(sha); } final FileRequestFileContent content = new FileRequestFileContent(timestamp, shaList); return content; }
public static void main(final String[] args) { final File targetFile = new File("D:\\abc.def"); final File tmp = new File(targetFile.getPath() + ".frftmp"); if (!FileAccess.compressFileGZip(targetFile, tmp)) { return; // error } targetFile.delete(); tmp.renameTo(targetFile); }
private void processXml(Node operation) throws ParserConfigurationException, TransformerConfigurationException { List<Node> targets = getChildNodes(operation, "target"); List<Node> appendOpNodes = getChildNodes(operation, "append"); List<Node> setOpNodes = getChildNodes(operation, "set"); List<Node> replaceOpNodes = getChildNodes(operation, "replace"); List<Node> removeOpNodes = getChildNodes(operation, "remove"); if (targets.isEmpty()) { return; } for (int t = 0; t < targets.size(); t++) { File target = new File(absolutePath(targets.get(t).getTextContent())); if (!target.exists()) { Utils.onError(new Error.FileNotFound(target.getPath())); return; } DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = null; try { doc = db.parse(target); } catch (Exception ex) { Utils.onError(new Error.FileParse(target.getPath())); return; } for (int i = 0; i < removeOpNodes.size(); i++) removeXmlEntry(doc, removeOpNodes.get(i)); for (int i = 0; i < replaceOpNodes.size(); i++) replaceXmlEntry(doc, replaceOpNodes.get(i)); for (int i = 0; i < setOpNodes.size(); i++) setXmlEntry(doc, setOpNodes.get(i)); for (int i = 0; i < appendOpNodes.size(); i++) appendXmlEntry(doc, appendOpNodes.get(i)); try { OutputFormat format = new OutputFormat(doc); format.setOmitXMLDeclaration(true); format.setLineWidth(65); format.setIndenting(true); format.setIndent(4); Writer out = new FileWriter(target); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(doc); } catch (IOException e) { Utils.onError(new Error.WriteXmlConfig(target.getPath())); } } }
private void processCopy(Node operation) { List<Node> fromNode = getChildNodes(operation, "from"); List<Node> toNode = getChildNodes(operation, "to"); if (fromNode.isEmpty() || toNode.isEmpty()) { return; } String path = fromNode.get(0).getTextContent(); String fromPath = (new File(path).isAbsolute()) ? path : absolutePath(path); String toPath = absolutePath(toNode.get(0).getTextContent()); Boolean copyContents = fromPath.endsWith(File.separator + "*"); File from = new File((copyContents) ? fromPath.substring(0, fromPath.length() - 2) : fromPath); File to = new File(toPath); if (!from.exists()) { Utils.onError(new Error.FileNotFound(from.getPath())); return; } try { if (copyContents) { FileUtils.forceMkdir(to); FileUtils.copyDirectory(from, to); } else if (from.isDirectory()) { FileUtils.forceMkdir(to); FileUtils.copyDirectoryToDirectory(from, to); } else { if (to.isDirectory()) { FileUtils.forceMkdir(to); FileUtils.copyFileToDirectory(from, to); } else { File toDir = new File(Utils.getParentDir(to)); FileUtils.forceMkdir(toDir); FileUtils.copyFile(from, to); } } } catch (IOException ex) { Utils.onError(new Error.FileCopy(from.getPath(), to.getPath())); } }
private Element getTopLevelElement(File file) throws ASExternalImporterException { BufferedReader reader; try { reader = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { throw new ASExternalImporterException("Cannot find " + file.getPath()); } DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); Document doc; try { doc = fact.newDocumentBuilder().parse(new InputSource(reader)); } catch (Exception e) { throw new ASExternalImporterException("Corrupted file: " + file.getPath()); } Element el = doc.getDocumentElement(); if (el == null) { throw new ASExternalImporterException("Corrupted file: " + file.getPath()); } return el; }
private void processTxt(Node operation) { List<Node> targets = getChildNodes(operation, "target"); List<Node> optionNodes = getChildNodes(operation, "opt"); List<Node> separatorNode = getChildNodes(operation, "separator"); if (targets.isEmpty() || optionNodes.isEmpty()) { return; } String defaultSeparator = "="; String globalSeparator = defaultSeparator; if (!separatorNode.isEmpty()) { globalSeparator = separatorNode.get(0).getTextContent(); if (globalSeparator.length() != 1) { globalSeparator = defaultSeparator; } } Map<String, String> options = new HashMap<String, String>(); Map<String, String> processedOptions = new HashMap<String, String>(); for (int i = 0; i < optionNodes.size(); i++) { Node option = optionNodes.get(i); String name = option.getAttributes().getNamedItem("name").getNodeValue(); String value = option.getTextContent(); if (options.containsKey(name)) { options.remove(name); } options.put(name, value); } for (int t = 0; t < targets.size(); t++) { File target = new File(absolutePath(targets.get(t).getTextContent())); File tmpFile = new File(Utils.timestamp()); BufferedWriter bw = null; BufferedReader br = null; try { Node separatorAttr = targets.get(t).getAttributes().getNamedItem("separator"); String separator = (separatorAttr == null) ? globalSeparator : separatorAttr.getNodeValue(); if (separator.length() != 1) { separator = globalSeparator; } bw = new BufferedWriter(new FileWriter(tmpFile)); if (target.exists()) { br = new BufferedReader(new FileReader(target)); for (String line; (line = br.readLine()) != null; ) { String[] parts = line.split(separator); if (parts.length < 2) { bw.write(line); bw.newLine(); continue; } String optName = parts[0].trim(); if (options.containsKey(optName)) { String optValue = options.get(optName); bw.write(optName + " " + separator + " " + optValue); bw.newLine(); processedOptions.put(optName, optValue); options.remove(optName); } else if (processedOptions.containsKey(optName)) { bw.write(optName + " " + separator + " " + processedOptions.get(optName)); bw.newLine(); } else { bw.write(line); bw.newLine(); } } br.close(); } for (Map.Entry<String, String> entry : options.entrySet()) { bw.write(entry.getKey() + " " + separator + " " + entry.getValue()); bw.newLine(); } bw.close(); FileUtils.copyFile(tmpFile, target); FileUtils.forceDelete(tmpFile); } catch (IOException ex) { Utils.onError(new Error.WriteTxtConfig(target.getPath())); } } }
public void setFile(File file) { elt.setAttribute("file", file.getPath()); }
protected void locateModules() throws ASExternalImporterException { modules.clear(); File f; String fDescription; // Both .ipr and modules.xml files have the same structure if (projectType == ProjectType.IPR) { f = iprFile; fDescription = "project"; } else if (projectType == ProjectType.IDEA) { f = new File(ideaDir.getPath() + File.separator + "modules.xml"); fDescription = "modules"; } else { // Unreachable throw new ASExternalImporterException("Project type was not determined"); } BufferedReader reader; try { reader = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { // Unreachable throw new ASExternalImporterException("Cannot find " + fDescription + " file " + f.getPath()); } DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance(); Document doc; try { doc = fact.newDocumentBuilder().parse(new InputSource(reader)); } catch (Exception e) { throw new ASExternalImporterException("Corrupted " + fDescription + " file " + f.getPath()); } Element el = doc.getDocumentElement(); if (el == null) { throw new ASExternalImporterException("Corrupted " + fDescription + " file " + f.getPath()); } NodeList list = el.getChildNodes(); Node moduleNode = null; for (int i = 0; i < list.getLength(); i++) { NamedNodeMap nodeMap = list.item(i).getAttributes(); if (nodeMap != null && nodeMap.getNamedItem("name") != null) { if ("ProjectModuleManager".equals(nodeMap.getNamedItem("name").getNodeValue())) { moduleNode = list.item(i); break; } } } if (moduleNode == null) { throw new ASExternalImporterException("Corrupted " + fDescription + " file " + f.getPath()); } NodeList moduleNodeList = null; for (int i = 0; i < moduleNode.getChildNodes().getLength(); i++) { if ("modules".equals(moduleNode.getChildNodes().item(i).getNodeName())) { moduleNodeList = moduleNode.getChildNodes().item(i).getChildNodes(); break; } } if (moduleNodeList == null) { throw new ASExternalImporterException("Corrupted " + fDescription + " file " + f.getPath()); } for (int i = 0; i < moduleNodeList.getLength(); i++) { NamedNodeMap nodeMap = moduleNodeList.item(i).getAttributes(); if (nodeMap != null && nodeMap.getNamedItem("filepath") != null) { modules.add( new ASIdeaModule( nodeMap .getNamedItem("filepath") .getNodeValue() .replace("$PROJECT_DIR$", projectPath))); } } resolveModuleDependencies(); }