📄 o3documenthelper.java
字号:
/** <p><code>sort</code> sorts the given List of Nodes * using an XPath expression as a {@link java.util.Comparator} * and optionally removing duplicates.</p> * * @param list is the list of Nodes to sort * @param xpathExpression is the XPath expression used for comparison * @param distinct if true then duplicate values (using the sortXPath for * comparisions) will be removed from the List */ public static void sort(List list, String xpathExpression, boolean distinct) throws Exception { XPath xpath = createXPath(xpathExpression); xpath.sort(list, distinct); } /** <p><code>parseText</code> parses the given text as an XML document * and returns the newly created Document. * * @param text is the XML text to be parsed * @return a newly parsed Document * @throws org.dom4j.DocumentException if the document could not be parsed */ public static Document parseText(String text) throws DocumentException { try { return getDocumentBuilder().parse(text); } catch (Exception e) { throw new DocumentException(e); } } public static Document parseText(String text, String objectName) throws DocumentException { try { return getDocumentBuilder().parse(text, objectName); } catch (Exception e) { throw new DocumentException(e); } } /** * Since an InputStream is not serializable we convert the content to a String * This might cause memory issues on big documents * @param in * @return * @throws DocumentException */ public static Document parse(InputStream in) throws DocumentException { //return getDocumentBuilder().parse(in); return parse(new InputStreamReader(in)); } /** * Since an InputStream is not serializable we convert the content to a String * This might cause memory issues on big documents * @param in * @return * @throws DocumentException */ public static Document parse(InputStream in, String objectName) throws DocumentException { return parse(new InputStreamReader(in), objectName); } /** * Since a Reader is not serializable we convert the content to a String * This might cause memory issues on big documents * @param reader * @return * @throws DocumentException */ public static Document parse(Reader reader, String objectName) throws DocumentException { try { return getDocumentBuilder().parse(convertToString(reader), objectName); } catch (Exception e) { throw new DocumentException(e); } } /** * Since a Reader is not serializable we convert the content to a String * This might cause memory issues on big documents * @param reader * @return * @throws DocumentException */ public static Document parse(Reader reader) throws DocumentException { try { //return getDocumentBuilder().parse(reader); return getDocumentBuilder().parse(convertToString(reader)); } catch (Exception e) { throw new DocumentException(e); } } private static String convertToString(Reader reader) throws IOException { LineNumberReader lineReader = new LineNumberReader(reader); StringBuffer buf = new StringBuffer(); String temp; while ((temp = lineReader.readLine()) != null) { buf.append(temp); } lineReader.close(); return buf.toString(); } /** * This will probably not work but something to this effect needs to exist * in order to send the stream content through the pipe over to the server * @param in * @return * @throws Exception */ public static Document parseRemote(InputStream in) throws Exception { DocumentBuilder builder = getDocumentBuilder(); SocketAddress address = builder.prepareStreamParsing(); Socket socket = new Socket(); socket.connect(address); OutputStream out = socket.getOutputStream(); byte[] buf = new byte[8092]; for (int i = 0; (i = in.read(buf)) != -1;) { out.write(buf, 0, i); } out.flush(); out.close(); in.close(); Document result = builder.getResult(); return result; } private static DocumentBuilder getDocumentBuilder() throws Exception { if (builder == null) { builder = (DocumentBuilder) getDb().objectForName(DocumentBuilder.OBJECT_NAME); if (builder == null) { builder = DocumentBuilderImpl.create(getDb()); } } return builder; } /** * Should not be used on the client side since the use will be quite slow * (every sax event will result in a remote call). But if we are using LocalDatabase * this is not the case so we might as well expose it since it gives much more flexibility * @return a SAXREader used for SAX based parsing */ public static SAXReader getSAXReader() { return new O3SAXReader(getDb(), factory); } /** <p>makeElement</p> a helper method which navigates from the * given Document or Element node to some Element using the path * expression, creating any necessary elements along the way. * For example the path <code>a/b/c</code> would get the first * child <a> element, which would be created if it did not * exist, then the next child <b> and so on until finally a * <c> element is returned. * * @param source is the Element or Document to start navigating from * @param path is a simple path expression, seperated by '/' which denotes * the path from the source to the resulting element such as a/b/c * * @return the first Element on the given path which either already * existed on the path or were created by this method. */ public static Element makeElement(Branch source, String path) { StringTokenizer enum = new StringTokenizer(path, "/"); Element parent; if (source instanceof Document) { Document document = (Document) source; parent = document.getRootElement(); // lets throw a NoSuchElementException // if we are given an empty path String name = enum.nextToken(); if (parent == null) { parent = document.addElement(name); } } else { parent = (Element) source; } Element element = null; while (enum.hasMoreTokens()) { String name = enum.nextToken(); if (name.indexOf(':') > 0) { element = parent.element(parent.getQName(name)); } else { element = parent.element(name); } if (element == null) { element = parent.addElement(name); } parent = element; } return element; }}/* * Redistribution and use of this software and associated documentation * ("Software"), with or without modification, are permitted provided * that the following conditions are met: * * 1. Redistributions of source code must retain copyright * statements and notices. Redistributions must also contain a * copy of this document. * * 2. Redistributions in binary form must reproduce the * above copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * * 3. The name "DOM4J" must not be used to endorse or promote * products derived from this Software without prior written * permission of MetaStuff, Ltd. For written permission, * please contact dom4j-info@metastuff.com. * * 4. Products derived from this Software may not be called "DOM4J" * nor may "DOM4J" appear in their names without prior written * permission of MetaStuff, Ltd. DOM4J is a registered * trademark of MetaStuff, Ltd. * * 5. Due credit should be given to the DOM4J Project * (http://dom4j.org/). * * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. * * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * $Id: O3DocumentHelper.java,v 1.3 2003/11/02 18:10:02 per_nyfelt Exp $ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -