Source Code Cross Referenced for XMLJdomUtil.java in  » Portal » Open-Portal » com » sun » portal » fabric » util » 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 » Portal » Open Portal » com.sun.portal.fabric.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        package com.sun.portal.fabric.util;
0002:
0003:        import java.io.File;
0004:        import java.io.FileOutputStream;
0005:        import java.io.FileNotFoundException;
0006:        import java.io.PrintWriter;
0007:        import java.io.IOException;
0008:        import java.util.List;
0009:        import java.util.ArrayList;
0010:        import java.util.Map;
0011:        import java.util.HashMap;
0012:        import java.util.Enumeration;
0013:        import java.util.Iterator;
0014:        import java.util.Properties;
0015:        import java.util.logging.Logger;
0016:        import java.util.logging.Level;
0017:        import java.net.MalformedURLException;
0018:        import java.net.URL;
0019:
0020:        import javax.xml.parsers.DocumentBuilder;
0021:        import javax.xml.parsers.DocumentBuilderFactory;
0022:        import javax.xml.parsers.ParserConfigurationException;
0023:
0024:        import org.jdom.Attribute;
0025:        import org.jdom.Document;
0026:        import org.jdom.Element;
0027:        import org.jdom.Namespace;
0028:        import org.jdom.JDOMException;
0029:        import org.jdom.output.XMLOutputter;
0030:        import org.jdom.input.SAXBuilder;
0031:        import org.jdom.input.SAXHandler;
0032:        import org.jdom.output.Format;
0033:
0034:        import com.sun.portal.fabric.util.XMLJdomFileCreationException;
0035:        import com.sun.portal.util.DTDResolver;
0036:        import com.sun.portal.util.Platform;
0037:
0038:        public class XMLJdomUtil {
0039:
0040:            //??private static final String TAGGED = "Tagged";
0041:
0042:            //??private Logger logger = null;
0043:            private Document doc = null;
0044:            private Map hash = null;
0045:
0046:            // Constructor for Writer
0047:            public XMLJdomUtil() {
0048:            }
0049:
0050:            // Constructor for Reader
0051:            /*??public XMLJdomUtil(String sFilePath)
0052:                       throws XMLJdomFileCreationException {
0053:
0054:                    readXMLFile(sFilePath);
0055:                }??*/
0056:
0057:            private static boolean isWinOS() {
0058:                return Platform.name.equals("windows");
0059:            }
0060:
0061:            /**
0062:             * Return XML JDOM Document.
0063:             */
0064:            public Document getXMLJdomDocument() {
0065:
0066:                return doc;
0067:            }
0068:
0069:            /**
0070:             * Return XML JDOM Document as String
0071:             */
0072:            public String getXMLJdomDocumentAsString() {
0073:
0074:                String sDocStr = null;
0075:                if (doc != null) {
0076:
0077:                    XMLOutputter xmlOutputter = new XMLOutputter();
0078:                    sDocStr = xmlOutputter.outputString(doc);
0079:                }
0080:
0081:                return sDocStr;
0082:            }
0083:
0084:            /**
0085:             * Return a node of the xml Document (org.jdom.Document) as
0086:             * org.jdom.Element.
0087:             */
0088:            public Element getChildAsElement(final String sChildPath)
0089:                    throws XMLJdomFileCreationException {
0090:
0091:                Element elm = null;
0092:
0093:                if ((sChildPath == null) || (sChildPath.trim().length() == 0)) {
0094:
0095:                    String sMsg = "sChildPath == Null/Empty";
0096:                    throw new XMLJdomFileCreationException(sMsg);
0097:                } else {
0098:
0099:                    if (hash != null) {
0100:
0101:                        // while in read Mode
0102:                        elm = (Element) hash.get(sChildPath);
0103:                        if (elm != null) {
0104:
0105:                            return elm;
0106:                        }
0107:                    }
0108:
0109:                    String[] sChildPathL = sChildPath.split("@");
0110:                    if (sChildPathL.length > 0) {
0111:
0112:                        // Verify the tag of root element
0113:                        elm = (doc != null) ? doc.getRootElement() : null;
0114:                        if (elm == null) {
0115:
0116:                            return null;
0117:                        }
0118:
0119:                        String sRootTagName = elm.getName();
0120:                        if ((sRootTagName != null)
0121:                                && !sRootTagName.equals(sChildPathL[0])) {
0122:
0123:                            //??String sMsg = "No child found for '" +
0124:                            //??              sChildPath + "'";
0125:                            //??throw new XMLJdomFileCreationException(sMsg);
0126:                            return null;
0127:                        }
0128:
0129:                        for (int i = 1; i < sChildPathL.length; i++) {
0130:
0131:                            String sField = sChildPathL[i].trim();
0132:                            elm = elm.getChild(sField);
0133:
0134:                            if (elm == null) {
0135:
0136:                                //??String sMsg = "No child found for '" +
0137:                                //??              sChildPath + "'";
0138:                                //??throw new XMLJdomFileCreationException(sMsg);
0139:                                return null;
0140:                            }
0141:                        }
0142:                    }
0143:                }
0144:
0145:                return elm;
0146:            }
0147:
0148:            /**
0149:             * Get the attributes of a node (org.jdom.Element) as
0150:             * java.util.Properties.
0151:             */
0152:            public Properties getChildAsProperties(final String sChildPath)
0153:                    throws XMLJdomFileCreationException {
0154:
0155:                Properties sProperties = null;
0156:
0157:                Element elm = getChildAsElement(sChildPath);
0158:                if (elm != null) {
0159:
0160:                    sProperties = convertElementToProperties(elm);
0161:                }
0162:
0163:                return sProperties;
0164:            }
0165:
0166:            /**
0167:             * Get the list of the children of a node (org.jdom.Element) in the
0168:             * xml document (org.jdom.Document) as list of the Element 
0169:             * org.jdom.Element.
0170:             */
0171:            public List getChildrenAsElementsList(final String sParentPath,
0172:                    final String sChildTagName)
0173:                    throws XMLJdomFileCreationException {
0174:
0175:                List sNodeList = null;
0176:
0177:                Element elm = getChildAsElement(sParentPath);
0178:                if (elm != null) {
0179:
0180:                    sNodeList = ((sChildTagName != null) && (sChildTagName
0181:                            .length() > 0)) ? elm.getChildren(sChildTagName)
0182:                            : elm.getChildren();
0183:                }
0184:
0185:                return sNodeList;
0186:            }
0187:
0188:            /**
0189:             * Get the list of children (org.jdom.Element) of a node
0190:             * (org.jdom.Element) as list of java.util.Properties from a given
0191:             * xml document (org.jdom.Document). This will return only the
0192:             * 'attributes' part of the org.jdom.Element.
0193:             */
0194:            public List getChildrenAsPropertiesList(final String sParentPath,
0195:                    final String sChildTagName)
0196:                    throws XMLJdomFileCreationException {
0197:
0198:                List sPropList = null;
0199:                List sElmList = getChildrenAsElementsList(sParentPath,
0200:                        sChildTagName);
0201:                int iSize = (sElmList != null) ? sElmList.size() : 0;
0202:                if (sElmList != null) {
0203:
0204:                    sPropList = new ArrayList();
0205:                    for (int i = 0; i < iSize; i++) {
0206:
0207:                        Element elm = (Element) sElmList.get(i);
0208:                        sPropList.add(convertElementToProperties(elm));
0209:                    }
0210:                }
0211:
0212:                return sPropList;
0213:            }
0214:
0215:            /**
0216:             * Get the list of children (org.jdom.Element) of a node
0217:             * (org.jdom.Element) as list of java.util.String from a given
0218:             * xml document (org.jdom.Document). This will return only the
0219:             * 'text' type data of the org.jdom.Element.
0220:             */
0221:            public List getChildrenAsStringList(final String sParentPath,
0222:                    final String sChildTagName)
0223:                    throws XMLJdomFileCreationException {
0224:
0225:                List sTextList = null;
0226:                List sElmList = getChildrenAsElementsList(sParentPath,
0227:                        sChildTagName);
0228:                int iSize = (sElmList != null) ? sElmList.size() : 0;
0229:                if (sElmList != null) {
0230:
0231:                    sTextList = new ArrayList();
0232:                    for (int i = 0; i < iSize; i++) {
0233:
0234:                        Element elm = (Element) sElmList.get(i);
0235:                        sTextList.add(convertElementToString(elm));
0236:                    }
0237:                }
0238:
0239:                return sTextList;
0240:            }
0241:
0242:            /**
0243:             * Get value of a particular attribute of a child (org.jdom.Element)
0244:             * of the given xml document (org.jdom.Document).
0245:             */
0246:            public String getAttribute(final String sChildPath,
0247:                    final String sAttribName)
0248:                    throws XMLJdomFileCreationException {
0249:
0250:                String str = null;
0251:
0252:                if ((sAttribName == null) || (sAttribName.length() == 0)) {
0253:
0254:                    String sMsg = "sAttribName == Null/Empty";
0255:                    throw new XMLJdomFileCreationException(sMsg);
0256:                } else {
0257:
0258:                    Element elm = getChildAsElement(sChildPath);
0259:                    if (elm != null) {
0260:
0261:                        str = elm.getAttributeValue(sAttribName);
0262:                    }
0263:                }
0264:
0265:                return str;
0266:            }
0267:
0268:            /**
0269:             * Convert the attributes of a child (org.jdom.Element) to
0270:             * java.util.Properties.
0271:             */
0272:            private Properties convertElementToProperties(final Element elm)
0273:                    throws XMLJdomFileCreationException {
0274:
0275:                Properties sProperties = null;
0276:
0277:                if (elm == null) {
0278:
0279:                    String sMsg = "elm == Null";
0280:                    throw new XMLJdomFileCreationException(sMsg);
0281:                } else {
0282:
0283:                    sProperties = new Properties();
0284:                    List attrib = elm.getAttributes();
0285:                    int iSize = (attrib != null) ? attrib.size() : 0;
0286:                    for (int i = 0; i < iSize; i++) {
0287:
0288:                        Attribute attr = (Attribute) attrib.get(i);
0289:                        sProperties
0290:                                .setProperty(attr.getName(), attr.getValue());
0291:                    }
0292:                }
0293:
0294:                return sProperties;
0295:            }
0296:
0297:            /**
0298:             * Convert the 'text' type data of a node (org.jdom.Element) to a
0299:             * java.util.String.
0300:             */
0301:            private String convertElementToString(final Element elm)
0302:                    throws XMLJdomFileCreationException {
0303:
0304:                String str = null;
0305:
0306:                if (elm == null) {
0307:
0308:                    String sMsg = "elm == Null";
0309:                    throw new XMLJdomFileCreationException(sMsg);
0310:                } else {
0311:
0312:                    str = elm.getTextTrim();
0313:                }
0314:
0315:                return str;
0316:            }
0317:
0318:            /**
0319:             * Read an xml file into an xml document (org.jdom.Document).
0320:             * Also, Do NOT Validate.
0321:             */
0322:            public Document readXMLFileLessSchemaValidation(
0323:                    final String sXMLFile) throws XMLJdomFileCreationException {
0324:
0325:                return readXMLFile(sXMLFile, false);
0326:            }
0327:
0328:            /**
0329:             * Read an xml file into an xml document (org.jdom.Document).
0330:             * Also, Validate.
0331:             */
0332:            public Document readXMLFile(final String sXMLFile)
0333:                    throws XMLJdomFileCreationException {
0334:
0335:                return readXMLFile(sXMLFile, true);
0336:            }
0337:
0338:            private Document readXMLFile(final String sXMLFile,
0339:                    final boolean bValidate)
0340:                    throws XMLJdomFileCreationException {
0341:
0342:                try {
0343:
0344:                    SAXBuilder builder = new SAXBuilder();
0345:                    builder.setValidation(bValidate);
0346:                    if (bValidate) {
0347:                        builder
0348:                                .setFeature(
0349:                                        "http://apache.org/xml/features/validation/schema",
0350:                                        true);
0351:                    }
0352:                    doc = builder.build(sXMLFile);
0353:                    String message = "The Input XML File '" + sXMLFile
0354:                            + "' is valid.";
0355:                    //??logger.log(Level.INFO, message);
0356:                    //??hash = new HashMap();
0357:                    //??prepareHash(null, null);
0358:                } catch (NullPointerException e0) {
0359:
0360:                    String message = "Please provide the Sample Silent File";
0361:                    //??logger.log(Level.SEVERE, message, e0);
0362:                    throw new XMLJdomFileCreationException(e0);
0363:                } catch (IOException e1) {
0364:
0365:                    String message = e1.getMessage();
0366:                    //??logger.log(Level.SEVERE, message, e1);
0367:                    throw new XMLJdomFileCreationException(e1);
0368:                } catch (JDOMException e2) {
0369:
0370:                    // Indicates Xml Well Formedness or Validity Error
0371:                    String message = "The Input XML File is not well formed.";
0372:                    //??logger.log(Level.SEVERE, message, e2);
0373:                    throw new XMLJdomFileCreationException(e2);
0374:                }
0375:
0376:                return doc;
0377:            }
0378:
0379:            /**
0380:             * Write an xml document (org.jdom.Document) to a file.
0381:             */
0382:            public void writeXMLFile(final String sXMLFile)
0383:                    throws XMLJdomFileCreationException {
0384:
0385:                writeXMLFile(sXMLFile, true);
0386:            }
0387:
0388:            public void writeXMLFile(final String sXMLFile, final boolean bPrune)
0389:                    throws XMLJdomFileCreationException {
0390:
0391:                if (doc == null) {
0392:
0393:                    String sMessage = "doc == null";
0394:                    throw new XMLJdomFileCreationException(sMessage);
0395:                }
0396:
0397:                FileOutputStream fXMLout = null;
0398:                boolean bGood = false;
0399:                File fXMLFile = null;
0400:                File fXMLFileTemp = null;
0401:                try {
0402:
0403:                    fXMLFile = new File(sXMLFile);
0404:                    if (fXMLFile.isFile()) {
0405:
0406:                        fXMLFile.delete();
0407:                    } else if (fXMLFile.isDirectory()) {
0408:
0409:                        String sMsg = "'" + sXMLFile + "' is a directory.";
0410:                        throw new XMLJdomFileCreationException(sMsg);
0411:                    }
0412:
0413:                    File fDir = new File("/tmp");
0414:                    if (isWinOS())
0415:                        fXMLFileTemp = File.createTempFile("psconfig", ".tmp");
0416:                    else
0417:                        fXMLFileTemp = File.createTempFile("psconfig", ".tmp",
0418:                                fDir);
0419:                    fXMLFileTemp.deleteOnExit();
0420:
0421:                    XMLOutputter XMLToFile = new XMLOutputter();
0422:                    fXMLout = new FileOutputStream(fXMLFileTemp);
0423:                    /*??if (bPrune) {
0424:
0425:                        pruneXMLDocument();
0426:                    }??*/
0427:                    XMLToFile.output(doc, fXMLout);
0428:                    fXMLout.flush();
0429:                    bGood = true;
0430:                } catch (NullPointerException e0) {
0431:
0432:                    String message = "Please provide the XML Output file.";
0433:                    //??logger.log(Level.SEVERE, message, e0);
0434:                    throw new XMLJdomFileCreationException(e0);
0435:                } catch (IOException e1) {
0436:
0437:                    String message = e1.getMessage();
0438:                    //??logger.log(Level.SEVERE, message, e1);
0439:                    throw new XMLJdomFileCreationException(e1);
0440:                } finally {
0441:
0442:                    try {
0443:
0444:                        if (fXMLout == null) {
0445:
0446:                            fXMLout.close();
0447:                        }
0448:                    } catch (IOException e) {
0449:
0450:                        throw new XMLJdomFileCreationException(e);
0451:                    }
0452:                }
0453:
0454:                if (bGood) {
0455:
0456:                    try {
0457:                        indentXML(fXMLFileTemp, fXMLFile);
0458:                    } catch (IOException e0) {
0459:
0460:                        throw new XMLJdomFileCreationException(e0);
0461:                    } catch (JDOMException e1) {
0462:
0463:                        throw new XMLJdomFileCreationException(e1);
0464:                    } finally {
0465:
0466:                        fXMLFileTemp.delete();
0467:                    }
0468:                }
0469:            }
0470:
0471:            /**
0472:             * Modify an existing child (org.jdom.Element) of the xml document
0473:             * (org.jdom.Document). The modification can be of 3 types as follows:
0474:             *
0475:             * 1. Attach a new attributes list
0476:             * 2. Rename tag
0477:             * 3. Remove contents of the child
0478:             * 
0479:             * More then one type of modification can be performed simultaneously.
0480:             */
0481:            public void modifyChild(final String sChildPath,
0482:                    final String sNewTagName, final Properties sNewProps,
0483:                    final boolean bCleanContents)
0484:                    throws XMLJdomFileCreationException {
0485:
0486:                if (((sNewTagName == null) || (sNewTagName.trim().length() == 0))
0487:                        && (sNewProps == null) && !bCleanContents) {
0488:
0489:                    String sMsg = "sNewTagName == Null/Empty and sNewProps == Null and bCleanContents=false. At least one of them should be *not* Null/True.";
0490:                    throw new XMLJdomFileCreationException(sMsg);
0491:                } else {
0492:
0493:                    Element elm = getChildAsElement(sChildPath);
0494:                    if ((sNewTagName != null)
0495:                            && (sNewTagName.trim().length() > 0)) {
0496:
0497:                        elm.setName(sNewTagName);
0498:                    }
0499:
0500:                    if (bCleanContents) {
0501:
0502:                        elm.removeContent();
0503:                    }
0504:
0505:                    if (sNewProps != null) {
0506:
0507:                        // Remove old Attributes
0508:                        Iterator itr = elm.getAttributes().iterator();
0509:                        while (itr.hasNext()) {
0510:
0511:                            Attribute attrib = (Attribute) itr.next();
0512:                            itr.remove();
0513:                        }
0514:
0515:                        // Add new attrbutes and attach it to the element in
0516:                        // question.
0517:                        for (Enumeration enum1 = sNewProps.propertyNames(); enum1
0518:                                .hasMoreElements();) {
0519:
0520:                            String sKey = (String) enum1.nextElement();
0521:                            String sValue = (String) sNewProps
0522:                                    .getProperty(sKey);
0523:                            elm.setAttribute(sKey, sValue);
0524:                        }
0525:                    }
0526:                }
0527:            }
0528:
0529:            /**
0530:             * Convert a java.lang.Object to a child (org.jdom.Element), and add
0531:             * it to a node (org.jdom.Element) of an xml document
0532:             * (org.jdom.Document).
0533:             */
0534:            public int addChild(final String sParentPath,
0535:                    final String sChildTagName, final Object object)
0536:                    throws XMLJdomFileCreationException {
0537:
0538:                int iHashCode = -1;
0539:
0540:                if (object == null) {
0541:
0542:                    String sMsg = "object == Null";
0543:                    throw new XMLJdomFileCreationException(sMsg);
0544:                } else {
0545:
0546:                    Element child = convertObjectToElement(sChildTagName,
0547:                            object);
0548:                    iHashCode = child.hashCode();
0549:                    Element parent = null;
0550:                    if (sParentPath == null) {
0551:
0552:                        // Root Element
0553:                        if (doc != null) {
0554:
0555:                            String sMsg = "Root Element is already defined. There can be only one root element per document.";
0556:                            throw new XMLJdomFileCreationException(sMsg);
0557:                        } else {
0558:
0559:                            doc = new Document(child);
0560:                        }
0561:                    } else {
0562:
0563:                        parent = getChildAsElement(sParentPath);
0564:                        if (parent == null) {
0565:
0566:                            String sMsg = "No Element was found for tag path '"
0567:                                    + sParentPath + "'.";
0568:                            throw new XMLJdomFileCreationException(sMsg);
0569:                        }
0570:                        parent.addContent(child);
0571:                    }
0572:
0573:                    // Tagging
0574:                    /*??boolean bTag = false;
0575:                    List attrib = child.getAttributes();
0576:                    int iSize = (attrib != null) ? attrib.size() : 0;
0577:                    if (iSize > 0) {
0578:
0579:                        bTag = true;
0580:                        child.setAttribute(TAGGED, TAGGED);
0581:                    } else {
0582:
0583:                        String sText = child.getText();
0584:                        if ((sText != null) && (sText.length() > 0)) {
0585:
0586:                            bTag = true;
0587:                            child.setAttribute(TAGGED, TAGGED);
0588:                        }
0589:                    }
0590:
0591:                    if (bTag) {
0592:
0593:                        child.setAttribute(TAGGED, TAGGED);
0594:                        tagAncestors(child);
0595:                    }??*/
0596:                }
0597:
0598:                return iHashCode;
0599:            }
0600:
0601:            /**
0602:             * Convert a list of java.lang.Object to a list of children
0603:             * (org.jdom.Element), and add it to a node (org.jdom.Element)
0604:             * of an xml document (org.jdom.Document).
0605:             */
0606:            public int[] addChildren(final String sParentPath,
0607:                    final String sChildTagName, final List lObject)
0608:                    throws XMLJdomFileCreationException {
0609:
0610:                int iSize = (lObject != null) ? lObject.size() : 0;
0611:                if (iSize == 0) {
0612:
0613:                    String sMsg = "lObject == Null/Empty";
0614:                    throw new XMLJdomFileCreationException(sMsg);
0615:                }
0616:
0617:                if ((sChildTagName == null)
0618:                        || (sChildTagName.trim().length() == 0)) {
0619:
0620:                    String sMsg = "sChildTagName == Null/Empty";
0621:                    throw new XMLJdomFileCreationException(sMsg);
0622:                }
0623:
0624:                List lChildTagName = new ArrayList();
0625:                for (int i = 0; i < iSize; i++) {
0626:
0627:                    lChildTagName.add(sChildTagName);
0628:                }
0629:
0630:                return addChildren(sParentPath, lChildTagName, lObject);
0631:            }
0632:
0633:            /**
0634:             * Convert a list of java.lang.Object to a list of children
0635:             * (org.jdom.Element), and add it to a node (org.jdom.Element)
0636:             * of an xml document (org.jdom.Document).
0637:             */
0638:            public int[] addChildren(final String sParentPath,
0639:                    final List lChildTagName, final List lObject)
0640:                    throws XMLJdomFileCreationException {
0641:
0642:                int[] iHashCode = null;
0643:                if (lChildTagName == null) {
0644:
0645:                    String sMsg = "lChildTagName == Null";
0646:                    throw new XMLJdomFileCreationException(sMsg);
0647:                } else if (lObject == null) {
0648:
0649:                    String sMsg = "lObject == Null";
0650:                    throw new XMLJdomFileCreationException(sMsg);
0651:                } else {
0652:
0653:                    int iChildTagNameListSize = lChildTagName.size();
0654:                    int iObjectListSize = lObject.size();
0655:
0656:                    if (iChildTagNameListSize != iObjectListSize) {
0657:
0658:                        String sMsg = "Size mismatch for Lists lChildTagName & lObject";
0659:                        throw new XMLJdomFileCreationException(sMsg);
0660:                    } else if (iObjectListSize > 0) {
0661:
0662:                        Element parent = getChildAsElement(sParentPath);
0663:                        iHashCode = new int[iObjectListSize];
0664:                        for (int i = 0; i < iObjectListSize; i++) {
0665:
0666:                            String sChildTagName = (String) lChildTagName
0667:                                    .get(i);
0668:                            Object object = lObject.get(i);
0669:                            iHashCode[i] = addChild(sParentPath, sChildTagName,
0670:                                    object);
0671:                        }
0672:                    }
0673:                }
0674:
0675:                return iHashCode;
0676:            }
0677:
0678:            /**
0679:             * Set an Name space (xsi) to a child (org.jdom.Element).
0680:             */
0681:            public void setNamespace(final String sChildPath,
0682:                    final String sNamespaceName, final String sNamespaceURI,
0683:                    final String sAttribName, final String sAttribValue)
0684:                    throws XMLJdomFileCreationException {
0685:
0686:                if ((sNamespaceName == null)
0687:                        || (sNamespaceName.trim().length() == 0)) {
0688:
0689:                    String sMsg = "sNamespaceName == Null/Empty";
0690:                    throw new XMLJdomFileCreationException(sMsg);
0691:                } else if ((sNamespaceURI == null)
0692:                        || (sNamespaceURI.trim().length() == 0)) {
0693:
0694:                    String sMsg = "sNamespaceURI == Null/Empty";
0695:                    throw new XMLJdomFileCreationException(sMsg);
0696:                } else if ((sAttribName == null)
0697:                        || (sAttribName.trim().length() == 0)) {
0698:
0699:                    String sMsg = "sAttribName == Null/Empty";
0700:                    throw new XMLJdomFileCreationException(sMsg);
0701:                } else {
0702:
0703:                    Element elm = getChildAsElement(sChildPath);
0704:                    Namespace xsi = null;
0705:                    if (elm != null) {
0706:
0707:                        xsi = Namespace.getNamespace(sNamespaceName,
0708:                                sNamespaceURI);
0709:                        elm.addNamespaceDeclaration(xsi);
0710:
0711:                        String sValue = (sAttribValue != null) ? sAttribValue
0712:                                : "";
0713:                        elm.setAttribute(sAttribName, sValue, xsi);
0714:                    }
0715:                }
0716:            }
0717:
0718:            /**
0719:             * Set an Name space (xsi) to a child (org.jdom.Element).
0720:             */
0721:            /*??public Namespace setNamespace(final String sChildPath,
0722:                                          final String sNamespaceName,
0723:                                          final String sNamespaceURI)
0724:                        throws XMLJdomFileCreationException {
0725:
0726:                if ((sNamespaceName == null) || (sNamespaceName.trim().length() == 0)) {
0727:
0728:                   String sMsg = "sNamespaceName == Null/Empty";
0729:                   throw new XMLJdomFileCreationException(sMsg);
0730:                } else if ((sNamespaceURI == null) || (sNamespaceURI.trim().length() == 0)) {
0731:
0732:                   String sMsg = "sNamespaceURI == Null/Empty";
0733:                   throw new XMLJdomFileCreationException(sMsg);
0734:                } else {
0735:
0736:                   Element elm = getChildAsElement(sChildPath);
0737:                   Namespace xsi = null;
0738:                   if (elm != null) {
0739:
0740:                      xsi = Namespace.getNamespace(sNamespaceName, sNamespaceURI);
0741:                      elm.addNamespaceDeclaration(xsi);
0742:                   }
0743:
0744:                   return xsi;
0745:                }
0746:            }
0747:
0748:            public void setNamespaceAttribute(Namespace xsi,
0749:                                              final String sChildPath,
0750:                                              final String sAttribName,
0751:                                              final String sAttribValue)
0752:                        throws XMLJdomFileCreationException {
0753:
0754:                if ((sAttribName == null) || (sAttribName.trim().length() == 0)) {
0755:
0756:                   String sMsg = "sAttribName == Null/Empty";
0757:                   throw new XMLJdomFileCreationException(sMsg);
0758:                } else {
0759:
0760:                   Element elm = getChildAsElement(sChildPath);
0761:                   if (elm != null) {
0762:
0763:                      //??Namespace xsi = elm.getNamespace();
0764:                      if (xsi == null) {
0765:
0766:                         String sMsg = "No namespace associated with this element";
0767:                         throw new XMLJdomFileCreationException(sMsg);
0768:                      }
0769:
0770:                      String sValue = (sAttribValue != null) ? sAttribValue : "";
0771:                      elm.setAttribute(sAttribName, sValue, xsi);
0772:                   }
0773:                }
0774:            }??*/
0775:
0776:            /**
0777:             * Set an attribute (name, value) to a child (org.jdom.Element).
0778:             */
0779:            public void setAttribute(final String sChildPath,
0780:                    final String sAttribName, final String sAttribValue)
0781:                    throws XMLJdomFileCreationException {
0782:
0783:                if ((sAttribName == null) || (sAttribName.trim().length() == 0)) {
0784:
0785:                    String sMsg = "sAttribName == Null/Empty";
0786:                    throw new XMLJdomFileCreationException(sMsg);
0787:                } else {
0788:
0789:                    Element elm = getChildAsElement(sChildPath);
0790:                    if (elm != null) {
0791:
0792:                        String sValue = (sAttribValue != null) ? sAttribValue
0793:                                : "";
0794:                        elm.setAttribute(sAttribName, sValue);
0795:                    }
0796:                }
0797:            }
0798:
0799:            /**
0800:             * Convert java.lang.Object to org.jdom.Element.
0801:             */
0802:            private Element convertObjectToElement(final String sElmTagName,
0803:                    final Object object) throws XMLJdomFileCreationException {
0804:
0805:                Element elm = null;
0806:
0807:                if (object == null) {
0808:
0809:                    String sMsg = "object == Null";
0810:                    throw new XMLJdomFileCreationException(sMsg);
0811:                } else if (object instanceof  Element) {
0812:
0813:                    elm = (Element) object;
0814:                } else if ((sElmTagName == null) || (sElmTagName.length() == 0)) {
0815:
0816:                    String sMsg = "sElmTagName == Null/Empty";
0817:                    throw new XMLJdomFileCreationException(sMsg);
0818:                } else if (object instanceof  String) {
0819:
0820:                    elm = convertStringToElement(sElmTagName, (String) object);
0821:                } else if (object instanceof  Properties) {
0822:
0823:                    elm = convertPropertiesToElement(sElmTagName,
0824:                            (Properties) object);
0825:                } else if (object instanceof  Map) {
0826:
0827:                    elm = convertMapToElement(sElmTagName, (Map) object);
0828:                } else {
0829:
0830:                    String sMsg = "Unknown object type";
0831:                    throw new XMLJdomFileCreationException(sMsg);
0832:                }
0833:
0834:                return elm;
0835:            }
0836:
0837:            /**
0838:             * Convert java.util.Properties to org.jdom.Element.
0839:             */
0840:            private Element convertPropertiesToElement(
0841:                    final String sElmTagName, final Properties sProp)
0842:                    throws XMLJdomFileCreationException {
0843:
0844:                Element elm = new Element(sElmTagName);
0845:                for (Enumeration enum1 = sProp.propertyNames(); enum1
0846:                        .hasMoreElements();) {
0847:
0848:                    String sKey = (String) enum1.nextElement();
0849:                    String sValue = (String) sProp.getProperty(sKey);
0850:                    elm.setAttribute(sKey, sValue);
0851:                }
0852:
0853:                return elm;
0854:            }
0855:
0856:            /**
0857:             * Convert java.util.Map to org.jdom.Element.
0858:             */
0859:            private Element convertMapToElement(final String sElmTagName,
0860:                    final Map sMap) throws XMLJdomFileCreationException {
0861:
0862:                Element elm = new Element(sElmTagName);
0863:                Iterator itr = sMap.keySet().iterator();
0864:                while (itr.hasNext()) {
0865:
0866:                    String sKey = (String) itr.next();
0867:                    String sValue = (String) sMap.get(sKey);
0868:                    elm.setAttribute(sKey, sValue);
0869:                }
0870:
0871:                return elm;
0872:            }
0873:
0874:            /**
0875:             * Convert java.util.String to org.jdom.Element. 
0876:             */
0877:            private Element convertStringToElement(final String sElmTagName,
0878:                    final String str) throws XMLJdomFileCreationException {
0879:
0880:                Element elm = new Element(sElmTagName);
0881:                elm.setText(str);
0882:
0883:                return elm;
0884:            }
0885:
0886:            /**
0887:             * Remove a particular child (org.jdom.Element) of the xml document 
0888:             * (org.jdom.Document). Only the first occurence is removed if there
0889:             * are more then one children (org.jdom.Element) with the same path.
0890:             */
0891:            public void removeChild(final String sChildPath)
0892:                    throws XMLJdomFileCreationException {
0893:
0894:                Element elm = getChildAsElement(sChildPath);
0895:
0896:                if (elm != null) {
0897:
0898:                    elm.detach();
0899:                }
0900:            }
0901:
0902:            /**
0903:             * Remove a particular attribute (org.jdom.Attribute) of a particular
0904:             * child (org.jdom.Element) of the xml document (org.jdom.Document).
0905:             */
0906:            public void removeAttribute(final String sChildPath,
0907:                    final String sAttribName)
0908:                    throws XMLJdomFileCreationException {
0909:
0910:                if ((sAttribName == null) || (sAttribName.length() == 0)) {
0911:
0912:                    String sMsg = "sAttribName == Null/Empty";
0913:                    throw new XMLJdomFileCreationException(sMsg);
0914:                } else {
0915:
0916:                    Element elm = getChildAsElement(sChildPath);
0917:
0918:                    if (elm != null) {
0919:
0920:                        elm.removeAttribute(sAttribName);
0921:                    }
0922:                }
0923:            }
0924:
0925:            private void indentXML(File inXMLFile, File outXMLFile)
0926:                    throws JDOMException, IOException, FileNotFoundException {
0927:
0928:                SAXBuilder sbuilder = new SAXBuilder();
0929:                Document doc = sbuilder.build(inXMLFile);
0930:                FileOutputStream fos = new FileOutputStream(outXMLFile);
0931:                XMLOutputter fmt = new XMLOutputter(Format.getPrettyFormat());
0932:
0933:                fmt.output(doc, fos);
0934:                fos.close();
0935:            }
0936:
0937:            /*??private void tagAncestors(Element element) {
0938:
0939:                if (element == null) {
0940:
0941:                    return;
0942:                }
0943:
0944:                Element parent = element;
0945:                while(true) {
0946:
0947:                    if (parent.isRootElement()) {
0948:
0949:                        break;
0950:                    }
0951:
0952:                    parent = (Element) parent.getParent();
0953:                    Attribute sAttrib = parent.getAttribute(TAGGED);
0954:                    if (sAttrib != null) {
0955:
0956:                        break;
0957:                    }
0958:
0959:                    parent.setAttribute(TAGGED, TAGGED);
0960:                }
0961:            }
0962:
0963:            private void pruneXMLDocument() {
0964:
0965:                pruneXMLDocument(doc.getRootElement());
0966:            }
0967:
0968:            private void pruneXMLDocument(Element element) {
0969:
0970:                if (element == null) {
0971:
0972:                    return;
0973:                }
0974:
0975:                Attribute sAttrib = (element != null) ? element.getAttribute(TAGGED) : null;
0976:                if (sAttrib != null) {
0977:
0978:                   element.removeAttribute(TAGGED);
0979:                   Iterator itr = element.getChildren().iterator();
0980:                   while(itr.hasNext()) {
0981:
0982:                       pruneXMLDocument((Element) itr.next());
0983:                   }
0984:                } else {
0985:
0986:                   // Remove the element and all its children/grandchildren/so on...
0987:                   if (element.isRootElement()) {
0988:
0989:                       doc.detachRootElement();
0990:                       doc = null;
0991:                   } else {
0992:
0993:                       Element parent = (Element) element.getParent();
0994:                       parent.removeContent(element);
0995:                   }
0996:                }
0997:            }??*/
0998:
0999:            private void prepareHash(final Element rootElm,
1000:                    final String sTagPath) {
1001:
1002:                Element myRootElm = null;
1003:                if (rootElm == null) {
1004:
1005:                    myRootElm = (doc != null) ? doc.getRootElement() : rootElm;
1006:                }
1007:
1008:                if (myRootElm != null) {
1009:
1010:                    String sNewTagPath = (sTagPath == null) ? ""
1011:                            : (sTagPath + "@");
1012:                    sNewTagPath = sNewTagPath + myRootElm.getName();
1013:                    if (hash.get(sNewTagPath) == null) {
1014:
1015:                        hash.put(sNewTagPath, myRootElm);
1016:                    }
1017:
1018:                    Iterator itr = myRootElm.getChildren().iterator();
1019:                    while (itr.hasNext()) {
1020:
1021:                        Element oneLevelDeep = (Element) itr.next();
1022:                        prepareHash(oneLevelDeep, sNewTagPath);
1023:                    }
1024:                }
1025:            }
1026:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.