List of usage examples for org.dom4j.io XMLWriter close
public void close() throws IOException
From source file:com.doculibre.constellio.izpack.PersistenceXMLUtils.java
License:Open Source License
@SuppressWarnings("unchecked") public static void run(AbstractUIProcessHandler handler, String[] args) { if (args.length != 5) { System.out.println("persistence_mysqlPath defaultPersistencePath server login password"); return;//from ww w. j a v a 2 s . c o m } String persistence_mysqlPath = args[0]; String defaultPersistencePath = args[1]; String server = args[2]; String login = args[3]; String password = args[4]; Document xmlDocument; try { xmlDocument = new SAXReader().read(persistence_mysqlPath); Element root = xmlDocument.getRootElement(); Iterator<Element> it = root.elementIterator("persistence-unit"); if (!it.hasNext()) { System.out.println("Corrupt persistence file :" + persistence_mysqlPath); return; } it = it.next().elementIterator("properties"); if (!it.hasNext()) { System.out.println("Corrupt persistence file :" + persistence_mysqlPath); return; } Element properties = it.next(); for (it = properties.elementIterator("property"); it.hasNext();) { Element property = it.next(); String id = property.attributeValue("name"); if (id.equals(SERVER_ELEMENT_ID)) { Attribute att = property.attribute("value"); att.setText(BEFORE_SERVER_NAME + server + AFTER_SERVER_NAME); } else { if (id.equals(LOGIN_ELEMENT_ID)) { Attribute att = property.attribute("value"); att.setText(login); } else { if (id.equals(PASSWORD_ELEMENT_ID)) { Attribute att = property.attribute("value"); att.setText(password); } } } } OutputFormat format = OutputFormat.createPrettyPrint(); File xmlFile = new File(persistence_mysqlPath); XMLWriter writer2 = new XMLWriter(new FileOutputStream(xmlFile), format); writer2.write(xmlDocument); writer2.close(); // copier au fichier de persistence par dfaut: xmlFile = new File(defaultPersistencePath); writer2 = new XMLWriter(new FileOutputStream(xmlFile), format); writer2.write(xmlDocument); writer2.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.doculibre.constellio.izpack.TomcatUtil.java
License:Open Source License
@SuppressWarnings("unchecked") public static void run(AbstractUIProcessHandler handler, String[] args) { if (args.length != 2) { System.out.println("serverPath port"); return;// www . java2 s.co m } String serverPath = args[0]; String port = args[1]; if (port.equals("8080")) { // C'est celui par defaut => ne rien faire return; } Document xmlDocument; try { xmlDocument = new SAXReader().read(serverPath); Element root = xmlDocument.getRootElement(); Iterator<Element> it = root.elementIterator("Service"); if (!it.hasNext()) { System.out.println("Corrupt persistence file :" + serverPath); return; } Element connectors = it.next(); for (it = connectors.elementIterator("Connector"); it.hasNext();) { Element connector = it.next(); String id = connector.attributeValue("protocol"); if (id.startsWith("HTTP")) { Attribute att = connector.attribute("port"); att.setText(port); break; } } OutputFormat format = OutputFormat.createPrettyPrint(); File xmlFile = new File(serverPath); XMLWriter writer2 = new XMLWriter(new FileOutputStream(xmlFile), format); writer2.write(xmlDocument); writer2.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.doculibre.constellio.izpack.UsersToXmlFile.java
License:Open Source License
public static void run(AbstractUIProcessHandler handler, String[] args) { if (args.length != 3) { System.out.println("file login password"); return;//from w w w.jav a2 s .c o m } String target = args[0]; File xmlFile = new File(target); BufferedWriter writer; try { writer = new BufferedWriter(new FileWriter(xmlFile)); } catch (IOException e) { e.printStackTrace(); return; } try { for (String line : Arrays.asList(emptyFileLines)) { writer.write(line + System.getProperty("line.separator")); } writer.close(); } catch (Exception e) { e.printStackTrace(); } String login = args[1]; String passwd = args[2]; UsersToXmlFile elem = new UsersToXmlFile(); ConstellioUser dataUser = elem.new ConstellioUser(login, passwd, null); dataUser.setFirstName("System"); dataUser.setLastName("Administrator"); dataUser.getRoles().add(Roles.ADMIN); Document xmlDocument; try { xmlDocument = new SAXReader().read(target); Element root = xmlDocument.getRootElement(); BaseElement user = new BaseElement(USER); user.addAttribute(FIRST_NAME, dataUser.getFirstName()); user.addAttribute(LAST_NAME, dataUser.getLastName()); user.addAttribute(LOGIN, dataUser.getUsername()); user.addAttribute(PASSWORD_HASH, dataUser.getPasswordHash()); if (dataUser.getLocale() != null) { user.addAttribute(LOCALE, dataUser.getLocaleCode()); } Set<String> constellioRoles = dataUser.getRoles(); if (!constellioRoles.isEmpty()) { Element roles = user.addElement(ROLES); for (String constellioRole : constellioRoles) { Element role = roles.addElement(ROLE); role.addAttribute(VALUE, constellioRole); } } root.add(user); OutputFormat format = OutputFormat.createPrettyPrint(); xmlFile = new File(target); // FIXME recrire la DTD // xmlDocument.addDocType(arg0, arg1, arg2) XMLWriter writer2 = new XMLWriter(new FileOutputStream(xmlFile), format); writer2.write(xmlDocument); writer2.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.doculibre.constellio.services.SolrServicesImpl.java
License:Open Source License
public static void writeXMLConfigInCloud(String collectionName, String fileName, Document schemaDocument) { String realCollectionName;/* w ww .ja va 2 s . c o m*/ if (SolrServicesImpl.isAliasInCloud(collectionName)) { realCollectionName = SolrServicesImpl.getRealCollectionInCloud(collectionName); } else { realCollectionName = collectionName; } try { OutputFormat format = OutputFormat.createPrettyPrint(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); XMLWriter writer = new XMLWriter(outputStream, format); writer.write(schemaDocument); writer.close(); SolrZkClient zkClient = SolrCoreContext.getSolrZkClient(); zkClient.setData(ZkController.CONFIGS_ZKNODE + "/" + realCollectionName + "/" + fileName, outputStream.toByteArray(), true); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.doculibre.constellio.services.SolrServicesImpl.java
License:Open Source License
private static void writeConstellioDefaultSchema(Document schemaDocument) { File schemaFile = new File( ClasspathUtils.getCollectionsRootDir() + File.separator + SolrCoreContext.DEFAULT_COLLECTION_NAME + File.separator + "conf" + File.separator + "schema.xml"); FileOutputStream fos = null;//from w w w . j av a 2 s . com try { OutputFormat format = OutputFormat.createPrettyPrint(); fos = new FileOutputStream(schemaFile); XMLWriter writer = new XMLWriter(fos, format); writer.write(schemaDocument); writer.close(); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(fos); } }
From source file:com.doculibre.constellio.utils.izpack.UsersXmlFileUtils.java
License:Open Source License
public static void addUserTo(ConstellioUser constellioUser, String fileName) { Document xmlDocument;/*from w ww .jav a 2 s.c o m*/ try { xmlDocument = new SAXReader().read(fileName); Element root = xmlDocument.getRootElement(); Element user = toXmlElement(constellioUser); root.add(user); OutputFormat format = OutputFormat.createPrettyPrint(); File xmlFile = new File(fileName); //FIXME recrire la DTD //xmlDocument.addDocType(arg0, arg1, arg2) XMLWriter writer = new XMLWriter(new FileOutputStream(xmlFile), format); writer.write(xmlDocument); writer.close(); } catch (DocumentException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.dp2345.util.SettingUtils.java
License:Open Source License
/** * //from w w w. j a v a 2 s . c om * * @param setting * */ public static void set(Setting setting) { try { File dp2345XmlFile = new ClassPathResource(CommonAttributes.DP2345_XML_PATH).getFile(); Document document = new SAXReader().read(dp2345XmlFile); List<Element> elements = document.selectNodes("/dp2345/setting"); for (Element element : elements) { try { String name = element.attributeValue("name"); String value = beanUtils.getProperty(setting, name); Attribute attribute = element.attribute("value"); attribute.setValue(value); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } FileOutputStream fileOutputStream = null; XMLWriter xmlWriter = null; try { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setEncoding("UTF-8"); outputFormat.setIndent(true); outputFormat.setIndent(" "); outputFormat.setNewlines(true); fileOutputStream = new FileOutputStream(dp2345XmlFile); xmlWriter = new XMLWriter(fileOutputStream, outputFormat); xmlWriter.write(document); } catch (Exception e) { e.printStackTrace(); } finally { if (xmlWriter != null) { try { xmlWriter.close(); } catch (IOException e) { } } IOUtils.closeQuietly(fileOutputStream); } Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME); cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting)); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java
private static void writePackageInfoZipEntry(final SoundPackage soundPackage, final ZipOutputStream out) throws UnsupportedEncodingException, IOException { // write xml to temporary byte array, because of stripping problems, when // directly writing to encrypted stream ByteArrayOutputStream bOut = new ByteArrayOutputStream(); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(ENCODING);/*from w w w.j av a2 s .c o m*/ XMLWriter writer = new XMLWriter(bOut, format); writer.setEscapeText(true); writer.write(createPackageInfoXml(soundPackage)); writer.close(); // write temporary byte array to encrypet zip entry ByteArrayInputStream bIn = new ByteArrayInputStream(bOut.toByteArray()); writeZipEntry(PACKAGE_INFO, out, bIn); }
From source file:com.easyjf.generator.AllGenerator.java
License:Apache License
private void genXml(AllProcessor processor) { for (String templateFile : this.xmlfiles.keySet()) { tg.setProcess(processor);/*from w w w.j av a 2 s . co m*/ String fileName = (String) xmlfiles.get(templateFile); File targetFile = new File(fileName); tg.setTargetDir(""); tg.setTemplateDir(templateDir); tg.setTemplateName(templateFile); if (targetFile.exists()) { tg.setTargetName(fileName + "_tmp"); tg.generator(false); try { Document doc = this.getDocument(fileName + "_tmp"); Document document = this.getDocument(fileName); String existNode = "/easyjf-web/modules/module[@name='" + this.lowerBeanName + "']"; Node node = "mvc.xml".equals(templateFile) ? document.selectSingleNode(existNode) : findBean(document, this.lowerBeanName + ("dao.xml".equals(templateFile) ? "Dao" : "Service")); if (node == null) { String appendNode = "/easyjf-web/modules", cnode = appendNode + "/module"; if (!"mvc.xml".equals(templateFile)) appendNode = "/beans"; Element moduleE = (Element) document.selectSingleNode(appendNode); Node n = "mvc.xml".equals(templateFile) ? doc.selectSingleNode(cnode) : findBean(doc, this.lowerBeanName + ("dao.xml".equals(templateFile) ? "Dao" : "Service")); if (moduleE != null && n != null) { n.detach(); moduleE.add(n); } OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter output = new XMLWriter(new FileWriter(new File(fileName)), format); output.write(document); output.close(); } new File(fileName + "_tmp").deleteOnExit(); new File(fileName + "_tmp").delete(); } catch (Exception e) { // e.printStackTrace(); } System.out.println( I18n.getLocaleMessage("generator.Successfully.add.to.configuration.information.to.a.file") + fileName); } else { System.out.println( I18n.getLocaleMessage("generator.Successful.configuration.file.generation") + fileName); tg.setTargetName(fileName); new File(fileName + "_tmp").deleteOnExit(); new File(fileName + "_tmp").delete(); tg.generator(false); } } }
From source file:com.eurelis.opencms.ant.task.ManifestBuilderTask.java
License:Open Source License
public void write() throws Exception { File tmpFile = new File(distfolder + "/manifest.xml"); FileOutputStream fos = new FileOutputStream(tmpFile); XMLWriter writer = new XMLWriter(fos, OutputFormat.createPrettyPrint()); writer.write(document);// w ww . j av a 2s. c o m writer.close(); }