📄 wicutils.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . 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. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS 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 THE COPYRIGHT OWNER OR 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. * * $Id: WicUtils.java,v 1.5 2005/05/17 16:40:28 jmettraux Exp $ *///// WicUtils.java//// john.mettraux@openwfe.org//// generated with // jtmpl 1.0.04 31.10.2002 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.engine.impl.workitem.xml;import openwfe.org.engine.workitem.CodingException;/** * WorkItem Coder Utils. * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Date: 2005/05/17 16:40:28 $ * <br>$Id: WicUtils.java,v 1.5 2005/05/17 16:40:28 jmettraux Exp $ </font> * * @author john.mettraux@openwfe.org */public abstract class WicUtils{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(WicUtils.class.getName()); // // METHODS public static org.jdom.Element enforceElement (final Object o, final String nodeName) throws CodingException { if (o == null) { throw new CodingException ("Null element. Should be '"+nodeName+"'"); } if ( ! (o instanceof org.jdom.Element)) { throw new CodingException ("Object to decode should be of class 'org.jdom.Element'. "+ "Received a '"+o.getClass().getName()+"' instead."); } final org.jdom.Element elt = (org.jdom.Element)o; if ( ! elt.getName().equals(nodeName)) { throw new CodingException ("Waited for a <"+nodeName+"> element not a <"+ elt.getName()+"> one."); } return elt; } public static org.jdom.Element getFirstChild (final org.jdom.Element elt) { //log.debug("\n"+toString(elt)); /* java.util.List children = elt.getChildren(); if (children == null || children.size() < 1) return null; return (org.jdom.Element)children.get(0); */ if (elt == null) return null; final java.util.Iterator it = elt.getContent().iterator(); while (it.hasNext()) { final Object o = it.next(); if (o instanceof org.jdom.Element) return (org.jdom.Element)o; } return null; } public static String fetchTextContent (org.jdom.Element elt) throws CodingException { if (elt.getContent().size() < 1) { //throw new CodingException // ("Element without any content. Cannot turn into an attribute."); //log.debug("fetchTextContent() returning ''"); return ""; } //log.debug("fetchTextContent() size : "+elt.getContent().size()); //java.util.Iterator iit = elt.getContent().iterator(); //while (iit.hasNext()) //{ // final Object o = iit.next(); // log.debug("fetchTextContent() - "+o.getClass().getName()); //} java.util.Iterator it = elt.getContent().iterator(); while (it.hasNext()) { Object content = it.next(); if (content instanceof org.jdom.Text) { //log.debug("fetchTextContent() returning text"); return ((org.jdom.Text)content).getText().trim(); } else if (content instanceof org.jdom.CDATA) { //log.debug("fetchTextContent() returning cdata"); return ((org.jdom.CDATA)content).getText().trim(); } } throw new CodingException ("Element doesn't contain any textual info "+ "which may be used to build an attribute"); } /** * Returns the first CDATA section held in the given elt as a String. */ public static String fetchCdataContent (final org.jdom.Element elt) throws CodingException { if (elt.getContent().size() < 1) { return ""; } final java.util.Iterator it = elt.getContent().iterator(); while (it.hasNext()) { final Object o = it.next(); if (o instanceof org.jdom.CDATA) return ((org.jdom.CDATA)o).getText().trim(); } throw new CodingException ("Element doesn't contain a CDATA section"); } public static void setAttribute (org.jdom.Element elt, String attributeName, String attributeValue) { if (attributeValue == null) return; elt.setAttribute(attributeName, attributeValue); } public static org.jdom.Element fetchElementFromStream (java.io.InputStream is) throws CodingException { org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(); org.jdom.Document doc = null; try { doc = builder.build(is); is.close(); } catch (Exception e) { throw new CodingException ("Failed to decode xml element", e); } return doc.getRootElement(); } public static long getLong (org.jdom.Element elt, String attributeName) throws CodingException { return getLong(elt, attributeName, null); } public static long getLong (org.jdom.Element elt, String attributeName, long defaultValue) throws CodingException { return getLong(elt, attributeName, new Long(defaultValue)); } private static long getLong (org.jdom.Element elt, String attributeName, Long defaultValue) throws CodingException { if (elt == null || attributeName == null) { throw new CodingException ("Cannot extract a long from a null element "+ "or a from a 'null' attribute"); } String sLong = elt.getAttributeValue(attributeName); try { return Long.parseLong(sLong); } catch (NumberFormatException nfe) { if (defaultValue != null) return defaultValue.longValue(); throw new CodingException ("Cannot extract a long from '"+sLong+"'"); } } public static String toString (org.jdom.Element elt) { if (elt == null) return "<nil/>"; try { org.jdom.output.XMLOutputter out = new org.jdom.output.XMLOutputter(); java.io.StringWriter writer = new java.io.StringWriter(); out.output(elt, writer); writer.flush(); return writer.toString(); } catch (java.io.IOException ie) { return "Failed to output as string xml for element named '"+ elt.getName()+"' : "+ie; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -