private void deflate(String tmpDir, String path) { String tmpFile = "tmp-" + Utils.timestamp() + ".zip"; try { ZipFile zipFile = new ZipFile(tmpFile); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setIncludeRootFolder(false); zipFile.addFolder(tmpDir, parameters); } catch (Exception e) { e.printStackTrace(); return; } File from = null; File to = null; try { File target = new File(path); if (target.exists()) FileUtils.forceDelete(target); from = new File(tmpFile); to = new File(path); FileUtils.moveFile(from, to); } catch (IOException e) { Utils.onError(new Error.FileMove(tmpFile, path)); } try { FileUtils.deleteDirectory(new File(tmpDir)); } catch (IOException e) { Utils.log("can't delete temporary folder"); } }
private void call(Node operation) throws ParserConfigurationException, TransformerConfigurationException { String opName = operation.getNodeName(); if (opName.equals("call")) { String script = Utils.combine(Utils.getParentDir(this.currentScript), operation.getTextContent()); call(script); } else if (opName.equals("apply")) processApply(operation); else if (opName.equals("xml")) processXml(operation); else if (opName.equals("txt")) processTxt(operation); else if (opName.equals("copy")) processCopy(operation); else if (opName.equals("delete")) processDelete(operation); }
private String inflate(String path) { if (!new File(path).canRead()) { Utils.onError(new Error.FileRead(path)); return null; } String tmpDir = "tmp-" + Utils.timestamp(); try { ZipFile zipFile = new ZipFile(path); zipFile.extractAll(tmpDir); } catch (Exception e) { e.printStackTrace(); return null; } return tmpDir; }
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())); } }
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())); } }
public void updateBean(AlbumBean newBean, AlbumBean oldBean) throws LibraryException { synchronized (libraryList) { // Find the bean int index = libraryList.indexOf(oldBean); if (index == -1) { LibraryException le = new LibraryException(Utils.getResourceString(ERROR_BEAN_NOT_FOUND)); throw le; } // Set the new bean libraryList.set(index, newBean); setChanged(); fireLibraryChangeEvent( new LibraryChangeEvent(this, new AlbumBean[] {oldBean}, LibraryChangeEvent.UPDATE)); } }
void parseConnectors(NodeList list) { int len = list.getLength(); for (int i = 0; i < len; i++) { if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { Element node = (Element) list.item(i); String name = node.getAttribute("name"); String desc = node.getAttribute("description"); String cls = node.getAttribute("class"); String mode = node.getAttribute("mode"); String catg = node.getAttribute("category"); String icon = null; NodeList icontag = node.getElementsByTagName("icon"); if (icontag.getLength() > 0) icon = ((Element) icontag.item(0)).getAttribute("url"); ConnectorDesc nd = new ConnectorDesc(name, desc, cls, mode); nd.setIcon(icon); nd.setCategory(catg); formalism.addConnectorDesc(nd); NodeList graphic = node.getElementsByTagName("graphic-element"); if (graphic.getLength() > 0) { // System.err.println("nodeselement-"+name+" "+nd.getGraphicClass()); String graphicClass = ((Element) graphic.item(0)).getAttribute("class"); if (!graphicClass.equals("")) nd.setGraphicClass(graphicClass); // System.err.println("nodeselement+"+name+" "+nd.getGraphicClass()); NodeList proplist = ((Element) graphic.item(0)).getElementsByTagName("property"); if (proplist.getLength() > 0) nd.setGraphicProperties(getProperties(proplist)); // System.err.println("IN PROPERTY NDGR"+proplist.getLength()+" // "+nd.getGraphicProperties()); node.removeChild(graphic.item(0)); } NodeList proplist = node.getElementsByTagName("property"); if (proplist.getLength() > 0) try { // System.err.println("IN PROPERTY ND"+proplist); ReflectorUtil.setProperties(nd, getProperties(proplist)); } catch (Exception e) { Utils.log("setProperties failed:" + e); } } } }
private NodeList findNodes(Document doc, Node operation) { List<Node> xpaths = getChildNodes(operation, "xpath"); if (xpaths.isEmpty()) { return null; } String xpathExpression = xpaths.get(0).getTextContent(); if (xpathExpression == null) { return null; } XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); NodeList nl = null; try { XPathExpression expr = xpath.compile(xpathExpression); nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); } catch (XPathExpressionException ex) { Utils.onError(new Error.WrongXpathExpression(xpathExpression)); } return nl; }
private void call(String script) throws ParserConfigurationException, TransformerConfigurationException { File callerScript = this.currentScript; Document doc = null; try { this.currentScript = new File(script); DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = db.parse(this.currentScript); } catch (Exception ex) { this.currentScript = callerScript; Utils.onError(new Error.FileParse(script)); return; } NodeList operations = doc.getDocumentElement().getChildNodes(); for (int i = 0; i < operations.getLength(); i++) { Node operation = operations.item(i); if (operation.getNodeType() != Node.ELEMENT_NODE) continue; call(operation); } this.currentScript = callerScript; }
private String absolutePath(String path) { return (path.startsWith("..")) ? Utils.combine(Utils.getParentDir(this.currentScript), path) : Utils.combine(this.targetDir, path); }
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())); } } }
void parseArrows(NodeList list) { int len = list.getLength(); for (int i = 0; i < len; i++) { if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { Element node = (Element) list.item(i); String name = node.getAttribute("name"); String desc = node.getAttribute("description"); String cls = node.getAttribute("class"); String catg = node.getAttribute("category"); String from = null; String to = null; String icon = null; NodeDesc ndfrom, ndto; ndfrom = formalism.getNodeDesc(node.getAttribute("from")); // System.err.println(nd); if (ndfrom == null) ndfrom = formalism.getConnectorDesc(node.getAttribute("from")); if (ndfrom != null) from = ndfrom.getElementClass(); ndto = formalism.getNodeDesc(node.getAttribute("to")); if (ndto == null) ndto = formalism.getConnectorDesc(node.getAttribute("to")); if (ndto != null) to = ndto.getElementClass(); NodeList icontag = node.getElementsByTagName("icon"); if (icontag.getLength() > 0) icon = ((Element) icontag.item(0)).getAttribute("url"); ArrowDesc ad = new ArrowDesc( name, desc, cls, icon, from, to, ndfrom, ndto); // formalism.getNodeDesc(from), formalism.getNodeDesc(to)); // System.err.println("LOADXF"+from+to+"|"+ndfrom+ // " /"+ ndto+" "+ // formalism.getNodeDescList()); ad.setCategory(catg); formalism.addArrowDesc(ad); ad.setFormalism(formalism); NodeList graphic = node.getElementsByTagName("graphic-element"); if (graphic.getLength() > 0) { String graphicClass = ((Element) graphic.item(0)).getAttribute("class"); if (!graphicClass.equals("")) ad.setGraphicClass(graphicClass); // System.err.println("calsselementpost"+ad.getGraphicClass()); NodeList proplist = ((Element) graphic.item(0)).getElementsByTagName("property"); if (proplist.getLength() > 0) ad.setGraphicProperties(getProperties(proplist)); node.removeChild(graphic.item(0)); } NodeList proplist = node.getElementsByTagName("property"); if (proplist.getLength() > 0) try { ReflectorUtil.setProperties(ad, getProperties(proplist)); } catch (Exception e) { Utils.log("setProperties failed:" + e); } NodeList actionList = node.getElementsByTagName("action"); for (int j = 0; j < actionList.getLength(); j++) if (actionList.item(j).getNodeType() == Node.ELEMENT_NODE) ad.addActionDesc(parseAction((Element) actionList.item(j))); } } }
void parseNodes(NodeList list) { int len = list.getLength(); for (int i = 0; i < len; i++) { Hashtable prophash = new Hashtable(); NodeDesc nd = null; if (list.item(i).getNodeType() == Node.ELEMENT_NODE) { Element node = (Element) list.item(i); String name = node.getAttribute("name"); String catg = node.getAttribute("category"); String desc = node.getAttribute("description"); String cls = node.getAttribute("class"); String icon = null; NodeList childs = node.getChildNodes(); boolean component = false; for (int j = 0; j < childs.getLength(); j++) if (childs.item(j).getNodeType() == Node.ELEMENT_NODE) { Element child = (Element) childs.item(j); if (child.getNodeName().equals("module")) component = true; } if (component) nd = new ComponentDesc(name, desc, cls); else nd = new NodeDesc(name, desc, cls); nd.setFormalism(formalism); for (int j = 0; j < childs.getLength(); j++) if (childs.item(j).getNodeType() == Node.ELEMENT_NODE) { Element child = (Element) childs.item(j); if (child.getNodeName().equals("icon")) { // NodeList icontag = node.getElementsByTagName("icon"); // if (icontag.getLength() > 0) // (Element) icon = child.getAttribute("url"); nd.setIcon(icon); } if (child.getNodeName().equals("module")) { String type = child.getAttribute("type"); // System.err.println("Module detected:"+type); int codemodule = 0; if (type.toLowerCase().equals("free")) codemodule = ComponentDesc.MODULE_FREE; if (type.toLowerCase().equals("template")) codemodule = ComponentDesc.MODULE_TEMPLATE; if (type.toLowerCase().equals("fixed")) codemodule = ComponentDesc.MODULE_FIXED; String layout = child.getAttribute("layout"); // System.err.println("Module layout detected:"+layout); int codelayout = 0; if (layout.toLowerCase().equals("auto")) codelayout = ComponentDesc.MODULE_PLACEMENT_AUTO; if (layout.toLowerCase().equals("justified")) codelayout = ComponentDesc.MODULE_PLACEMENT_JUSTIFIED; if (layout.toLowerCase().equals("manual")) codelayout = ComponentDesc.MODULE_PLACEMENT_MANUAL; // nd = new ComponentDesc(name, desc, cls, icon, codemodule, codelayout); ((ComponentDesc) nd).setModule(codemodule); ((ComponentDesc) nd).setLayout(codelayout); NodeList connlist = child.getChildNodes(); for (int k = 0; k < connlist.getLength(); k++) if (connlist.item(k).getNodeType() == Node.ELEMENT_NODE) { Element prop = (Element) connlist.item(k); if (connlist.item(k).getNodeType() == Node.ELEMENT_NODE) { String cref = prop.getAttribute("type"); String cname = prop.getAttribute("name"); String pside = prop.getAttribute("side"); String pratio = prop.getAttribute("ratio"); int codeside = ConnectorPlacement.UNSPECIFIED; if (pside.toLowerCase().equals("left")) codeside = ConnectorPlacement.LEFT; if (pside.toLowerCase().equals("right")) codeside = ConnectorPlacement.RIGHT; if (pside.toLowerCase().equals("top")) codeside = ConnectorPlacement.TOP; if (pside.toLowerCase().equals("bottom")) codeside = ConnectorPlacement.BOTTOM; double ratio = -1; if ((pratio != null) && (!pratio.equals(""))) ratio = (new Double(pratio)).doubleValue(); ConnectorPlacement cp; if ((ratio < 0) || (ratio > 1)) cp = new ConnectorPlacement(codeside); else cp = new ConnectorPlacement(codeside, ratio); ConnectorDesc cnx; cnx = formalism.getConnectorDesc(cref); // System.err.println("got cnx+"+cref+"("+cname+")"+cnx+" Pl "+cp); ((ComponentDesc) nd).addModuleConnector(cname, cnx, cp); } } } if (child.getNodeName().equals("graphic-element")) { String graphicClass = child.getAttribute("class"); if (!graphicClass.equals("")) nd.setGraphicClass(graphicClass); // System.err.println("nodeselement2"+name+" "+nd.getGraphicClass()); Hashtable graphprop = new Hashtable(); NodeList graphproplist = child.getChildNodes(); for (int m = 0; m < graphproplist.getLength(); m++) if (graphproplist.item(m).getNodeType() == Node.ELEMENT_NODE) { Element prop = (Element) graphproplist.item(m); if (prop.getNodeName().equals("property")) addProperty(graphprop, prop); } nd.setGraphicProperties(graphprop); // System.err.println(i+" "+j+" "+"IN PazeROPERTY NDGR"+child+ // proplist.getLength()+" "+ // nd.getGraphicProperties()); } if (child.getNodeName().equals("property")) { addProperty(prophash, child); nd.setProperties(prophash); } if (child.getNodeName().equals("action")) nd.addActionDesc(parseAction(child)); } if (nd != null) { nd.setCategory(catg); formalism.addNodeDesc(nd); try { ReflectorUtil.setProperties(nd, prophash); } catch (Exception e) { Utils.log("setProperties failed:" + e); } } } } }
protected Document parse(InputStream inputStream) throws LibraryException { Map<Integer, SongBean> songMap = new HashMap<Integer, SongBean>(); Map<Integer, AlbumBean> albumMap = new HashMap<Integer, AlbumBean>(); Map<Integer, AlbumBean> secondaryAlbumMap = new HashMap<Integer, AlbumBean>(); warningList.clear(); try { // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); factory.setIgnoringElementContentWhitespace(true); factory.setIgnoringComments(true); // Create the builder and parse the file DocumentBuilder builder = factory.newDocumentBuilder(); // Set an error listener and parse the document builder.setErrorHandler(new iTradeTunesLibraryErrorHandler()); builder.setEntityResolver(new iTradeTunesLibraryResolver()); Document document = builder.parse(inputStream); synchronized (libraryList) { // Query the library document and build the library list XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression xPathExpression = xPath.compile(XPATH_LIBRARY_LIST); NodeList nodelist = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET); // Process the elements in the nodelist SongBean song = null; for (int i = 0; i < nodelist.getLength(); i++) { boolean isTrackID = false; // Get element and child nodes Element elem = (Element) nodelist.item(i); NodeList list = elem.getChildNodes(); // Get node value Node childKey = list.item(0); String key = childKey.getNodeValue(); // Check if we have to create a new bean if (SongBean.NAME_TRACK_ID.equals(key)) { isTrackID = true; SongBean previousSong = song; song = new SongBean(); if (previousSong != null && !("AAC audio file".equals(previousSong.getKind()) || "MPEG audio file".equals(previousSong.getKind()))) { songMap.remove(previousSong.getTrack_ID()); } else { // Add an album bean addOrUpdateAlbum(albumMap, previousSong, false); } } // The first parameter is the key String prop = childKey.getNodeValue().replace(' ', '_'); // The second parameter is the value i++; // Get element and child nodes elem = (Element) nodelist.item(i); // Check for boolean properties Object value = null; // Get node value list = elem.getChildNodes(); childKey = list.item(0); value = (childKey == null) ? elem.getNodeName() : childKey.getNodeValue(); if (isTrackID) { isTrackID = false; } // Set the property of the song bean Statement stmt = new Statement(song, "set" + prop, new Object[] {value}); try { stmt.execute(); } catch (Exception e) { // Ignore that field, we do not have it in our bean } // If the property is the track ID, add the song to the hash // map if (SongBean.NAME_TRACK_ID.equals(key)) { int trackID = Integer.valueOf((String) value); songMap.put(trackID, song); } } // Update album for last song addOrUpdateAlbum(albumMap, song, false); // Check the album map for inconsistencies Iterator<AlbumBean> albums = albumMap.values().iterator(); while (albums.hasNext()) { AlbumBean album = albums.next(); if (album.checkConsistency()) { libraryList.add(album); album.setHashCode(); } else { // Add an inconsistent album only using the album title SongBean[] songs = album.getSongs(); for (int i = 0; i < songs.length; i++) { addOrUpdateAlbum(secondaryAlbumMap, songs[i], true); } } } // Check secondary album map for consistency albums = secondaryAlbumMap.values().iterator(); while (albums.hasNext()) { AlbumBean album = albums.next(); if (album.checkConsistency()) { libraryList.add(album); album.setHashCode(); } else { // This album cannot be matched // TODO: Add to warning message } } setChanged(); } return document; } catch (IOException ioe) { // Log an expected connect exception throw new LibraryException(ioe); } catch (SAXException se) { // Catch all other exceptions throw new LibraryException(se); } catch (ParserConfigurationException pce) { // Catch all other exceptions Utils.logSevere(pce); throw new LibraryException(pce); } catch (XPathExpressionException xpe) { // Catch all other exceptions Utils.logSevere(xpe); throw new LibraryException(xpe); } catch (NumberFormatException nfe) { // Catch all other exceptions throw new LibraryException(nfe); } }