casts a value to a XML Element Array

@param doc XML Document
@param o Object to cast
@return XML Comment Array
@throws PageException
187945
public static Node[] toNodeArray(Document doc, Object o) throws PageException {
	if (o instanceof Node) return new Node[] { (Node) o };
	// Node[]
	if (o instanceof Node[]) {
	    return (Node[]) o;
	}
	// Collection
	else if (o instanceof Collection) {
	    Collection coll = (Collection) o;
	    Iterator<Object> it = coll.valueIterator();
	    List<Node> nodes = new ArrayList<Node>();
	    while (it.hasNext()) {
		nodes.add(toNode(doc, it.next(), false));
	    }
	    return nodes.toArray(new Node[nodes.size()]);
	}
	// Node Map and List
	Node[] nodes = _toNodeArray(doc, o);
	if (nodes != null) return nodes;
	// Single Text Node
	try {
	    return new Node[] { toNode(doc, o, false) };
	}
	catch (ExpressionException e) {
	    throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Node Array");
	}
    }
2251
public static String toXml(com.greenpepper.document.Document document) throws IOException
	{
		StringWriter sw = new StringWriter();

		XmlReport xmlReport = XmlReport.newInstance("");
		xmlReport.generate( document );
		xmlReport.printTo( sw );

		return sw.toString();
	}
582537
public static Object castTo(PageContext pc, String type, Object o, boolean alsoPattern) throws PageException {
	type = StringUtil.toLowerCase(type).trim();
	if (type.length() > 2) {
	    char first = type.charAt(0);
	    switch (first) {
	    case 'a':
		if (type.equals("any")) {
		    return o;
		}
		else if (type.equals("array")) {
		    return toArray(o);
		}
		break;
	    case 'b':
		if (type.equals("boolean") || type.equals("bool")) {
		    return toBoolean(o);
		}
		else if (type.equals("binary")) {
		    return toBinary(o);
		}
		else if (type.equals("byte[]")) {
		    return toBinary(o);
		}
		else if (type.equals("base64")) {
		    return toBase64(o, null);
		}
		else if (type.equals("bigdecimal") || type.equals("big_decimal")) {
		    return toBigDecimal(o);
		}
		else if (type.equals("biginteger") || type.equals("big_integer")) {
		    return toBigInteger(o);
		}
		break;
	    case 'c':
		if (alsoPattern && type.equals("creditcard")) {
		    return toCreditCard(o);
		}
		break;
	    case 'd':
		if (type.equals("date")) {
		    return DateCaster.toDateAdvanced(o, pc.getTimeZone());
		}
		else if (type.equals("datetime")) {
		    return DateCaster.toDateAdvanced(o, pc.getTimeZone());
		}
		else if (type.equals("double")) {
		    return toDouble(o);
		}
		else if (type.equals("decimal")) {
		    return toDecimal(o);
		}
		break;
	    case 'e':
		if (type.equals("eurodate")) {
		    return DateCaster.toEuroDate(o, pc.getTimeZone());
		}
		else if (alsoPattern && type.equals("email")) {
		    return toEmail(o);
		}
		break;
	    case 'f':
		if (type.equals("float")) {
		    return toDouble(o);
		}
		else if (type.equals("function")) {
		    return toFunction(o);
		}
		break;
	    case 'g':
		if (type.equals("guid")) {
		    return toGUId(o);
		}
		break;
	    case 'i':
		if (type.equals("integer") || type.equals("int")) {
		    return toInteger(o);
		}
		break;
	    case 'l':
		if (type.equals("long")) {
		    return toLong(o);
		}
		break;
	    case 'n':
		if (type.equals("numeric")) {
		    return toDouble(o);
		}
		else if (type.equals("number")) {
		    return toDouble(o);
		}
		else if (type.equals("node")) {
		    return toXML(o);
		}
		break;
	    case 'o':
		if (type.equals("object")) {
		    return o;
		}
		else if (type.equals("other")) {
		    return o;
		}
		break;
	    case 'p':
		if (alsoPattern && type.equals("phone")) {
		    return toPhone(o);
		}
		break;
	    case 'q':
		if (type.equals("query")) {
		    return toQuery(o);
		}
		break;
	    case 's':
		if (type.equals("string")) {
		    return toString(o);
		}
		else if (type.equals("struct")) {
		    return toStruct(o);
		}
		else if (type.equals("short")) {
		    return toShort(o);
		}
		else if (alsoPattern && (type.equals("ssn") || type.equals("social_security_number"))) {
		    return toSSN(o);
		}
		break;
	    case 't':
		if (type.equals("timespan")) {
		    return toTimespan(o);
		}
		if (type.equals("time")) {
		    return DateCaster.toDateAdvanced(o, pc.getTimeZone());
		}
		if (alsoPattern && type.equals("telephone")) {
		    return toPhone(o);
		}
		break;
	    case 'u':
		if (type.equals("uuid")) {
		    return toUUId(o);
		}
		if (alsoPattern && type.equals("url")) {
		    return toURL(o);
		}
		if (type.equals("usdate")) {
		    return DateCaster.toUSDate(o, pc.getTimeZone());
		    // return DateCaster.toDate(o,pc.getTimeZone());
		}
		break;
	    case 'v':
		if (type.equals("variablename")) {
		    return toVariableName(o);
		}
		else if (type.equals("void")) {
		    return toVoid(o);
		}
		else if (type.equals("variable_name")) {
		    return toVariableName(o);
		}
		else if (type.equals("variable-name")) {
		    return toVariableName(o);
		}
		break;
	    case 'x':
		if (type.equals("xml")) {
		    return toXML(o);
		}
	    case 'z':
		if (alsoPattern && (type.equals("zip") || type.equals("zipcode"))) {
		    return toZip(o);
		}
		break;
	    }
	}

	// <type>[]
	if (type.endsWith("[]")) {
	    String componentType = type.substring(0, type.length() - 2);
	    Object[] src = toNativeArray(o);
	    Array trg = new ArrayImpl();
	    for (int i = 0; i < src.length; i++) {
		if (src[i] == null) {
		    continue;
		}
		trg.setE(i + 1, castTo(pc, componentType, src[i], alsoPattern));
	    }
	    return trg;
	}

	return _castTo(pc, type, o);
    }
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");
    }
