List of usage examples for org.dom4j.dom DOMDocumentFactory createElement
public Element createElement(QName qname, int attributeCount)
From source file:net.unicon.academus.apps.briefcase.migrate.BriefcaseMigrator.java
License:Open Source License
public static void main(String[] args) { System.out.println("Starting Briefcase portlet migration of data from Academus 1.5 " + " to Academus 1.6"); if (args.length == 2) { try {//from www .jav a 2 s . co m DataSource ds = new SimpleDataSource(); SAXReader reader = new SAXReader(); // replace the old urls with the new classpaths System.out.println("Updating the resource urls."); updateUrl(ds); updateSharedResourceUrls(ds); // replace the OWNER access type. System.out.println("Updating the accessTypes."); updateAccessType(ds); // set the uni_sequence table for the RdbmsAccessBroker Counter // and the FSA_Kernel_Sequencer updateSequencers(ds); // read the old config file // and correct the classpaths for all the access brokers. Document oldDoc = reader.read(args[0]); DOMDocumentFactory dFac = new DOMDocumentFactory(); // Get all access brokers List brokers = oldDoc.selectNodes("//access-broker"); Element broker = null; // Change access brokers to point to alchemist instead of academus for (int i = 0; i < brokers.size(); i++) { broker = (Element) brokers.get(i); String implValue = broker.attributeValue("impl").replaceFirst("academus", "alchemist"); broker.addAttribute("impl", implValue); } // Get the Access Broker under the personal Drive, and make sure it's a JIT broker Element targetsJitAccessBroker = (Element) ((Element) oldDoc.selectNodes( "/briefcase/drive[@handle='personal']/access-broker[@impl='net.unicon.alchemist.access.jit.JitAccessBroker']") .get(0)).detach(); // Only enter this code if the personal drive section was successful selected if (targetsJitAccessBroker != null) { // Create new permissions element, mirroring targets element Element permissionsJitAccessBroker = (Element) targetsJitAccessBroker.clone(); // Change handles targetsJitAccessBroker.addAttribute("handle", "personal-resourses-t"); permissionsJitAccessBroker.addAttribute("handle", "personal-resourses-p"); // Create new permissions access-broker Element permAccessBroker = dFac.createElement(new QName("access-broker"), 2); permAccessBroker.addAttribute("handle", "personal-jit"); permAccessBroker.addAttribute("impl", PermissionsAccessBroker.class.getName()); // Create new access element and add it to permAccessBroker Element permAccess = dFac.createElement(new QName("access"), 1); permAccess.addAttribute("impl", BriefcaseAccessType.class.getName()); permAccessBroker.add(permAccess); // Create targets and permissions elements and add to the new permissions access-broker Element targets = permAccessBroker.addElement("targets"); targets.add(targetsJitAccessBroker); Element permissions = permAccessBroker.addElement("permissions"); permissions.add(permissionsJitAccessBroker); // Add new permissions access broker to the drive Element curDrive = (Element) oldDoc.selectNodes("/briefcase/drive[@handle='personal']").get(0); curDrive.add(permAccessBroker); // // Change targets internals // List targetsAccess = targets.selectNodes("access-broker/jit-rule/behavior/access"); for (int i = 0; i < targetsAccess.size(); i++) { // Add impl attribute with value of fully-qualified class name ((Element) targetsAccess.get(i)).addAttribute("impl", DummyAccessType.class.getName()); // Get all child type elements and remove them List types = ((Element) targetsAccess.get(i)).elements(); for (int j = 0; j < types.size(); j++) { ((Element) types.get(j)).detach(); } // Add a single dummy element Element eType = dFac.createElement(new QName("type"), 2); eType.addAttribute("value", "GRANT"); eType.addAttribute("handle", "DUMMY"); ((Element) targetsAccess.get(i)).add(eType); } // Add internal access broker's access element Element targetsIAccessBroker = (Element) (targets.selectNodes("access-broker/access-broker") .get(0)); Element targetsIAccess = dFac.createElement(new QName("access"), 1); targetsIAccess.addAttribute("impl", DummyAccessType.class.getName()); targetsIAccessBroker.add(targetsIAccess); // // Change permissions internals // List permissionsAccess = permissions.selectNodes("access-broker/jit-rule/behavior/access"); for (int i = 0; i < permissionsAccess.size(); i++) { // Add impl attribute with value of fully-qualified class name ((Element) permissionsAccess.get(i)).addAttribute("impl", BriefcaseAccessType.class.getName()); // Get all child type elements and replace them List types = ((Element) permissionsAccess.get(i)).elements(); for (int j = 0; j < types.size(); j++) { Attribute value = ((Element) types.get(j)).attribute("value"); String text = value.getValue(); value.setValue("GRANT"); if (text.equals("0")) { BriefcaseAccessType[] aTypes = BriefcaseAccessType.getInstances(); ((Element) types.get(j)).addAttribute("handle", aTypes[0].getName()); for (int k = 1; k < aTypes.length; k++) { Element eType = dFac.createElement(new QName("type"), 2); eType.addAttribute("value", "GRANT"); eType.addAttribute("handle", aTypes[k].getName()); ((Element) permissionsAccess.get(i)).add(eType); } } else { ((Element) types.get(j)).addAttribute("handle", BriefcaseAccessType.getAccessType(Integer.parseInt(text)).getName()); } } } // Change other elements in the permissions section List permissionsBehavior = permissions.selectNodes("access-broker/jit-rule/behavior"); for (int i = 0; i < permissionsBehavior.size(); i++) { Element trigger = (Element) ((Element) permissionsBehavior.get(i)).elements("trigger") .get(0); Element target = (Element) ((Element) permissionsBehavior.get(i)).elements("target").get(0); Element creator = (Element) ((Element) permissionsBehavior.get(i)).elements("creator") .get(0); // Insert trigger text into target target.addAttribute("type", "GROUP"); target.addText(trigger.getText()); // Remove current creator element creator.detach(); // Add new current creator element Element eCreator = dFac.createElement(new QName("creator"), 1); eCreator.addAttribute("impl", DummyCreator.class.getName()); ((Element) permissionsBehavior.get(i)).add(eCreator); } // Change internal access broker's name Element permissionsIAccessBroker = (Element) (permissions .selectNodes("access-broker/access-broker").get(0)); permissionsIAccessBroker.addAttribute("handle", "personal-resources-p-i"); // Add internal access broker's access element Element permissionsIAccess = dFac.createElement(new QName("access"), 1); permissionsIAccess.addAttribute("impl", BriefcaseAccessType.class.getName()); permissionsIAccessBroker.add(permissionsIAccess); } List access = oldDoc.selectNodes("/briefcase/drive[@handle!='personal']//access"); for (int i = 0; i < access.size(); i++) { // Add impl attribute with value of fully-qualified class name ((Element) access.get(i)).addAttribute("impl", BriefcaseAccessType.class.getName()); List types = ((Element) access.get(i)).elements(); for (int j = 0; j < types.size(); j++) { Attribute value = ((Element) types.get(j)).attribute("value"); String text = value.getValue(); value.setValue("GRANT"); if (text.equals("0")) { BriefcaseAccessType[] aTypes = BriefcaseAccessType.getInstances(); ((Element) types.get(j)).addAttribute("handle", aTypes[0].getName()); for (int k = 1; k < aTypes.length; k++) { Element eType = dFac.createElement(new QName("type"), 2); eType.addAttribute("value", "GRANT"); eType.addAttribute("handle", aTypes[k].getName()); ((Element) access.get(i)).add(eType); } } else { ((Element) types.get(j)).addAttribute("handle", BriefcaseAccessType.getAccessType(Integer.parseInt(text)).getName()); } } } // add impl attribute to specify the UserDirectory impl to user-attribute element List userAttr = oldDoc.selectNodes("//user-attribute"); for (int i = 0; i < userAttr.size(); i++) { ((Element) userAttr.get(i)).addAttribute("impl", "net.unicon.academus.apps.briefcase.UserAttributeDirectory"); } //replace the resource factory urls List entries = oldDoc .selectNodes("/briefcase/drive[@handle!='personal']//access-broker/entry[@target!='']"); for (int i = 0; i < entries.size(); i++) { ((Element) entries.get(i)).addAttribute("target", ((Element) entries.get(i)) .attributeValue("target").replaceAll("academus.resource.factory", "demetrius.fac")); } // add access element to specify the AccessType to the RdbmsAccessBroker brokers = oldDoc.selectNodes("/briefcase/drive[@handle!='personal']//access-broker[@impl='" + RdbmsAccessBroker.class.getName() + "']"); for (int i = 0; i < brokers.size(); i++) { broker = (Element) brokers.get(i); Element eType = dFac.createElement(new QName("access"), 1); eType.addAttribute("impl", BriefcaseAccessType.class.getName()); broker.add(eType); } // add the civis address book information. Element drive = (Element) oldDoc.selectSingleNode("briefcase"); drive.addComment("Civis implementation to be used to resolve usernames and group paths" + " to academus users and groups. This should not require" + " modification, as it utilized the Academus framework for gathering this" + " information."); Element civis = dFac.createElement("civis"); civis.addAttribute("id", "addressBook"); civis.addAttribute("impl", "net.unicon.civis.fac.academus.AcademusCivisFactory"); Element restrictor = dFac.createElement("restrictor"); restrictor.addAttribute("impl", "net.unicon.civis.grouprestrictor.AcademusGroupRestrictor"); civis.add(restrictor); drive.add(civis); File f = new File(args[1]); PrintWriter pw = new PrintWriter(new FileOutputStream(f)); pw.write(oldDoc.asXML()); pw.flush(); pw.close(); System.out.println( "Done. Enjoy !! \n Remember to the use the migrated config file with the 1.6 deploy."); } catch (Exception e) { e.printStackTrace(); } } else { usage(); } }