Skip to main content
summaryrefslogtreecommitdiffstats
blob: 97ea2bdd94573496ddad4b91951ba025cca7b1e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.ws.util;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

/**
 * @author gilberta
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class DomElementHelper {


  public static org.w3c.dom.Element createDomElementFromXMLString(java.lang.String xmlString )
  {
    java.io.StringReader stringReader = new java.io.StringReader(xmlString); 
    org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(stringReader); 
    try
    {
      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
      Document document = docBuilder.parse(inputSource);
      return document.getDocumentElement();
    }
    catch (Throwable t)
    {
      return null;
    }
  }

  public static java.lang.String domWriter(org.w3c.dom.Node node,java.lang.StringBuffer buffer)
  {
    if ( node == null ) {
      return "";
    }
	int type = node.getNodeType();
	switch ( type ) {
	  case org.w3c.dom.Node.DOCUMENT_NODE: {
	  buffer.append(JspUtils.markup("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") + "<br>");
	  domWriter(((org.w3c.dom.Document)node).getDocumentElement(),buffer);
	  break;
	}
	  case org.w3c.dom.Node.ELEMENT_NODE: {
	  buffer.append(JspUtils.markup("<" + node.getNodeName()));
	  org.w3c.dom.Attr attrs[] = sortAttributes(node.getAttributes());
	  for ( int i = 0; i < attrs.length; i++ ) {
	    org.w3c.dom.Attr attr = attrs[i];
	    buffer.append(" " + attr.getNodeName() + "=\"" + JspUtils.markup(attr.getNodeValue()) + "\"");
	  }
	    buffer.append(JspUtils.markup(">"));
	    org.w3c.dom.NodeList children = node.getChildNodes();
	    if ( children != null ) {
	      int len = children.getLength();
		  for ( int i = 0; i < len; i++ ) {
		    if(((org.w3c.dom.Node)children.item(i)).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE)
			  buffer.append("<br>");
			domWriter(children.item(i),buffer);
		  }
		}
		buffer.append(JspUtils.markup("</" + node.getNodeName() + ">"));
		break;
      }
	  case org.w3c.dom.Node.ENTITY_REFERENCE_NODE: {
	    org.w3c.dom.NodeList children = node.getChildNodes();
	    if ( children != null ) {
		  int len = children.getLength();
		  for ( int i = 0; i < len; i++ )
	      {
		    buffer.append(children.item(i));
		  }
		}
		break;
	  }
	  case org.w3c.dom.Node.CDATA_SECTION_NODE: {
	    buffer.append(JspUtils.markup(node.getNodeValue()));
	    break;
      }
	  case org.w3c.dom.Node.TEXT_NODE:{
	    buffer.append(JspUtils.markup(node.getNodeValue()));
	    break;
	  }
	  case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:{
		buffer.append(JspUtils.markup("<?"));
		buffer.append(node.getNodeName());
		String data = node.getNodeValue();
		if ( data != null && data.length() > 0 ){
	      buffer.append(" ");
		  buffer.append(data);
		}
		buffer.append(JspUtils.markup("?>"));
		  break;
		}
	  }
	  return buffer.toString();
    }
 
	public static org.w3c.dom.Attr[] sortAttributes(org.w3c.dom.NamedNodeMap attrs)
	{
	  int len = (attrs != null) ? attrs.getLength() : 0;
	  org.w3c.dom.Attr array[] = new org.w3c.dom.Attr[len];
	  for ( int i = 0; i < len; i++ ){
	   array[i] = (org.w3c.dom.Attr)attrs.item(i);
	  }
	  for ( int i = 0; i < len - 1; i++ ) {
	    String name  = array[i].getNodeName();
	    int index = i;
	    for ( int j = i + 1; j < len; j++ ) {
	      String curName = array[j].getNodeName();
		  if ( curName.compareTo(name) < 0 ) {
		    name  = curName;
			index = j;
		  }
		}
		if ( index != i ) {
		  org.w3c.dom.Attr temp = array[i];
		  array[i] = array[index];
		  array[index] = temp;
		}
	  }
	  return (array);
    }
 

}

Back to the top