611467
public static Text toText(Document doc, Object o) throws PageException {
	if (o instanceof Text) return (Text) o;
	else if (o instanceof CharacterData) return doc.createTextNode(((CharacterData) o).getData());
	return doc.createTextNode(Caster.toString(o));
    }
696633
public static char toCharValue(Object o) throws PageException {
	if (o instanceof Character) return ((Character) o).charValue();
	else if (o instanceof Boolean) return (char) ((((Boolean) o).booleanValue()) ? 1 : 0);
	else if (o instanceof Double) return (char) (((Double) o).doubleValue());
	else if (o instanceof Number) return (char) (((Number) o).doubleValue());
	else if (o instanceof String) {
	    String str = o.toString();
	    if (str.length() > 0) return str.charAt(0);
	    throw new ExpressionException("can't cast empty string to a char");

	}
	else if (o instanceof ObjectWrap) {
	    return toCharValue(((ObjectWrap) o).getEmbededObject());
	}
	else if (o == null) return toCharValue("");
	throw new CasterException(o, "char");
    }
949976
public static Attr toAttr(Document doc, Object o) throws PageException {
	if (o instanceof Attr) return (Attr) o;
	if (o instanceof Struct && ((Struct) o).size() == 1) {
	    Struct sct = (Struct) o;
	    Entry<Key, Object> e = sct.entryIterator().next();
	    Attr attr = doc.createAttribute(e.getKey().getString());
	    attr.setValue(Caster.toString(e.getValue()));
	    return attr;
	}

	throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Attribute");
    }
