/** * Handles the creation of new XML files into a particular directory. Files are stored based on * the user's unique userid. Each file contains the user's name, userid and resource IDs. * * @param userid * @param locator * @param name * @param address * @throws IOException * @throws SAXException */ public void createFile(String userid, String locator, String name, String address) throws IOException, SAXException { // Where to create the XML file String filename = "/home/policygrid/apache-tomcat-6.0.18/webapps/ourspaces/users/" + userid + ".xml"; // Define a new filetype File file = new File(filename); FileOutputStream fos = new FileOutputStream(filename); // Set the output of the filetype OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true); of.setIndent(1); of.setIndenting(true); XMLSerializer serializer = new XMLSerializer(fos, of); ContentHandler hd = serializer.asContentHandler(); hd.startDocument(); AttributesImpl atts = new AttributesImpl(); // Generate XML tags hd.startElement("", "", "user", null); hd.startElement("", "", "userid", null); hd.characters(userid.toCharArray(), 0, userid.length()); hd.endElement("", "", "userid"); hd.startElement("", "", "locator", null); hd.characters(locator.toCharArray(), 0, locator.length()); hd.endElement("", "", "locator"); hd.startElement("", "", "name", null); hd.characters(name.toCharArray(), 0, name.length()); hd.endElement("", "", "name"); hd.startElement("", "", "address", null); hd.characters(address.toCharArray(), 0, address.length()); hd.endElement("", "", "address"); hd.startElement("", "", "home", null); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "myresources"); atts.addAttribute("", "", "column", "CDATA", "2"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "2"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "myprojects"); atts.addAttribute("", "", "column", "CDATA", "2"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "1"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "mytags"); atts.addAttribute("", "", "column", "CDATA", "1"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "2"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "mycontacts"); atts.addAttribute("", "", "column", "CDATA", "1"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "1"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "myblog"); atts.addAttribute("", "", "column", "CDATA", "3"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "1"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "ouractivities"); atts.addAttribute("", "", "column", "CDATA", "3"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "2"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); atts.clear(); atts.addAttribute("", "", "name", "CDATA", "myactivities"); atts.addAttribute("", "", "column", "CDATA", "3"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", "2"); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); hd.endElement("", "", "home"); hd.endElement("", "", "user"); hd.endDocument(); // Close the file fos.close(); }
public void deleteBox(int id, String original, String remove) throws ParserConfigurationException, SAXException, IOException { // Open the correct file String filename = "/home/policygrid/apache-tomcat-6.0.18/webapps/ourspaces/users/" + id + ".xml"; /* * Handle the tokenizing of the string. Remove the '&' symbol, then split into columns. */ String pattern = "&"; String newString = original.substring(0, original.length() - 1); String[] fields = newString.split("&"); ArrayList lft = new ArrayList(); ArrayList mid = new ArrayList(); ArrayList rht = new ArrayList(); for (int i = 0; i < fields.length; i++) { if (fields[i].charAt(11) == '0') { lft.add(fields[i].split("widget_col_0=")); } if (fields[i].charAt(11) == '1') { mid.add(fields[i].split("widget_col_1=")); } if (fields[i].charAt(11) == '2') { rht.add(fields[i].split("widget_col_2=")); } } for (int i = 0; i < lft.size(); i++) { String[] tmp = (String[]) lft.get(i); String name2 = (String) tmp[1]; if (name2.equals(remove)) lft.remove(i); } for (int i = 0; i < mid.size(); i++) { String[] tmp = (String[]) mid.get(i); String name2 = (String) tmp[1]; if (name2.equals(remove)) mid.remove(i); } for (int i = 0; i < rht.size(); i++) { String[] tmp = (String[]) rht.get(i); String name2 = (String) tmp[1]; if (name2.equals(remove)) rht.remove(i); } // Initialise the DOM parser DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document dom; DocumentBuilder db = dbf.newDocumentBuilder(); // Parse the file dom = db.parse(filename); Element docEle = dom.getDocumentElement(); // Retrieve the previous tags from the parsed file so they can be re-added. String userid = getTextValue(docEle, "userid"); String locator = getTextValue(docEle, "locator"); String name = getTextValue(docEle, "name"); String address = getTextValue(docEle, "address"); File file = new File(filename); FileOutputStream fos = new FileOutputStream(filename); // Set the output of the filetype OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true); of.setIndent(1); of.setIndenting(true); XMLSerializer serializer = new XMLSerializer(fos, of); ContentHandler hd = serializer.asContentHandler(); hd.startDocument(); AttributesImpl atts = new AttributesImpl(); // Generate new XML tags hd.startElement("", "", "user", null); hd.startElement("", "", "userid", null); hd.characters(userid.toCharArray(), 0, userid.length()); hd.endElement("", "", "userid"); hd.startElement("", "", "locator", null); hd.characters(locator.toCharArray(), 0, locator.length()); hd.endElement("", "", "locator"); hd.startElement("", "", "name", null); hd.characters(name.toCharArray(), 0, name.length()); hd.endElement("", "", "name"); hd.startElement("", "", "address", null); hd.characters(address.toCharArray(), 0, address.length()); hd.endElement("", "", "address"); hd.startElement("", "", "home", null); for (int j = 0; j < lft.size(); j++) { String[] tmp = (String[]) lft.get(j); String name2 = (String) tmp[1]; Integer pos = j + 1; String position = pos.toString(); atts.clear(); atts.addAttribute("", "", "name", "CDATA", getTagName(name2)); atts.addAttribute("", "", "column", "CDATA", "1"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", position); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); } for (int j = 0; j < mid.size(); j++) { String[] tmp = (String[]) mid.get(j); String name2 = (String) tmp[1]; Integer pos = j + 1; String position = pos.toString(); atts.clear(); atts.addAttribute("", "", "name", "CDATA", getTagName(name2)); atts.addAttribute("", "", "column", "CDATA", "2"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", position); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); } for (int j = 0; j < rht.size(); j++) { String[] tmp = (String[]) rht.get(j); String name2 = (String) tmp[1]; Integer pos = j + 1; String position = pos.toString(); atts.clear(); atts.addAttribute("", "", "name", "CDATA", getTagName(name2)); atts.addAttribute("", "", "column", "CDATA", "3"); atts.addAttribute("", "", "status", "CDATA", "1"); atts.addAttribute("", "", "position", "CDATA", position); hd.startElement("", "", "box", atts); hd.endElement("", "", "box"); } hd.endElement("", "", "home"); hd.endElement("", "", "user"); hd.endDocument(); // Close the file fos.close(); }