casts a value to a XML Text Array

@param doc XML Document
@param o Object to cast
@return XML Text Array
@throws PageException
518800
public static Text[] toTextArray(Document doc, Object o) throws PageException {
	// Node[]
	if (o instanceof Node[]) {
	    Node[] nodes = (Node[]) o;
	    if (_isAllOfSameType(nodes, Node.TEXT_NODE)) return (Text[]) nodes;

	    Text[] textes = new Text[nodes.length];
	    for (int i = 0; i < nodes.length; i++) {
		textes[i] = toText(doc, nodes[i]);
	    }
	    return textes;
	}
	// Collection
	else if (o instanceof Collection) {
	    Collection coll = (Collection) o;
	    Iterator<Object> it = coll.valueIterator();
	    List<Text> textes = new ArrayList<Text>();
	    while (it.hasNext()) {
		textes.add(toText(doc, it.next()));
	    }
	    return textes.toArray(new Text[textes.size()]);
	}
	// Node Map and List
	Node[] nodes = _toNodeArray(doc, o);
	if (nodes != null) return toTextArray(doc, nodes);
	// Single Text Node
	try {
	    return new Text[] { toText(doc, o) };
	}
	catch (ExpressionException e) {
	    throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Text Array");
	}
    }
1152584
@Override
  public ScriptElement createScriptElement(Document doc, String source) {
    ScriptElement elem = (ScriptElement) createElement(doc, "script");
    elem.setInnerText(source);
    return elem;
  }
1213634
public void store(Document doc, Resource res) throws IOException {
	try {
	    XMLCaster.writeTo(doc, res);
	}
	catch (PageException e) {
	    throw ExceptionUtil.toIOException(e);
	}

	/*
	 * OutputFormat format = new OutputFormat(doc, null, true); format.setLineSeparator("\r\n");
	 * format.setLineWidth(72);
	 * 
	 * OutputStream os=null; try { XMLSerializer serializer = new
	 * XMLSerializer(os=res.getOutputStream(), format); serializer.serialize(doc.getDocumentElement());
	 * } finally { IOUtil.closeEL(os); }
	 */
    }
955108
public static Object castTo(PageContext pc, short type, Object o) throws PageException {
	if (type == CFTypes.TYPE_ANY) return o;
	else if (type == CFTypes.TYPE_ARRAY) return toArray(o);
	else if (type == CFTypes.TYPE_BOOLEAN) return toBoolean(o);
	else if (type == CFTypes.TYPE_BINARY) return toBinary(o);
	else if (type == CFTypes.TYPE_DATETIME) return DateCaster.toDateAdvanced(o, pc.getTimeZone());
	else if (type == CFTypes.TYPE_NUMERIC) return toDouble(o);
	else if (type == CFTypes.TYPE_QUERY) return toQuery(o);
	else if (type == CFTypes.TYPE_QUERY_COLUMN) return toQueryColumn(o);
	else if (type == CFTypes.TYPE_STRING) return toString(o);
	else if (type == CFTypes.TYPE_STRUCT) return toStruct(o);
	else if (type == CFTypes.TYPE_TIMESPAN) return toTimespan(o);
	else if (type == CFTypes.TYPE_UUID) return toGUId(o);
	else if (type == CFTypes.TYPE_UUID) return toUUId(o);
	else if (type == CFTypes.TYPE_VARIABLE_NAME) return toVariableName(o);
	else if (type == CFTypes.TYPE_VOID) return toVoid(o);
	else if (type == CFTypes.TYPE_FUNCTION) return toFunction(o);
	else if (type == CFTypes.TYPE_XML) return toXML(o);
	// ext.img else if(type==CFTypes.TYPE_IMAGE) return ImageUtil.toImage(pc,o,true);

	if (type == CFTypes.TYPE_UNDEFINED) throw new ExpressionException("type isn't defined (TYPE_UNDEFINED)");
	throw new ExpressionException("invalid type [" + type + "]");
    }
658478
public static Element[] toElementArray(Document doc, Object o) throws PageException {
	// Node[]
	if (o instanceof Node[]) {
	    Node[] nodes = (Node[]) o;
	    if (_isAllOfSameType(nodes, Node.ELEMENT_NODE)) return (Element[]) nodes;

	    Element[] elements = new Element[nodes.length];
	    for (int i = 0; i < nodes.length; i++) {
		elements[i] = toElement(doc, nodes[i]);
	    }
	    return elements;
	}
	// Collection
	else if (o instanceof Collection) {
	    Collection coll = (Collection) o;
	    Iterator<Object> it = coll.valueIterator();
	    List<Element> elements = new ArrayList<Element>();
	    while (it.hasNext()) {
		elements.add(toElement(doc, it.next()));
	    }
	    return elements.toArray(new Element[elements.size()]);
	}
	// Node Map and List
	Node[] nodes = _toNodeArray(doc, o);
	if (nodes != null) return toElementArray(doc, nodes);
	// Single Text Node
	try {
	    return new Element[] { toElement(doc, o) };
	}
	catch (ExpressionException e) {
	    throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Element Array");
	}
    }