676837
public static void writeXML(Document xmldocument, OutputStream stream) throws IOException {
		assert xmldocument != null : AssertMessages.notNullParameter(0);
		assert stream != null : AssertMessages.notNullParameter(1);
		writeNode(xmldocument, stream);
	}
1020687
public function generateXML (&$doc, $arr = FALSE, $xml = FALSE)
	{
		
		// If there is DOMElement reate the root element
		
		if ($xml === FALSE)
		{	
			$xml	= $doc->createElement (strtolower ($this->rootNode));	
		}
		
		// If there is no element set to the result
		
		if ($arr === FALSE)
		{
			$arr	= $this->result;
		}
			
		// For each element in the element types
		
		foreach ($arr as $key => $val)
		{
			
			// If the element is an object and the key is a number
			
			if (is_object ($val) && is_numeric ($key))
			{
				
				// Set key to class name
				
				$key	= get_class ($val);
				
			}
			
			// Else if there is an array key 'class'
			
			else if (is_array ($val) && array_key_exists ('class', $val))
			{
				
				// Set key to class name
				
				$key	= $val['class'];
				
				// Remove class key
				
				unset ($val['class']);
				
			}
			
			// Else if the key is a number
			
			else if (is_numeric ($key))
			{
				
				// Set key to string
				
				$key	= 'result_' . $key;
				
			}
			
			// Create a new element
			
			$element	= $doc->createElement (strtolower ($key));
			
			// If the value is an array generate XML
			
			if (is_array ($val))
			{
				$element	= $this->generateXML ($doc, $val, $element);
			}
			
			// Else set the element value
			
			else
			{
				$element->nodeValue	= htmlentities ($val);
			}
			
			// Append element
			
			$xml->appendChild ($element);
			
		}
		
		return $xml;
		
	}
75900
private synchronized Document asXmlDocument() throws IOException {
    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
    for (Enumeration<Object> e = properties.keys(); e.hasMoreElements();) {
      String name = (String)e.nextElement();
      Object object = properties.get(name);
      String value = null;
      if (object instanceof String) {
        value = (String) object;
      }else {
        continue;
      }
      Element propNode = doc.createElement("property");
      conf.appendChild(propNode);

      Element nameNode = doc.createElement("name");
      nameNode.appendChild(doc.createTextNode(name));
      propNode.appendChild(nameNode);

      Element valueNode = doc.createElement("value");
      valueNode.appendChild(doc.createTextNode(value));
      propNode.appendChild(valueNode);

      if (updatingResource != null) {
        String[] sources = updatingResource.get(name);
        if(sources != null) {
          for(String s : sources) {
            Element sourceNode = doc.createElement("source");
            sourceNode.appendChild(doc.createTextNode(s));
            propNode.appendChild(sourceNode);
          }
        }
      }

      conf.appendChild(doc.createTextNode("\n"));
    }
    return doc;
  }
850845
public static Attr[] toAttrArray(Document doc, Object o) throws PageException {
	// Node[]
	if (o instanceof Node[]) {
	    Node[] nodes = (Node[]) o;
	    if (_isAllOfSameType(nodes, Node.ATTRIBUTE_NODE)) return (Attr[]) nodes;

	    Attr[] attres = new Attr[nodes.length];
	    for (int i = 0; i < nodes.length; i++) {
		attres[i] = toAttr(doc, nodes[i]);
	    }
	    return attres;
	}
	// Collection
	else if (o instanceof Collection) {
	    Collection coll = (Collection) o;
	    Iterator<Entry<Key, Object>> it = coll.entryIterator();
	    Entry<Key, Object> e;
	    List<Attr> attres = new ArrayList<Attr>();
	    Attr attr;
	    Collection.Key k;
	    while (it.hasNext()) {
		e = it.next();
		k = e.getKey();
		attr = doc.createAttribute(Decision.isNumber(k.getString()) ? "attribute-" + k.getString() : k.getString());
		attr.setValue(Caster.toString(e.getValue()));
		attres.add(attr);
	    }
	    return attres.toArray(new Attr[attres.size()]);
	}
	// Node Map and List
	Node[] nodes = _toNodeArray(doc, o);
	if (nodes != null) return toAttrArray(doc, nodes);
	// Single Text Node
	try {
	    return new Attr[] { toAttr(doc, o) };
	}
	catch (ExpressionException e) {
	    throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Attributes Array");
	}
    }
