Source Code Cross Referenced for XfmDom.java in  » ESB » cbesb-1.2 » com » bostechcorp » cbesb » common » util » xfm » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » cbesb 1.2 » com.bostechcorp.cbesb.common.util.xfm 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.bostechcorp.cbesb.common.util.xfm;
002:
003:        import java.io.StringReader;
004:
005:        import javax.xml.namespace.QName;
006:        import javax.xml.transform.TransformerException;
007:
008:        import org.w3c.dom.Attr;
009:        import org.w3c.dom.Document;
010:        import org.w3c.dom.Element;
011:        import org.w3c.dom.NamedNodeMap;
012:        import org.w3c.dom.Node;
013:        import org.w3c.dom.NodeList;
014:        import org.w3c.dom.Text;
015:
016:        import com.bostechcorp.cbesb.common.util.Dom;
017:        import com.sun.org.apache.xerces.internal.dom.AttrImpl;
018:        import com.sun.org.apache.xpath.internal.NodeSet;
019:
020:        public class XfmDom {
021:
022:            public static NodeList findChildren(Node parent,
023:                    XmlFormatModelNode format) {
024:                if (format.getNameIndex() > -1) {
025:                    int nameIndex = format.getNameIndex();
026:                    int beginIndex = 0;
027:                    String originalName = format.getOriginalName();
028:                    XmlFormatModelNode parentFormat = format.getParent();
029:                    NodeList nodeList = Dom.getChildren(parent);
030:                    for (int i = 0; i < nameIndex; i++) {
031:                        XmlFormatModelNode formatNode = parentFormat
032:                                .getChild(originalName + "{" + i + "}");
033:                        beginIndex = getNextBeginIndexForNameIndex(formatNode,
034:                                nodeList, beginIndex);
035:                    }
036:                    int endIndex = getNextBeginIndexForNameIndex(format,
037:                            nodeList, beginIndex);
038:                    return findChildren(format, nodeList, beginIndex, endIndex);
039:                } else
040:                    return findChildren(parent, format.getName());
041:            }
042:
043:            public static Node findChild(Node parent, String nodeName) {
044:                parent.normalize();
045:                Node result = null;
046:                if (parent != null && nodeName != null) {
047:                    NodeList nl = parent.getChildNodes();
048:                    for (int i = 0; i < nl.getLength() && result == null; i++) {
049:                        if (nl.item(i).getNodeType() == Node.ELEMENT_NODE
050:                                && (nodeName.equals(nl.item(i).getNodeName()) || nl
051:                                        .item(i).getNodeName().endsWith(
052:                                                ":" + nodeName))) {
053:                            result = nl.item(i);
054:                        }
055:                    }
056:                }
057:                return result;
058:            }
059:
060:            public static NodeList findChildren(Node parent, String nodeName) {
061:                parent.normalize();
062:                NodeSet nodeList = new NodeSet();
063:                if (parent != null && nodeName != null) {
064:                    NodeList nl = parent.getChildNodes();
065:                    for (int i = 0; i < nl.getLength(); i++) {
066:                        if (nodeName.equals(nl.item(i).getNodeName())
067:                                || nl.item(i).getNodeName().endsWith(
068:                                        ":" + nodeName)) {
069:                            nodeList.addElement(nl.item(i));
070:                        }
071:                    }
072:
073:                }
074:                return nodeList;
075:            }
076:
077:            public static Node findChild(Node parent, XmlFormatModelNode format) {
078:                if (format.getNameIndex() > -1) {
079:                    int nameIndex = format.getNameIndex();
080:                    int beginIndex = 0;
081:                    String originalName = format.getOriginalName();
082:                    XmlFormatModelNode parentFormat = format.getParent();
083:                    NodeList nodeList = Dom.getChildren(parent);
084:                    for (int i = 0; i < nameIndex; i++) {
085:                        XmlFormatModelNode formatNode = parentFormat
086:                                .getChild(originalName + "{" + i + "}");
087:                        beginIndex = getNextBeginIndexForNameIndex(formatNode,
088:                                nodeList, beginIndex);
089:                    }
090:                    return findChild(format, nodeList, beginIndex);
091:                } else
092:                    return findChild(parent, format.getOriginalName());
093:
094:            }
095:
096:            /**
097:             * It is used by preprocess anonymous node, it will handle prefix
098:             * @param parent
099:             * @param format
100:             * @return
101:             */
102:            public static Element createElementWithPrefix(Node parent,
103:                    XmlFormatModelNode format) {
104:                //  if(format.isElementQualified())
105:                return parent.getOwnerDocument().createElementNS(
106:                        format.getNamespaceURI(),
107:                        Dom.getPrefixForNamespaceURIRecursive(parent, format
108:                                .getNamespaceURI())
109:                                + format.getOriginalName());
110:                //  else
111:                //	   return parent.getOwnerDocument().createElementNS(null,format.getOriginalName());
112:
113:            }
114:
115:            /**
116:             * It is used by generate target document element, it don't handle prefix
117:             * @param doc
118:             * @param format
119:             * @return
120:             */
121:            public static Element createElement(Document doc,
122:                    XmlFormatModelNode format) {
123:                //		   if(format.isElementQualified())
124:                return doc.createElementNS(format.getNamespaceURI(), format
125:                        .getOriginalName());
126:                //		   else
127:                //			   return doc.createElementNS(null,format.getOriginalName());
128:
129:            }
130:
131:            public static void setAttribute(Element element, String attrName,
132:                    String attrValue, XmlFormatModelNode namedNode) {
133:                //		   if(namedNode.isAttributeQualified()){
134:                //			   element.setAttributeNS(namedNode.getNamespaceURI(), attrName, attrValue);
135:
136:                //		   }
137:                //		   else
138:                //			   element.setAttributeNS(null,attrName, attrValue);
139:
140:                QName attr = null;
141:                for (QName attr0 : namedNode.getAttributeList()) {
142:                    if (attr0.getLocalPart().equals(attrName)) {
143:                        attr = attr0;
144:                        break;
145:                    }
146:                }
147:
148:                if (attr == null)
149:                    element.setAttributeNS(null, attrName, attrValue);
150:                else
151:                    element.setAttributeNS(attr.getNamespaceURI(), attrName,
152:                            attrValue);
153:            }
154:
155:            public static String getAttributeValue(Node node, String attrName,
156:                    XmlFormatModelNode namedNode) {
157:
158:                //	String str="";
159:                //   NamedNodeMap attributes = node.getAttributes();
160:                //	Node attrNode = null;
161:
162:                //		   if(namedNode.isAttributeQualified()){
163:                //				attrNode = attributes.getNamedItemNS(namedNode.getNamespaceURI(), attrName);
164:
165:                //			   
166:                //		   }
167:                //		   else
168:                //				attrNode = attributes.getNamedItemNS(null,attrName);
169:                //		   
170:
171:                QName attr = null;
172:                for (QName attr0 : namedNode.getAttributeList()) {
173:                    if (attr0.getLocalPart().equals(attrName)) {
174:                        attr = attr0;
175:                        break;
176:                    }
177:                }
178:                String namespace = null;
179:                if (attr != null)
180:                    namespace = attr.getNamespaceURI();
181:
182:                return getAttributeValue(node, namespace, attrName);
183:
184:                //	System.out.println("attr name="+ attrName+" value="+str);
185:
186:            }
187:
188:            /**
189:             * Now it is only for UI: Map tester
190:             * @param node
191:             * @param namespaceURI
192:             * @param attrName
193:             * @return
194:             */
195:            public static String getAttributeValue(Node node,
196:                    String namespaceURI, String attrName) {
197:                Node attrNode = null;
198:                // if (namespaceURI==null)
199:                //	   attrNode = node.getAttributes().getNamedItem(attrName);
200:
201:                //  else{
202:                attrNode = getAttributeNodeNS(node, namespaceURI, attrName);
203:
204:                //   }
205:
206:                if (attrNode != null) {
207:                    return ((Attr) attrNode).getNodeValue();
208:                } else
209:                    return "";
210:            }
211:
212:            public static Node getAttributeNodeNS(Node node,
213:                    String namespaceURI, String attrName) {
214:                NamedNodeMap attributes = node.getAttributes();
215:                //  System.out.println("length is "+attributes.getLength());
216:                //  System.out.println("attrName is "+attrName);
217:
218:                for (int i = 0; i < attributes.getLength(); i++) {
219:                    Node attrNode = attributes.item(i);
220:                    //  if(attrNode instanceof AttrImpl){
221:                    //	  AttrImpl attr=(AttrImpl)attrNode;
222:                    String name = attrNode.getNodeName();
223:                    //	  System.out.println("name is "+name+" ;attrName is "+attrName);
224:                    if (name.endsWith(":" + attrName) || name.equals(attrName)) {
225:                        return attrNode;
226:
227:                    }
228:
229:                    //  }
230:
231:                }
232:                return null;
233:            }
234:
235:            //	   public static String  getAttributeValue(Node node,String attrName,XmlFormatModelNode namedNode) {
236:            //		   			
237:            //		   QName attr=null;
238:            //		   for (QName attr0:namedNode.getAttributeList() ) {
239:            //			   if (attr0.getLocalPart().equals(attrName)) {
240:            //				   attr=attr0;
241:            //				   break;
242:            //			   }	   
243:            //		   }
244:            //		   String namespaceURI=null;
245:            //		   if (attr!=null)
246:            //			   namespaceURI = attr.getNamespaceURI();
247:            //		   
248:            //			return  getAttributeValue(node,namespaceURI, attrName);
249:            //			
250:            //		   
251:            //		
252:            //	   }
253:            //	   public static Node getAttributeNodeNS( Node node ,String namespaceURI,String attrName){
254:            //		   NamedNodeMap attributes = node.getAttributes();
255:            //		   for(int i=0;i<attributes.getLength();i++){
256:            //			  Node attrNode= attributes.item(i);
257:            //			  if(attrNode instanceof AttrImpl){
258:            //				  AttrImpl attr=(AttrImpl)attrNode;
259:            //				  String name=attr.getName();
260:            //				  if(attrNode.getNamespaceURI()!=null){
261:            //					  if(attrNode.getNamespaceURI().equals(namespaceURI) && name.equals(attrName))
262:            //						  return attrNode;
263:            //				  }
264:            //				  else{
265:            //					  
266:            //					  if(name.endsWith(":"+attrName)||name.equals(attrName)){
267:            //						   String prefixNamespaceURI = Dom.getNamespaceURIForPrefixRecursive(node,Dom.getPrefix(name));
268:            //						   if((prefixNamespaceURI==null||"".equals(prefixNamespaceURI)) &&	(namespaceURI==null||"".equals(namespaceURI)) ){
269:            //								   return attrNode;
270:            //						
271:            //						   }
272:            //						   else  if(prefixNamespaceURI.equals(namespaceURI) ) 
273:            //						    		return attrNode;
274:            //						  }
275:            //				  }
276:            //				//  }
277:            //			  }
278:            //			 
279:            //		   }
280:            //		   return null;
281:            //	   }
282:            //	   public static String getAttributeValue( Node node ,String namespaceURI,String attrName){
283:            //		   Node attrNode=null;
284:            //		   if (namespaceURI==null)
285:            //			   attrNode = node.getAttributes().getNamedItemNS(null, attrName);
286:            //		
287:            //		   else
288:            //			   attrNode=getAttributeNodeNS(node,namespaceURI,attrName);
289:            //		  
290:            //		  if(attrNode!=null) {
291:            //				return ((Attr)attrNode).getNodeValue();
292:            //			}
293:            //			else return "";
294:            //	   }
295:            //	   
296:            private static int getNextBeginIndexForNameIndex(
297:                    XmlFormatModelNode formatNode, NodeList nodeList,
298:                    int beginIndex) {
299:                String nodeName = formatNode.getOriginalName();
300:
301:                //			if(!(nodeList.item(beginIndex).getNodeName().equals(nodeName)
302:                //					||nodeList.item(beginIndex).getNodeName().endsWith(":"+nodeName))){
303:                //				if(formatNode.getMinOccurs()>0) return beginIndex;
304:                //				else return beginIndex;
305:                //			}
306:                //			if(!formatNode.isRepeatable()) return beginIndex+1;
307:                boolean bExist = false;
308:                int i = beginIndex;
309:                for (i = beginIndex; i < nodeList.getLength(); i++) {
310:                    if (!bExist) {
311:                        if (nodeList.item(i).getNodeName().equals(nodeName)
312:                                || nodeList.item(beginIndex).getNodeName()
313:                                        .endsWith(":" + nodeName)) {
314:                            bExist = true;
315:                            if (!formatNode.isRepeatable())
316:                                return i + 1;
317:
318:                        }
319:                    } else {
320:                        if (!(nodeList.item(i).getNodeName().equals(nodeName) || nodeList
321:                                .item(beginIndex).getNodeName().endsWith(
322:                                        ":" + nodeName))) {
323:                            return i;
324:                        }
325:                    }
326:                }
327:
328:                if (bExist)
329:                    return i;
330:                else
331:                    return beginIndex;
332:            }
333:
334:            private static Node findChild(XmlFormatModelNode formatNode,
335:                    NodeList nodeList, int beginIndex) {
336:                String nodeName = formatNode.getOriginalName();
337:
338:                for (int i = beginIndex; i < nodeList.getLength(); i++) {
339:                    if (nodeList.item(i).getNodeName().equals(nodeName)
340:                            || nodeList.item(beginIndex).getNodeName()
341:                                    .endsWith(":" + nodeName)) {
342:                        return nodeList.item(i);
343:                    }
344:                }
345:
346:                return null;
347:            }
348:
349:            private static NodeList findChildren(XmlFormatModelNode formatNode,
350:                    NodeList nodeList, int beginIndex, int endIndex) {
351:                String nodeName = formatNode.getOriginalName();
352:                NodeSet result = new NodeSet();
353:                for (int i = beginIndex; i < endIndex; i++) {
354:                    if (nodeList.item(i).getNodeName().equals(nodeName)
355:                            || nodeList.item(beginIndex).getNodeName()
356:                                    .endsWith(":" + nodeName)) {
357:                        result.addNode(nodeList.item(i));
358:                    }
359:                }
360:
361:                return result;
362:            }
363:
364:            /*
365:             * Get the text content of a node without using
366:             * DOM level 3 getTextContent() method
367:             * since Axiom DOM implementation doesn't support it
368:             */
369:            public static String getNodeTextContent(Node node,
370:                    XmlFormatModelNode format) {
371:                if (node == null)
372:                    return "";
373:                if (format.getType() == XmlFormatModelNode.TYPE_NORMAL) {
374:                    StringBuffer sb = new StringBuffer();
375:                    Node child = node.getFirstChild();
376:                    while (child != null) {
377:                        if (child.getNodeType() == Node.TEXT_NODE
378:                                || child.getNodeType() == Node.CDATA_SECTION_NODE) {
379:                            sb.append(child.getNodeValue());
380:                        }
381:                        child = child.getNextSibling();
382:                    }
383:                    return sb.toString();
384:                } else {
385:                    try {
386:                        return createStringFromChildren(node);
387:                    } catch (TransformerException e) {
388:                        e.printStackTrace();
389:                        return Dom.getTextContent(node);
390:                    }
391:                }
392:            }
393:
394:            public static String createStringFromChildren(Node node)
395:                    throws TransformerException {
396:                StringBuffer sb = new StringBuffer();
397:                Node child = node.getFirstChild();
398:                while (child != null) {
399:                    sb.append(Dom.createStringFromDOMNode(child));
400:                    child = child.getNextSibling();
401:                }
402:                return sb.toString();
403:            }
404:
405:            /*
406:             * Set the text content of a node without using
407:             * DOM Level 3 setTextContent() method
408:             * since Axiom DOM implementation doesn't support it
409:             */
410:            private static void setNodeTextContent(Node node, String value) {
411:                Text text = node.getOwnerDocument().createTextNode(value);
412:                Node child = node.getFirstChild();
413:                while (child != null) {
414:                    node.removeChild(child);
415:                    child = child.getNextSibling();
416:                }
417:                node.appendChild(text);
418:            }
419:
420:            public static void setNodeTextContent(Node node,
421:                    XmlFormatModelNode format, String value) {
422:
423:                if (format.getType() == XmlFormatModelNode.TYPE_NORMAL)
424:                    setNodeTextContent(node, value);
425:                else if (format.getType() == XmlFormatModelNode.TYPE_ANY) {
426:                    try {
427:                        ParseAnyType(node, value);
428:
429:                    } catch (Exception e) {
430:
431:                        e.printStackTrace();
432:                    }
433:                } else if (format.getType() == XmlFormatModelNode.TYPE_ANYTYPE) {
434:                    try {
435:                        ParseAnyType(node, value);
436:
437:                    } catch (Exception e) {
438:                        setNodeTextContent(node, value);
439:                    }
440:                }
441:            }
442:
443:            private static void ParseAnyType(Node node, String value)
444:                    throws Exception {
445:                String newValue = "<root>" + value + "</root>";
446:                Document tdoc = Dom
447:                        .getDomTree(new StringReader(newValue), null);
448:                Node newNode = node.getOwnerDocument().adoptNode(
449:                        tdoc.getDocumentElement());
450:                Node child = newNode.getFirstChild();
451:                for (; child != null; child = newNode.getFirstChild()) {
452:                    node.appendChild(child);
453:                }
454:
455:            }
456:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.