List of usage examples for org.dom4j Document addProcessingInstruction
Document addProcessingInstruction(String target, Map<String, String> data);
From source file:at.jabberwocky.impl.core.io.PacketReader.java
private Document parseDocument() throws DocumentException, IOException, XmlPullParserException { DocumentFactory df = docFactory;/*from w w w.ja v a 2 s.co m*/ Document document = df.createDocument(); Element parent = null; XmlPullParser pp = parser; int count = 0; while (true) { int type = -1; type = pp.nextToken(); switch (type) { case XmlPullParser.PROCESSING_INSTRUCTION: { String text = pp.getText(); int loc = text.indexOf(" "); if (loc >= 0) { document.addProcessingInstruction(text.substring(0, loc), text.substring(loc + 1)); } else { document.addProcessingInstruction(text, ""); } break; } case XmlPullParser.COMMENT: { if (parent != null) { parent.addComment(pp.getText()); } else { document.addComment(pp.getText()); } break; } case XmlPullParser.CDSECT: { String text = pp.getText(); if (parent != null) { parent.addCDATA(text); } else { if (text.trim().length() > 0) { throw new DocumentException("Cannot have text content outside of the root document"); } } break; } case XmlPullParser.ENTITY_REF: { String text = pp.getText(); if (parent != null) { parent.addText(text); } else { if (text.trim().length() > 0) { throw new DocumentException("Cannot have an entityref outside of the root document"); } } break; } case XmlPullParser.END_DOCUMENT: { return document; } case XmlPullParser.START_TAG: { QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace()) : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace()); Element newElement = null; // Do not include the namespace if this is the start tag of a new packet // This avoids including "jabber:client", "jabber:server" or // "jabber:component:accept" if ("jabber:client".equals(qname.getNamespaceURI()) || "jabber:server".equals(qname.getNamespaceURI()) || "jabber:component:accept".equals(qname.getNamespaceURI()) || "http://jabber.org/protocol/httpbind".equals(qname.getNamespaceURI())) { newElement = df.createElement(pp.getName()); } else { newElement = df.createElement(qname); } int nsStart = pp.getNamespaceCount(pp.getDepth() - 1); int nsEnd = pp.getNamespaceCount(pp.getDepth()); for (int i = nsStart; i < nsEnd; i++) { if (pp.getNamespacePrefix(i) != null) { newElement.addNamespace(pp.getNamespacePrefix(i), pp.getNamespaceUri(i)); } } for (int i = 0; i < pp.getAttributeCount(); i++) { QName qa = (pp.getAttributePrefix(i) == null) ? df.createQName(pp.getAttributeName(i)) : df.createQName(pp.getAttributeName(i), pp.getAttributePrefix(i), pp.getAttributeNamespace(i)); newElement.addAttribute(qa, pp.getAttributeValue(i)); } if (parent != null) { parent.add(newElement); } else { document.add(newElement); } parent = newElement; count++; break; } case XmlPullParser.END_TAG: { if (parent != null) { parent = parent.getParent(); } count--; if (count < 1) { return document; } break; } case XmlPullParser.TEXT: { String text = pp.getText(); if (parent != null) { parent.addText(text); } else { if (text.trim().length() > 0) { throw new DocumentException("Cannot have text content outside of the root document"); } } break; } default: } } }
From source file:com.flaptor.hounder.searcher.OpenSearch.java
License:Apache License
/** * Creates a OpenSearch's compatible DOM document. * The generated dom contains only valid xml characters (infringing chars are removed). * Compliant with OpenSearch 1.0 with most of the Nutch 0.8.1 extensions. * @param baseUrl the url of the webapp/*from w w w.j a v a2 s . c o m*/ * @param htmlSearcher the name of the component (servlet/jsp) that returns the search results in an HTML page * @param opensearchSearcher the name of the component (servlet/jsp) that returns the search results in an OpenSearch RSS page * @param extraParams the parameters present in the request, not passed explicitly (such as sort, reverse, etc.) * @param queryString the query string, as entered by the user * @param start the offset of the first result * @param count the number of results requested (the actual number of results found may be smaller) * @param sr the SearchResults structure containing the result of performing the query * @return a DOM document * <br>An empty sr argument means that no results were found. */ public static final Document buildDom_1_0(String baseUrl, String htmlSearcher, String opensearchSearcher, String extraParams, String queryString, int start, int count, GroupedSearchResults sr, int status, String statusMessage, boolean useXslt) { String encodedQuery = null; try { encodedQuery = URLEncoder.encode(queryString, "UTF-8"); } catch (UnsupportedEncodingException e) { // Should never happen! encodedQuery = ""; } Document dom = DocumentHelper.createDocument(); if (useXslt) { Map<String, String> map = new HashMap<String, String>(); map.put("type", "text/xsl"); map.put("href", xsltPath); dom.addProcessingInstruction("xml-stylesheet", map); } Namespace opensearchNs = DocumentHelper.createNamespace("opensearch", XMLNS_A9_OPENSEARCH_1_0); Namespace hounderNs = DocumentHelper.createNamespace("hounder", XMLNS_HOUNDER_OPENSEARCH_1_0); Element root; Element channel; if (!useXslt) { root = dom.addElement("rss").addAttribute("version", "2.0"); channel = root.addElement("channel"); } else { channel = dom.addElement("searchResults"); root = channel; } root.add(opensearchNs); root.add(hounderNs); channel.addElement("title").addText(titlePrefix + " " + DomUtil.filterXml(queryString)); channel.addElement("link") .addText(baseUrl + "/" + htmlSearcher + "?query=" + encodedQuery + "&start=" + start + extraParams); channel.addElement("description").addText(descPrefix + " " + DomUtil.filterXml(queryString)); channel.addElement(QName.get("totalResults", opensearchNs)) .addText(Integer.toString(sr.totalGroupsEstimation())); channel.addElement(QName.get("startIndex", opensearchNs)).addText(Integer.toString(start)); channel.addElement(QName.get("itemsPerPage", opensearchNs)).addText(Integer.toString(count)); channel.addElement(QName.get("query", hounderNs)).addText(DomUtil.filterXml(queryString)); AQuery suggestedQuery = sr.getSuggestedQuery(); if (null != suggestedQuery) { channel.addElement(QName.get("suggestedQuery", hounderNs)) .addText(DomUtil.filterXml(suggestedQuery.toString())); } channel.addElement(QName.get("status", hounderNs)).addText(Integer.toString(status)); channel.addElement(QName.get("statusDesc", hounderNs)).addText(statusMessage); if (sr.lastDocumentOffset() > 0) { channel.addElement(QName.get("nextPage", hounderNs)).addText(baseUrl + "/" + opensearchSearcher + "?query=" + encodedQuery + "&start=" + (sr.lastDocumentOffset()) + extraParams); } for (int i = 0; i < sr.groups(); i++) { Vector<org.apache.lucene.document.Document> docs = sr.getGroup(i).last(); Element parent = null; for (int j = 0; j < docs.size(); j++) { org.apache.lucene.document.Document doc = sr.getGroup(i).last().get(j); if (0 == j) {// j=0 is head of group. j>0 is tail parent = createAndAddElement(doc, channel, hounderNs); } else { createAndAddElement(doc, parent, hounderNs); } } } return dom; }
From source file:com.flaptor.hounder.searcher.XmlResults.java
License:Apache License
/** * Creates a XML search results document (verbose version). * The generated dom contains only valid xml characters (infringing chars are removed). * @param queryString the query string, as entered by the user * @param start the offset of the first result * @param count the number of results requested (the actual number of results found may be smaller) * @param orderBy the field by which the results are sorted * @param sr the GroupedSearchResults structure containing the result of performing the query * @param status the code returned by the searcher * @param statusMsg the status description * @param xsltUri the uri for the xslt used to process the xml on the client side, * or null if no client-side processing is needed * @param rangeField field for which a range filter will be applied, or null if no filter used. * @param rangeStart start value for the range filter. * @param rangeEnd end value for the range filter. * @param params a map of parameters sent to the searcher with the request. * @return a DOM document//ww w. jav a 2 s . c om * <br>An empty sr argument means that no results were found. */ public static final Document buildXml(String queryString, int start, int count, String orderBy, GroupedSearchResults sr, int status, String statusMsg, String xsltUri, String rangeField, String rangeStart, String rangeEnd, Map<String, String[]> params) { Document dom = DocumentHelper.createDocument(); if (null != xsltUri) { Map<String, String> map = new HashMap<String, String>(); map.put("type", "text/xsl"); map.put("href", xsltUri); dom.addProcessingInstruction("xml-stylesheet", map); } Element root; Element group; root = dom.addElement("SearchResults"); root.addElement("totalResults").addText(Integer.toString(sr.totalResults())); root.addElement("totalGroupsEstimation").addText(Integer.toString(sr.totalGroupsEstimation())); if (count > 0) { root.addElement("startIndex").addText(Integer.toString(start)); } if (count > 0) { root.addElement("itemsPerPage").addText(Integer.toString(count)); } if (null != orderBy) { root.addElement("orderBy").addText(DomUtil.filterXml(orderBy)); } if (null != queryString) { root.addElement("query").addText(DomUtil.filterXml(queryString)); } if (null != rangeField) { root.addElement("filter").addAttribute("field", rangeField).addAttribute("start", rangeStart) .addAttribute("end", rangeEnd); } if (null != params) { for (String key : params.keySet()) { if (null == root.selectSingleNode(key)) { String val = params.get(key)[0]; root.addElement(key).addText(val); } } } AQuery suggestedQuery = sr.getSuggestedQuery(); if (null != suggestedQuery) { root.addElement("suggestedQuery") .addText(DomUtil.filterXml(((LazyParsedQuery) suggestedQuery).getQueryString())); } root.addElement("status").addText(Integer.toString(status)); root.addElement("statusDesc").addText(statusMsg); for (int i = 0; i < sr.groups(); i++) { String name = sr.getGroup(i).first(); group = root.addElement("group").addAttribute("name", name); Vector<org.apache.lucene.document.Document> docs = sr.getGroup(i).last(); for (int j = 0; j < docs.size(); j++) { org.apache.lucene.document.Document doc = sr.getGroup(i).last().get(j); createAndAddElement(doc, group); } } return dom; }
From source file:com.pactera.edg.am.metamanager.extractor.util.Dom4jWriter.java
License:Open Source License
public static Document createDocument() { Document document = DocumentHelper.createDocument(); Map<String, String> map = new TreeMap<String, String>(); map.put("name", ""); map.put("signature", ""); map.put("version", "1.0.1"); document.addProcessingInstruction("mm", map); return document; }
From source file:nl.tue.gale.common.XPPEntityReader.java
License:Open Source License
protected Document parseDocument() throws DocumentException, IOException, XmlPullParserException { DocumentFactory df = getDocumentFactory(); Document document = df.createDocument(); Element parent = null;/*from w ww.ja va 2s . c o m*/ XmlPullParser pp = getXPPParser(); pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); pp.setFeature(XmlPullParser.FEATURE_PROCESS_DOCDECL, false); defineEntities(pp); while (true) { int type = pp.nextToken(); switch (type) { case XmlPullParser.PROCESSING_INSTRUCTION: { String text = pp.getText(); int loc = text.indexOf(" "); if (loc >= 0) { String target = text.substring(0, loc); String txt = text.substring(loc + 1); document.addProcessingInstruction(target, txt); } else { document.addProcessingInstruction(text, ""); } break; } case XmlPullParser.COMMENT: { if (parent != null) { parent.addComment(pp.getText()); } else { document.addComment(pp.getText()); } break; } case XmlPullParser.CDSECT: { if (parent != null) { parent.addCDATA(pp.getText()); } else { String msg = "Cannot have text content outside of the " + "root document"; throw new DocumentException(msg); } break; } case XmlPullParser.ENTITY_REF: if (parent != null) { if (pp.getName().equals("gt")) { parent.addText(">"); } else if (pp.getName().equals("lt")) { parent.addText("<"); } else if (pp.getName().equals("amp")) { parent.addText("&"); } else if (pp.getName().equals("quot")) { parent.addText("\""); } else parent.addEntity(pp.getName(), "&" + pp.getName() + ";"); } break; case XmlPullParser.END_DOCUMENT: return document; case XmlPullParser.START_TAG: { QName qname = (pp.getPrefix() == null) ? df.createQName(pp.getName(), pp.getNamespace()) : df.createQName(pp.getName(), pp.getPrefix(), pp.getNamespace()); Element newElement = df.createElement(qname); int nsStart = pp.getNamespaceCount(pp.getDepth() - 1); int nsEnd = pp.getNamespaceCount(pp.getDepth()); for (int i = nsStart; i < nsEnd; i++) { if (pp.getNamespacePrefix(i) != null) { newElement.addNamespace(pp.getNamespacePrefix(i), pp.getNamespaceUri(i)); } } for (int i = 0; i < pp.getAttributeCount(); i++) { QName qa = (pp.getAttributePrefix(i) == null) ? df.createQName(pp.getAttributeName(i)) : df.createQName(pp.getAttributeName(i), pp.getAttributePrefix(i), pp.getAttributeNamespace(i)); newElement.addAttribute(qa, pp.getAttributeValue(i)); } if (parent != null) { parent.add(newElement); } else { document.add(newElement); } parent = newElement; break; } case XmlPullParser.END_TAG: { if (parent != null) { parent = parent.getParent(); } break; } case XmlPullParser.TEXT: { String text = pp.getText(); if (parent != null) { parent.addText(text); } else { String msg = "Cannot have text content outside of the " + "root document"; throw new DocumentException(msg); } break; } default: break; } } }