List of usage examples for org.dom4j.io SAXReader read
public Document read(InputSource in) throws DocumentException
Reads a Document from the given InputSource
using SAX
From source file:com.magicpwd._bean.mpro.GuidBean.java
License:Open Source License
public void exptCardActionPerformed(java.awt.event.ActionEvent evt) { if (processing) { return;//from w w w . j a v a 2 s. c o m } processing = true; if (fileTM == null) { fileTM = new FileTM("card", Pattern.compile("[^.]+[.]amc$", Pattern.CASE_INSENSITIVE), false); pm_CardMenu = new javax.swing.JPopupMenu(); mu_HtmMenu = new javax.swing.JMenu("HTM"); mu_TxtMenu = new javax.swing.JMenu("TXT"); mu_PngMenu = new javax.swing.JMenu("PNG"); mu_SvgMenu = new javax.swing.JMenu("SVG"); al_Listener = new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { cardItemActionPerformed(evt); } }; } if (fileTM.checkModified()) { pm_CardMenu.removeAll(); mu_HtmMenu.removeAll(); pm_CardMenu.add(mu_HtmMenu); mu_TxtMenu.removeAll(); pm_CardMenu.add(mu_TxtMenu); mu_PngMenu.removeAll(); pm_CardMenu.add(mu_PngMenu); mu_SvgMenu.removeAll(); pm_CardMenu.add(mu_SvgMenu); SAXReader reader = new SAXReader(); javax.swing.JMenuItem item; for (java.io.File cardFile : fileTM.getFileList()) { try { Document doc = reader.read(cardFile); Node name = doc.selectSingleNode("/magicpwd/base/name"); item = new javax.swing.JMenuItem(); item.addActionListener(al_Listener); item.setText(name != null ? name.getText() : cardFile.getName()); Node type = doc.selectSingleNode("/magicpwd/base/type"); if (type != null) { String text = (type.getText() + "").trim().toLowerCase(); if (ConsEnv.CARD_HTM.equals(text)) { item.setActionCommand(ConsEnv.CARD_HTM + '/' + cardFile.getPath()); mu_HtmMenu.add(item); continue; } if (ConsEnv.CARD_TXT.equals(text)) { item.setActionCommand(ConsEnv.CARD_TXT + '/' + cardFile.getPath()); mu_TxtMenu.add(item); continue; } if (ConsEnv.CARD_PNG.equals(text)) { item.setActionCommand(ConsEnv.CARD_PNG + '/' + cardFile.getPath()); mu_PngMenu.add(item); continue; } if (ConsEnv.CARD_SVG.equals(text)) { item.setActionCommand(ConsEnv.CARD_SVG + '/' + cardFile.getPath()); mu_SvgMenu.add(item); continue; } } item.setActionCommand(ConsEnv.CARD_ALL + '/' + cardFile.getPath()); pm_CardMenu.add(item); } catch (Exception exp) { processing = false; Logs.exception(exp); Lang.showMesg(mainPtn, null, exp.getLocalizedMessage()); return; } } } pm_CardMenu.show(bt_ExptCard, 0, bt_ExptCard.getPreferredSize().height); processing = false; }
From source file:com.magicpwd._bean.mwiz.GuidBean.java
License:Open Source License
public void exptCardActionPerformed(java.awt.event.ActionEvent evt) { if (processing) { return;// w ww . j a v a2s . c o m } processing = true; if (fileTM == null) { fileTM = new FileTM("card", Pattern.compile("[^.]+[.]amc$", Pattern.CASE_INSENSITIVE), false); pm_CardMenu = new javax.swing.JPopupMenu(); mu_HtmMenu = new javax.swing.JMenu("HTM"); mu_TxtMenu = new javax.swing.JMenu("TXT"); mu_PngMenu = new javax.swing.JMenu("PNG"); mu_SvgMenu = new javax.swing.JMenu("SVG"); al_Listener = new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { cardItemActionPerformed(evt); } }; } if (fileTM.checkModified()) { pm_CardMenu.removeAll(); mu_HtmMenu.removeAll(); pm_CardMenu.add(mu_HtmMenu); mu_TxtMenu.removeAll(); pm_CardMenu.add(mu_TxtMenu); mu_PngMenu.removeAll(); pm_CardMenu.add(mu_PngMenu); mu_SvgMenu.removeAll(); pm_CardMenu.add(mu_SvgMenu); SAXReader reader = new SAXReader(); javax.swing.JMenuItem item; for (java.io.File cardFile : fileTM.getFileList()) { try { Document doc = reader.read(cardFile); Node name = doc.selectSingleNode("/magicpwd/base/name"); item = new javax.swing.JMenuItem(); item.addActionListener(al_Listener); item.setText(name != null ? name.getText() : cardFile.getName()); Node type = doc.selectSingleNode("/magicpwd/base/type"); if (type != null) { String text = (type.getText() + "").trim().toLowerCase(); if (ConsEnv.CARD_HTM.equals(text)) { item.setActionCommand(ConsEnv.CARD_HTM + '/' + cardFile.getPath()); mu_HtmMenu.add(item); continue; } if (ConsEnv.CARD_TXT.equals(text)) { item.setActionCommand(ConsEnv.CARD_TXT + '/' + cardFile.getPath()); mu_TxtMenu.add(item); continue; } if (ConsEnv.CARD_PNG.equals(text)) { item.setActionCommand(ConsEnv.CARD_PNG + '/' + cardFile.getPath()); mu_PngMenu.add(item); continue; } if (ConsEnv.CARD_SVG.equals(text)) { item.setActionCommand(ConsEnv.CARD_SVG + '/' + cardFile.getPath()); mu_SvgMenu.add(item); continue; } } item.setActionCommand(ConsEnv.CARD_ALL + '/' + cardFile.getPath()); pm_CardMenu.add(item); } catch (Exception exp) { processing = false; Logs.exception(exp); Lang.showMesg(normPtn, null, exp.getLocalizedMessage()); return; } } } pm_CardMenu.show(bt_ExptCard, 0, bt_ExptCard.getPreferredSize().height); processing = false; }
From source file:com.marklogic.client.example.handle.DOM4JHandleExample.java
License:Apache License
public static void runShortcut(ExampleProperties props) throws IOException, DocumentException { String filename = "flipper.xml"; // register the handle from the extra library DatabaseClientFactory.getHandleRegistry().register(DOM4JHandle.newFactory()); // create the client DatabaseClient client = DatabaseClientFactory.newClient(props.host, props.port, props.writerUser, props.writerPassword, props.authType); // create a manager for documents of any format XMLDocumentManager docMgr = client.newXMLDocumentManager(); // read the example file InputStream docStream = Util.openStream("data" + File.separator + filename); if (docStream == null) throw new IOException("Could not read document example"); // create an identifier for the document String docId = "/example/" + filename; // parse the example file with dom4j SAXReader reader = new SAXReader(); reader.setValidation(false);/* www .j a va2 s. c o m*/ Document writeDocument = reader.read(new InputStreamReader(docStream, "UTF-8")); // write the document docMgr.writeAs(docId, writeDocument); // ... at some other time ... // read the document content Document readDocument = docMgr.readAs(docId, Document.class); String rootName = readDocument.getRootElement().getName(); // delete the document docMgr.delete(docId); System.out.println("(Shortcut) Wrote and read /example/" + filename + " content with the <" + rootName + "/> root element using dom4j"); // release the client client.release(); }
From source file:com.metaware.guihili.GuihiliParser.java
License:Open Source License
public static Document parse(InputSource input) throws DocumentException { SAXReader reader = new SAXReader(XMLReaderFactory.makeReader()); return reader.read(input); }
From source file:com.mg.framework.support.ui.UIProducer.java
License:Open Source License
private static Form doProduceForm(URL url, String formName, RuntimeMacrosLoader runtimeMacrosLoader) { if (url == null) throw new ApplicationException(Messages.getInstance().getMessage(Messages.FORM_DESCRIPTOR_NOT_FOUND, new Object[] { formName })); SAXReader reader = new SAXReader(); try {//from w ww. j a va2s .co m return parseFormDescription(reader.read(url), formName, runtimeMacrosLoader); } catch (DocumentException e) { throw new ApplicationException(e); } }
From source file:com.mgmtp.perfload.core.common.xml.Dom4jReader.java
License:Apache License
/** * Creates a DOM4J documents from the specified {@link InputSource} and validates it against the * given schema./*from w ww . j a va2 s .c o m*/ * * @param xmlSource * the source for the XML document * @param schemaSource * the source for the schema * @param xIncludeAware * specifies whether XIncludes should be supported * @return the DOM4J document */ public static Document loadDocument(final InputSource xmlSource, final Source schemaSource, final boolean xIncludeAware, final String encoding) throws ParserConfigurationException, SAXException, DocumentException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); // turn on validation factory.setNamespaceAware(true); // factory.setFeature("http://apache.org/xml/features/validation/schema", true); if (xIncludeAware) { // allow XIncludes factory.setFeature("http://apache.org/xml/features/xinclude", true); factory.setFeature("http://apache.org/xml/features/xinclude/fixup-base-uris", false); } SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); factory.setSchema(schemaFactory.newSchema(schemaSource)); SAXParser parser = factory.newSAXParser(); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setEncoding(encoding); return reader.read(xmlSource); }
From source file:com.mindquarry.desktop.model.ModelBase.java
License:Open Source License
protected Document parseInput(InputStream data) { SAXReader reader = new SAXReader(); try {/*w ww . j a v a 2 s . c o m*/ return reader.read(data); } catch (DocumentException e) { throw new RuntimeException(e); } }
From source file:com.mnt.base.mail.MailHelper.java
License:Open Source License
private static String[] loadMailTemplate(String templateName) { String[] titleContents = new String[2]; InputStream in = BaseConfiguration.getRelativeFileStream("mail.template.xml"); if (in != null) { try {// www .j a va 2s. co m SAXReader reader = new SAXReader(); Document document = reader.read(in); Element rootElt = document.getRootElement(); // Element mailInfo = (Element) rootElt .selectSingleNode("//templates/mail[@id='" + templateName + "']"); if (mailInfo != null) { titleContents[0] = mailInfo.element("title").getText(); titleContents[1] = mailInfo.element("content").getText(); } } catch (Exception e) { log.error("fail to load the mail template: " + templateName, e); } finally { try { in.close(); } catch (IOException e) { log.error("fail to close the mail template file: " + templateName, e); } } } else { log.error("no corresponding mail template file be found: " + templateName); } return titleContents; }
From source file:com.mothsoft.alexis.engine.numeric.President2012DataSetImporter.java
License:Apache License
@SuppressWarnings("unchecked") private Map<Date, PollResults> getPage(final String after, final int pageNumber) { final Map<Date, PollResults> pageMap = new LinkedHashMap<Date, PollResults>(); HttpClientResponse httpResponse = null; try {//from w ww.j a va 2s . c om final URL url = new URL(String.format(BASE_URL, after, pageNumber)); httpResponse = NetworkingUtil.get(url, null, null); final SAXReader saxReader = new SAXReader(); org.dom4j.Document document; try { document = saxReader.read(httpResponse.getInputStream()); } catch (DocumentException e) { throw new IOException(e); } finally { httpResponse.close(); } final SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD); final List<Node> questions = document.selectNodes(XPATH_QUESTIONS); for (final Node question : questions) { final Date endDate; final Node poll = question.selectSingleNode(ANCESTOR_POLL); final Node endDateNode = poll.selectSingleNode(END_DATE); try { endDate = format.parse(endDateNode.getText()); logger.debug(String.format("%s: %s", END_DATE, format.format(endDate))); } catch (final ParseException e) { throw new RuntimeException(e); } final List<Node> responses = question.selectNodes(XPATH_RESPONSE_FROM_QUESTION); for (final Node response : responses) { final Node choiceNode = response.selectSingleNode(CHOICE); final String choice = choiceNode.getText(); if (President2012DataSetImporter.CANDIDATE_OPTIONS.contains(choice)) { final Node valueNode = response.selectSingleNode(VALUE); final Double value = Double.valueOf(valueNode.getText()); append(pageMap, endDate, choice, value); } } } httpResponse.close(); httpResponse = null; } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { if (httpResponse != null) { httpResponse.close(); } } return pageMap; }
From source file:com.mothsoft.alexis.engine.textual.ParseResponseMessageListener.java
License:Apache License
@SuppressWarnings("unchecked") private ParsedContent readResponse(final String xml) throws IOException { // turn the content into a ParsedContent complex object final Map<Term, Integer> termCountMap = new HashMap<Term, Integer>(); final Map<String, DocumentAssociation> associationCountMap = new HashMap<String, DocumentAssociation>(); final List<DocumentNamedEntity> entities = new ArrayList<DocumentNamedEntity>(); final List<Node> sentenceNodes = new ArrayList<Node>(); final SAXReader saxReader = new SAXReader(); org.dom4j.Document document;/*www. j a va 2 s . c om*/ try { document = saxReader.read(new StringReader(xml)); } catch (DocumentException e) { throw new IOException(e); } sentenceNodes.addAll(document.selectNodes("/document/sentences/s")); int sentencePosition = 1; for (final Node ith : sentenceNodes) { parseSentence(sentencePosition++, (Element) ith, termCountMap, associationCountMap); } sentenceNodes.clear(); final List<Node> nameNodes = new ArrayList<Node>(); nameNodes.addAll(document.selectNodes("/document/names/name")); for (final Node node : nameNodes) { final Element element = (Element) node; final String countString = element.attributeValue("count"); final Integer count = (countString == null ? 0 : Integer.valueOf(countString)); final String name = element.getTextTrim(); entities.add(new DocumentNamedEntity(name, count)); } // count up all the terms in the document Integer documentTermCount = 0; for (final Term ith : termCountMap.keySet()) { documentTermCount += (termCountMap.get(ith)); } // individual term count final List<DocumentTerm> documentTerms = new ArrayList<DocumentTerm>(); for (final Term term : termCountMap.keySet()) { final Integer count = termCountMap.get(term); documentTerms.add(new DocumentTerm(term, count)); } // sort the named entities by count Collections.sort(entities, new Comparator<DocumentNamedEntity>() { @Override public int compare(final DocumentNamedEntity e1, DocumentNamedEntity e2) { return -1 * e1.getCount().compareTo(e2.getCount()); } }); return new ParsedContent(associationCountMap.values(), documentTerms, entities, documentTermCount); }