474781
public DocCommentDuo getHtmlCommentDuo(Element e) {
        FileObject fo = null;
        PackageElement pe = null;
        switch (e.getKind()) {
            case OTHER:
                fo = configuration.getOverviewPath();
                pe = configuration.workArounds.getUnnamedPackage();
                break;
            case PACKAGE:
                fo = configuration.workArounds.getJavaFileObject((PackageElement)e);
                pe = (PackageElement)e;
                break;
            default:
                return null;
        }
        if (fo == null) {
            return null;
        }

        DocCommentTree dcTree = trees.getDocCommentTree(fo);
        if (dcTree == null) {
            return null;
        }
        DocTreePath treePath = trees.getDocTreePath(fo, pe);
        return new DocCommentDuo(treePath.getTreePath(), dcTree);
    }
239481
public static Document toDocument(String xml) {
		try {

			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
					.newInstance();
			documentBuilderFactory.setNamespaceAware(true);
			documentBuilderFactory.setIgnoringElementContentWhitespace(true);
			documentBuilderFactory.isIgnoringComments();
			documentBuilderFactory.setCoalescing(true);

			DocumentBuilder documentBuilder = documentBuilderFactory
					.newDocumentBuilder();

			InputSource inputSource = new InputSource();

			inputSource.setCharacterStream(new StringReader(xml));

			Document document = documentBuilder.parse(inputSource);

			removeUnusedNamespaces(document);

			return document;

		} catch (ParserConfigurationException e) {
			throw new RuntimeException(e);
		} catch (SAXException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
18679
@Override
	public Document readXML() {
		final String xmlString = readString();
		Document doc = null;
		try {
			doc = XMLUtils.stringToDoc(xmlString);
		} catch (IOException ioex) {
			log.error("IOException converting xml to dom", ioex);
		}
		storeReference(doc);
		return doc;
	}
739406
public static Document toDocument(String xml) throws SAXException {
		try {
			return toDocument(new StringReader(xml));
		} catch (IOException e) {
			//reading from string
			throw new RuntimeException(e);
		}
	}
276258
static void serialize(final Document doc, final OutputStream os, final String encoding)
        throws TransformerFactoryConfigurationError, TransformerException, IOException
  {
    if (doc == null)
      throw new IllegalArgumentException("No document provided.");

    if (os == null)
      throw new IllegalArgumentException("No output stream provided");

    if (encoding == null || encoding.isEmpty())
      throw new IllegalArgumentException("No encoding provided.");

    final TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", Integer.valueOf(2));
    final Transformer t = transformerFactory.newTransformer();

    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty(OutputKeys.ENCODING, encoding);

    final OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
    t.transform(new DOMSource(doc), new StreamResult(osw));
    osw.flush();
  }
1137032
public static short toShortValue(Object o) throws PageException {
	if (o instanceof Short) return ((Short) o).shortValue();
	else if (o instanceof CharSequence) return (short) toDoubleValue(o.toString());
	else if (o instanceof Character) return (short) (((Character) o).charValue());
	else if (o instanceof Boolean) return (short) ((((Boolean) o).booleanValue()) ? 1 : 0);
	else if (o instanceof Number) return (((Number) o).shortValue());
	else if (o instanceof Castable) return (short) ((Castable) o).castToDoubleValue();
	else if (o instanceof ObjectWrap) return toShortValue(((ObjectWrap) o).getEmbededObject());

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