1228190
public static NodeList toNodeList(Object o) throws PageException {
	// print.ln("nodeList:"+o);
	if (o instanceof NodeList) {
	    return (NodeList) o;
	}
	else if (o instanceof ObjectWrap) {
	    return toNodeList(((ObjectWrap) o).getEmbededObject());
	}
	throw new CasterException(o, "NodeList");
    }
408611
public static void serializeXMLDocument (Document doc, Writer output) throws TransformerException {
        // serialize XML document using identity transformation
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(output);
        trans.transform(source, result);
    }
200236
public static Character toCharacter(Object o) throws PageException {
	if (o instanceof Character) return (Character) o;
	return new Character(toCharValue(o));

    }
294189
private synchronized Document asXmlDocument(String propertyName)
			throws IOException, IllegalArgumentException {
		Document doc;
		try {
			doc = DocumentBuilderFactory
					.newInstance()
					.newDocumentBuilder()
					.newDocument();
		} catch (ParserConfigurationException pe) {
			throw new IOException(pe);
		}

		Element conf = doc.createElement("configuration");
		doc.appendChild(conf);
		conf.appendChild(doc.createTextNode("\n"));
		handleDeprecation(); //ensure properties is set and deprecation is handled

		if(!Strings.isNullOrEmpty(propertyName)) {
			if (!properties.containsKey(propertyName)) {
				// given property not found, illegal argument
				throw new IllegalArgumentException("Property " +
						propertyName + " not found");
			} else {
				// given property is found, write single property
				appendXMLProperty(doc, conf, propertyName);
				conf.appendChild(doc.createTextNode("\n"));
			}
		} else {
			// append all elements
			for (Enumeration<Object> e = properties.keys(); e.hasMoreElements();) {
				appendXMLProperty(doc, conf, (String)e.nextElement());
				conf.appendChild(doc.createTextNode("\n"));
			}
		}
		return doc;
	}
1150890
public static Component toComponent(Object o) throws PageException {
	if (o instanceof Component) return (Component) o;
	else if (o instanceof ObjectWrap) {
	    return toComponent(((ObjectWrap) o).getEmbededObject());
	}
	throw new CasterException(o, "Component");
    }
969220
public static Object toGUId(Object o) throws PageException {
	String str = toString(o);
	if (!Decision.isGUId(str)) throw new ExpressionException("can't cast [" + str + "] to guid value");
	return str;
    }
503715
Document convertSelectorToDocument(Document selector) {
        Document document = new Document();
        for (String key : selector.keySet()) {
            if (key.startsWith("$")) {
                continue;
            }

            Object value = selector.get(key);
            if (!Utils.containsQueryExpression(value)) {
                Utils.changeSubdocumentValue(document, key, value, (AtomicReference<Integer>) null);
            }
        }
        return document;
    }
807468
public static String getContentByDoc(Document doc) throws Exception {
        ContentExtractor ce = new ContentExtractor(doc);
        return ce.getContentElement().text();
    }
410137
@Deprecated
    public static Node toNode(Object o) throws PageException {
	if (o instanceof XMLStruct) return ((XMLStruct) o).toNode();
	if (o instanceof Node) return (Node) o;
	throw new CasterException(o, "node");
    }
1279203
public static Short toShort(Object o) throws PageException {
	if (o instanceof Short) return (Short) o;
	return Short.valueOf(toShortValue(o));

    }
917886
public static Element getDocumentElement(Document doc) {
        Element docElement = null;
        if (doc != null) {
            docElement = doc.getDocumentElement();
            if (docElement != null) {
                // optional, but recommended
                // see here: http://bit.ly/1h2Ybzb
                docElement.normalize();
            }
        }
        return docElement;
    }
585034
public static long toLongValue(Object o) throws PageException {
	if (o instanceof Boolean) return ((((Boolean) o).booleanValue()) ? 1L : 0L);
	else if (o instanceof Number) return (((Number) o).longValue());
	else if (o instanceof CharSequence) {
	    String str = o.toString();
	    try {
		return Long.parseLong(str);
	    }
	    catch (NumberFormatException nfe) {
		return (long) toDoubleValue(str);
	    }
	}
	else if (o instanceof Character) return (((Character) o).charValue());
	else if (o instanceof Castable) return (long) ((Castable) o).castToDoubleValue();
	else if (o instanceof ObjectWrap) return toLongValue(((ObjectWrap) o).getEmbededObject());

	throw new CasterException(o, "long");
    }