Source Code Cross Referenced for XMLHelper.java in  » Workflow-Engines » JFolder » org » jfolder » common » utils » xml » 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 » Workflow Engines » JFolder » org.jfolder.common.utils.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JFolder, Copyright 2001-2006 Gary Steinmetz
003:         *
004:         * Distributable under LGPL license.
005:         * See terms of license at gnu.org.
006:         */
007:
008:        package org.jfolder.common.utils.xml;
009:
010:        //base classes
011:        import java.io.File;
012:        import java.io.FileWriter;
013:        import java.io.IOException;
014:        import java.io.StringReader;
015:        import java.io.StringWriter;
016:        import java.io.Writer;
017:        import java.lang.reflect.Method;
018:        import java.util.ArrayList;
019:        import java.util.HashMap;
020:        import java.util.Iterator;
021:        import java.util.StringTokenizer;
022:        import javax.xml.parsers.DocumentBuilder;
023:        import javax.xml.parsers.DocumentBuilderFactory;
024:        import javax.xml.parsers.ParserConfigurationException;
025:        import org.w3c.dom.Attr;
026:        import org.w3c.dom.CharacterData;
027:        import org.w3c.dom.Comment;
028:        import org.w3c.dom.Document;
029:        import org.w3c.dom.Element;
030:        import org.w3c.dom.NamedNodeMap;
031:        import org.w3c.dom.Node;
032:        import org.w3c.dom.NodeList;
033:        import org.xml.sax.InputSource;
034:        import org.xml.sax.SAXException;
035:
036:        //project specific classes
037:        import org.jfolder.common.UnexpectedSystemException;
038:        import org.jfolder.common.utils.misc.MiscHelper;
039:
040:        //other classes
041:
042:        public final class XMLHelper {
043:
044:            //
045:            public final static String GT_XML = ">";
046:            public final static String LT_XML = "<";
047:            public final static String AMP_XML = "&";
048:            public final static String APOST_XML = "'";
049:            public final static String QUOTE_XML = """;
050:            //
051:            public final static String NBSP_XML = " ";
052:            public final static char NBSP_CHAR = (char) 160;
053:
054:            public final static Document loadDocument(String inText)
055:                    throws ParserConfigurationException, SAXException,
056:                    IOException {
057:
058:                //try {
059:                Document outValue = null;
060:                //
061:                StringReader sr = new StringReader(inText);
062:                InputSource is = new InputSource(sr);
063:                //
064:                //SAXBuilder builder = new SAXBuilder();
065:                //org.jdom.Document doc = builder.build(is);
066:                //
067:                //DOMOutputter outputter = new DOMOutputter();
068:                //outValue = outputter.output(doc);
069:                //
070:                //SAXReader reader = new SAXReader();
071:                //reader.setValidation(false);
072:                //org.dom4j.Document doc = reader.read(is);
073:                //
074:                //DOMWriter writer = new DOMWriter();
075:                //outValue = writer.write(doc);
076:                //
077:                //org.dom4j.Document doc = writer.read(is);
078:                //outValue = getDocumentBuilder().parse(is);
079:                //String compareText = MiscHelper.readTextFile(
080:                //    new File("./config.xml"));
081:                //MiscHelper.println("inText.length() = " + inText.length());
082:                //MiscHelper.println("compareText.length() = "
083:                //    + compareText.length());
084:                //for (int i = 0; i < 10; i++) {
085:                //    MiscHelper.println(compareText.equals(inText));
086:                //}
087:                //MiscHelper.writeTextFile(new File("./config.xml"), inText);
088:                //outValue = getDocumentBuilder().parse(
089:                //    new File("./config.xml"));
090:                outValue = getDocumentBuilder().parse(is);
091:                //try {
092:                //    Builder builder = new Builder();
093:                //    nu.xom.Document doc = builder.build(sr);
094:                //    //
095:                //    outValue = DOMConverter.convert(
096:                //        doc, createBlankDocument().getImplementation());
097:                //}
098:                //catch (Exception e) {
099:                //    MiscHelper.writeMessage(XMLHelper.class, e);
100:                //}
101:
102:                //for (int i = 0; i < 200; i++) {
103:                //    MiscHelper.println("Yeah! No XML parsing Exception");
104:                //}
105:
106:                return outValue;
107:                //}
108:                //catch (JDOMException jdome) {
109:                //    throw new SAXException(jdome);
110:                //}
111:            }
112:
113:            public final static Document loadDocument(File inFile)
114:                    throws ParserConfigurationException, SAXException,
115:                    IOException {
116:
117:                //try {
118:                Document outValue = null;
119:                //
120:                //SAXBuilder builder = new SAXBuilder();
121:                //org.jdom.Document doc = builder.build(inFile);
122:                //
123:                //DOMOutputter outputter = new DOMOutputter();
124:                //outValue = outputter.output(doc);
125:                //StringReader sr = new StringReader(inText);
126:                //InputSource is = new InputSource(sr);
127:                //
128:                //SAXReader reader = new SAXReader();
129:                //reader.setValidation(false);
130:                //org.dom4j.Document doc = reader.read(inFile);
131:                //
132:                //DOMWriter writer = new DOMWriter();
133:                //outValue = writer.write(doc);
134:                //
135:                //org.dom4j.Document doc = writer.read(is);
136:                //outValue = getDocumentBuilder().parse(is);
137:                outValue = getDocumentBuilder().parse(inFile);
138:                //try {
139:                //    Builder builder = new Builder();
140:                //    nu.xom.Document doc = builder.build(inFile);
141:                //    //
142:                //    outValue = DOMConverter.convert(
143:                //        doc, createBlankDocument().getImplementation());
144:                //}
145:                //catch (Exception e) {
146:                //    MiscHelper.writeMessage(XMLHelper.class, e);
147:                //}
148:
149:                return outValue;
150:                //}
151:                //catch (JDOMException jdome) {
152:                //    throw new SAXException(jdome);
153:                //}
154:                //Document outValue = getDocumentBuilder().parse(inFile);
155:                //
156:                //return outValue;
157:            }
158:
159:            //public final static Document loadDocument(InputSource inIs)
160:            //    throws ParserConfigurationException, SAXException, IOException {
161:            //    
162:            //    //try {
163:            //        Document outValue = null;
164:            //        //
165:            //        //SAXBuilder builder = new SAXBuilder();
166:            //        //org.jdom.Document doc = builder.build(inIs);
167:            //        //
168:            //        //DOMOutputter outputter = new DOMOutputter();
169:            //        //outValue = outputter.output(doc);
170:            //        //StringReader sr = new StringReader(inText);
171:            //        //InputSource is = new InputSource(sr);
172:            //        //
173:            //        //SAXReader reader = new SAXReader();
174:            //        //reader.setValidation(false);
175:            //        //org.dom4j.Document doc = reader.read(inIs);
176:            //        //
177:            //        //DOMWriter writer = new DOMWriter();
178:            //        //outValue = writer.write(doc);
179:            //        //
180:            //        //org.dom4j.Document doc = writer.read(is);
181:            //        //outValue = getDocumentBuilder().parse(is);
182:            //        
183:            //        String compareText = MiscHelper.readTextFile(
184:            //            new File("./config.xml"));
185:            //        String compareSecondText = MiscHelper.readReader(
186:            //            inIs.getCharacterStream());
187:            //        
188:            //        for (int i = 0; i < 100; i++) {
189:            //            MiscHelper.println(
190:            //                "FILE == XML = " + compareText.equals(compareSecondText));
191:            //        }
192:            //        
193:            //        //outValue = getDocumentBuilder().parse(inIs);
194:            //        //
195:            //        //try {
196:            //        //    Builder builder = new Builder();
197:            //        //    nu.xom.Document doc = builder.build(inIs.getByteStream());
198:            //        //    //
199:            //        //    outValue = DOMConverter.convert(
200:            //        //        doc, createBlankDocument().getImplementation());
201:            //        //}
202:            //        //catch (Exception e) {
203:            //        //    MiscHelper.writeMessage(XMLHelper.class, e);
204:            //        //}
205:            //        
206:            //        return outValue;
207:            //    //}
208:            //    //catch (JDOMException jdome) {
209:            //    //    throw new SAXException(jdome);
210:            //    //}
211:            //    //Document outValue = getDocumentBuilder().parse(inIs);
212:            //    //
213:            //    //return outValue;
214:            //}
215:
216:            public final static void storeDocument(Document inDoc, File inFile)
217:                    throws IOException {
218:
219:                FileWriter fw = new FileWriter(inFile);
220:
221:                writeDocument(inDoc, fw);
222:
223:                fw.flush();
224:                fw.close();
225:            }
226:
227:            public final static Document createBlankDocument()
228:                    throws ParserConfigurationException {
229:
230:                //try {
231:                Document outValue = null;
232:
233:                outValue = getDocumentBuilder().newDocument();
234:                //
235:                //DOMOutputter outputter = new DOMOutputter();
236:                //outValue = outputter.output(new org.jdom.Document());
237:                //outValue = new DOMDocument();
238:                //outValue = getDocumentBuilder().parse(is);
239:
240:                return outValue;
241:                //}
242:                //catch (JDOMException jdome) {
243:                //    throw new SAXException(jdome);
244:                //}
245:            }
246:
247:            private final static DocumentBuilder getDocumentBuilder()
248:                    throws ParserConfigurationException {
249:
250:                DocumentBuilder outValue = null;
251:
252:                DocumentBuilderFactory dbf = DocumentBuilderFactory
253:                        .newInstance();
254:                dbf.setNamespaceAware(true);
255:                dbf.setValidating(false);
256:                //MiscHelper.println("IS_NAMESPACE_AWARE = "
257:                //+ dbf.isNamespaceAware());
258:                outValue = dbf.newDocumentBuilder();
259:                //
260:                //reset to correct Xerces bug
261:                //try {
262:                //    Class c = Class.forName(
263:                //        "org.apache.xerces.jaxp.DocumentBuilderImpl");
264:                //    Method m = c.getDeclaredMethod("reset", new Class[0]);
265:                //    m.invoke(outValue, new Object[0]);
266:                //    MiscHelper.println("---Resetting DocumentBuilderImpl");
267:                //}
268:                //catch (Exception e) {
269:                //    MiscHelper.writeMessage(XMLHelper.class, e);
270:                //}
271:                ////
272:                //MiscHelper.println("<<<<<<<<<<<<<<<QUICK TEST");
273:                //try {
274:                //    outValue.parse(new File("./config.xml"));
275:                //}
276:                //catch (Exception e) {
277:                //    MiscHelper.writeMessage(XMLHelper.class, e);
278:                //}
279:                //MiscHelper.println("<<<<<<<<<<<<<<<QUICK TEST");
280:                //
281:                //outValue.setErrorHandler(new XMLErrorHandler());
282:
283:                return outValue;
284:            }
285:
286:            public final static void writeDocument(Document inDoc,
287:                    Writer inWriter) throws IOException {
288:                writeDocument(inDoc, inWriter, new HashMap(), false);
289:            }
290:
291:            public final static void writeDocument(Document inDoc,
292:                    Writer inWriter, boolean inIndent) throws IOException {
293:                writeDocument(inDoc, inWriter, new HashMap(), inIndent);
294:            }
295:
296:            public final static void writeDocument(Document inDoc,
297:                    Writer inWriter, HashMap inNameSpaces) throws IOException {
298:                writeDocument(inDoc, inWriter, new HashMap(), false);
299:            }
300:
301:            public final static void writeDocument(Document inDoc,
302:                    Writer inWriter, HashMap inNameSpaces, boolean inIndent)
303:                    throws IOException {
304:
305:                //MiscHelper.println("XMLHelper doc.getDoctEle() = "
306:                //    + inDoc.getDocumentElement());
307:                StringBuffer sb = new StringBuffer();
308:
309:                writeDocument(inDoc.getDocumentElement(), sb,
310:                        initializeNameSpaceNames(inDoc, inNameSpaces), 0, true,
311:                        inIndent, new SpecializedXMLWriterHandler());
312:
313:                inWriter.write(sb.toString());
314:            }
315:
316:            public final static HashMap initializeNameSpaceNames(
317:                    Document inDoc, HashMap inNameSpaces) {
318:
319:                HashMap outValue = new HashMap();
320:
321:                Iterator iter = inNameSpaces.keySet().iterator();
322:
323:                while (iter.hasNext()) {
324:                    String namespace = (String) iter.next();
325:                    String prefix = (String) inNameSpaces.get(namespace);
326:
327:                    namespace = namespace.trim();
328:                    prefix = prefix.trim();
329:
330:                    if (outValue.containsKey(namespace)) {
331:                        throw new UnexpectedSystemException("Namespace '"
332:                                + namespace + "' already declared");
333:                    }
334:
335:                    if (outValue.containsValue(prefix)) {
336:                        throw new UnexpectedSystemException(
337:                                "Namespace prefix '" + prefix
338:                                        + "' already declared");
339:                    }
340:
341:                    outValue.put(namespace, prefix);
342:                }
343:
344:                initializeNameSpaceNames(inDoc.getDocumentElement(), outValue);
345:
346:                return outValue;
347:            }
348:
349:            private final static void initializeNameSpaceNames(
350:                    Element inElement, HashMap inNameSpaces) {
351:
352:                //MiscHelper.println("XMLHelper inElement = " + inElement);
353:                assignNameSpacePrefix(inElement.getNamespaceURI(), inNameSpaces);
354:
355:                NamedNodeMap nnm = inElement.getAttributes();
356:                for (int i = 0; i < nnm.getLength(); i++) {
357:                    Node nextNode = nnm.item(i);
358:                    if (nextNode instanceof  Attr) {
359:                        Attr nextAttr = (Attr) nextNode;
360:                        assignNameSpacePrefix(nextAttr.getNamespaceURI(),
361:                                inNameSpaces);
362:                    }
363:                }
364:
365:                NodeList nl = inElement.getChildNodes();
366:                for (int i = 0; i < nl.getLength(); i++) {
367:                    Node nextNode = nl.item(i);
368:                    if (nextNode instanceof  Element) {
369:                        initializeNameSpaceNames((Element) nextNode,
370:                                inNameSpaces);
371:                    }
372:                }
373:            }
374:
375:            public final static void assignNameSpacePrefix(String inNamespace,
376:                    HashMap inNameSpaces) {
377:
378:                if (inNamespace != null
379:                        && !inNameSpaces.containsKey(inNamespace)) {
380:                    for (int i = 1; true; i++) {
381:                        String candidatePrefix = "a" + i;
382:                        //String candidatePrefix = ((char)('A' + (i%26))) + "" + i;
383:                        if (!inNameSpaces.containsValue(candidatePrefix)) {
384:                            inNameSpaces.put(inNamespace, candidatePrefix);
385:                            break;
386:                        }
387:                    }
388:                }
389:            }
390:
391:            public final static void writeDocument(Element inElement,
392:                    StringBuffer inSb, HashMap inNamespaces,
393:                    SpecializedXMLWriterHandler inSxwh) throws IOException {
394:                //
395:                writeDocument(inElement, inSb, inNamespaces, 0, false, false,
396:                        inSxwh);
397:            }
398:
399:            private final static void writeDocument(Element inElement,
400:                    StringBuffer inSb, HashMap inNamespaces, int inDepth,
401:                    boolean inListNs, boolean inIndent,
402:                    SpecializedXMLWriterHandler inSxwh) throws IOException {
403:
404:                //initialize
405:                String eleName = inElement.getTagName();
406:                if (eleName.indexOf(':') != -1) {
407:                    eleName = eleName.substring(eleName.indexOf(':') + 1);
408:                }
409:                //String eleName = inElement.getLocalName();
410:                NodeList eleNl = inElement.getChildNodes();
411:                NamedNodeMap eleNnm = inElement.getAttributes();
412:                String eleNs = inElement.getNamespaceURI();
413:                String eleNsPrefix = null;
414:
415:                //determine if element contains other elements
416:                boolean elementFound = false;
417:                for (int i = 0; i < eleNl.getLength(); i++) {
418:                    if (eleNl.item(i) instanceof  Element) {
419:                        elementFound = true;
420:                        break;
421:                    }
422:                }
423:
424:                //write beginning of element
425:                if (inIndent) {
426:                    if (!inListNs) {
427:                        inSb.append('\n');
428:                    }
429:                    for (int i = 0; i < inDepth; i++) {
430:                        inSb.append(' ');
431:                    }
432:                }
433:                //
434:                inSb.append('<');
435:                //write element with namespace, if present
436:                if (eleNs != null) {
437:                    eleNsPrefix = ((String) inNamespaces.get(eleNs));
438:                    inSb.append(eleNsPrefix);
439:                    inSb.append(':');
440:                    inSb.append(eleName);
441:                } else {
442:                    inSb.append(eleName);
443:                }
444:                //declare namespaces
445:                if (inListNs) {
446:                    Iterator iter = inNamespaces.keySet().iterator();
447:                    while (iter.hasNext()) {
448:                        String nextNamespace = ((String) iter.next());
449:                        String nextPrefix = ((String) inNamespaces
450:                                .get(nextNamespace));
451:                        inSb.append(" xmlns:");
452:                        inSb.append(nextPrefix);
453:                        inSb.append("=\"");
454:                        inSb.append(nextNamespace);
455:                        inSb.append('\"');
456:                    }
457:                }
458:                //write attributes
459:                for (int i = 0; i < eleNnm.getLength(); i++) {
460:                    Node nextNode = eleNnm.item(i);
461:                    if (nextNode instanceof  Attr) {
462:                        //
463:                        Attr nextAttr = ((Attr) nextNode);
464:                        //
465:                        String nextAttrNs = nextAttr.getNamespaceURI();
466:                        String nextAttrName = nextAttr.getName();
467:                        String nextAttrData = nextAttr.getValue();
468:
469:                        //write attribute with namespace, if present
470:                        if (inSxwh.isConstraintPrintable(inElement, nextAttr)) {
471:                            if (nextAttrNs != null) {
472:                                //
473:                                String nextAttrNsPrefix = null;
474:                                nextAttrNsPrefix = ((String) inNamespaces
475:                                        .get(nextAttrNs));
476:                                //
477:                                inSb.append(' ');
478:                                inSb.append(nextAttrNsPrefix);
479:                                inSb.append(':');
480:                                inSb.append(nextAttrName);
481:                                inSb.append('=');
482:                                inSb.append('\"');
483:                                inSb.append(fromStringToCData(nextAttrData,
484:                                        false));
485:                                inSb.append('\"');
486:                            } else {
487:                                inSb.append(' ');
488:                                inSb.append(nextAttrName);
489:                                inSb.append('=');
490:                                inSb.append('\"');
491:                                inSb.append(fromStringToCData(nextAttrData,
492:                                        false));
493:                                inSb.append('\"');
494:                            }
495:                        }
496:
497:                    }
498:                }
499:                //
500:                //
501:                //
502:                if (eleNl.getLength() > 0) {
503:                    //
504:                    //
505:                    inSb.append('>');
506:                    //
507:                    //write element contents
508:                    for (int i = 0; i < eleNl.getLength(); i++) {
509:                        Node nextNode = eleNl.item(i);
510:                        if (nextNode instanceof  Element) {
511:                            Element nextEle = (Element) nextNode;
512:                            //elementFound = true;
513:                            HashMap nextEleNsMap = ((HashMap) inNamespaces
514:                                    .clone());
515:                            //
516:                            if (inSxwh.isElementPrintable(nextEle, inSb)) {
517:                                //if (inSxwh.isElementPrintable(inElement, inSb)) {
518:                                writeDocument(nextEle, inSb, nextEleNsMap,
519:                                        (inDepth + 4), false, inIndent, inSxwh);
520:                            }
521:                        }
522:                        //new section
523:                        else if (nextNode instanceof  CharacterData) {
524:                            CharacterData nextCd = (CharacterData) nextNode;
525:                            if (!(nextCd instanceof  Comment)) {
526:                                String nextTextData = nextCd.getData();
527:                                inSb.append(fromStringToCData(nextTextData,
528:                                        true));
529:                            }
530:                        }
531:                        //end new section
532:                    }
533:
534:                    //write ending of element
535:                    if (inIndent && elementFound) {
536:                        inSb.append('\n');
537:                        for (int i = 0; i < inDepth; i++) {
538:                            inSb.append(' ');
539:                        }
540:                    }
541:                    inSb.append('<');
542:                    inSb.append('/');
543:                    if (eleNs != null) {
544:                        inSb.append(eleNsPrefix);
545:                        inSb.append(':');
546:                    } else {
547:                    }
548:                    inSb.append(eleName);
549:                    inSb.append('>');
550:                } else {
551:                    inSb.append("/>");
552:                }
553:            }
554:
555:            public final static String getAttrValue(Element inElement,
556:                    String inName) {
557:
558:                String outValue = null;
559:
560:                Attr attr = inElement.getAttributeNode(inName);
561:                if (attr != null) {
562:                    outValue = attr.getValue();
563:                }
564:
565:                return outValue;
566:            }
567:
568:            public final static String getAttrValue(Element inElement,
569:                    String inNamespace, String inName) {
570:
571:                String outValue = null;
572:
573:                Attr attr = inElement.getAttributeNodeNS(inNamespace, inName);
574:                if (attr != null) {
575:                    outValue = attr.getValue();
576:                }
577:
578:                return outValue;
579:            }
580:
581:            public final static String fromStringToJSCData(String inInput) {
582:
583:                String outValue = null;
584:
585:                StringBuffer sb = new StringBuffer();
586:
587:                for (int i = 0; i < inInput.length(); i++) {
588:
589:                    char nextChar = inInput.charAt(i);
590:
591:                    if (nextChar == '\'' || nextChar == '"' || nextChar == '\\'
592:                            || nextChar == '&' || nextChar == '\n'
593:                            || nextChar == '\r' || nextChar == '\t'
594:                            || nextChar == '\f') {
595:                        //
596:                        sb.append('\\');
597:                        if (nextChar == '\'') {
598:                            sb.append(nextChar);
599:                        } else if (nextChar == '"') {
600:                            sb.append(nextChar);
601:                        } else if (nextChar == '\\') {
602:                            sb.append(nextChar);
603:                        } else if (nextChar == '&') {
604:                            sb.append(nextChar);
605:                        } else if (nextChar == '\n') {
606:                            sb.append('n');
607:                        } else if (nextChar == '\r') {
608:                            sb.append('r');
609:                        } else if (nextChar == '\t') {
610:                            sb.append('t');
611:                        } else if (nextChar == '\f') {
612:                            sb.append('f');
613:                        } else {
614:                            throw UnexpectedSystemException.unknownState();
615:                        }
616:                    } else {
617:                        sb.append(nextChar);
618:                    }
619:                    //outValue.append("\\u");
620://int baseLength = outValue.length();
621:                    //for (int j = 0; j < 4; j++) {
622:                    //    int nextDigit = nextChar%16;
623:                    //    nextChar = (char)(nextChar/16);
624:                    //    outValue.insert(
625:                    //        baseLength, Character.forDigit(nextDigit, 16));
626:                    //}
627:                }
628:
629:                outValue = sb.toString();
630:
631:                //outValue = fromStringToCData(outValue);
632:
633:                return outValue;
634:            }
635:
636:            public final static String fromStringToHTML(String inInput) {
637:
638:                String outValue = null;
639:
640:                outValue = fromStringToCData(inInput, true);
641:
642:                outValue = outValue.replaceAll("  ", " " + NBSP_XML);
643:
644:                return outValue;
645:            }
646:
647:            public final static String fromStringToCData(String inInput) {
648:                return fromStringToCData(inInput, true);
649:            }
650:
651:            private final static String fromStringToCData(String inInput,
652:                    boolean inNotAttr) {
653:
654:                try {
655:                    StringWriter outValue = new StringWriter();
656:
657:                    fromStringToCData(inInput, outValue, inNotAttr);
658:
659:                    return outValue.toString();
660:                } catch (IOException ioe) {
661:                    throw new UnexpectedSystemException(ioe);
662:                }
663:            }
664:
665:            private final static void fromStringToCData(String inInput,
666:                    Writer inWriter, boolean inNotAttr) throws IOException {
667:
668:                if (inInput == null) {
669:                    MiscHelper.println("XMLHelper fromStrToCData null found");
670:                }
671:
672:                StringTokenizer st = new StringTokenizer(inInput, "&<>\""
673:                        + NBSP_CHAR, true);
674:                //    inInput, "&<>'\"" + NBSP_CHAR, true);
675:
676:                while (st.hasMoreTokens()) {
677:                    String nextToken = st.nextToken();
678:                    char testChar = nextToken.charAt(0);
679:
680:                    if ((testChar == '&' && inNotAttr) || testChar == '<'
681:                            || testChar == '>'
682:                            //|| testChar == '\''
683:                            || testChar == '"' || testChar == NBSP_CHAR) {
684:
685:                        for (int i = 0; i < nextToken.length(); i++) {
686:                            char nextChar = nextToken.charAt(i);
687:
688:                            if (nextChar == '&' && inNotAttr) {
689:                                inWriter.write(AMP_XML);
690:                            } else if (nextChar == '<') {
691:                                inWriter.write(LT_XML);
692:                            } else if (nextChar == '>') {
693:                                inWriter.write(GT_XML);
694:                            }
695:                            //else if (nextChar == '\'') {
696:                            //    inWriter.write(APOST_XML);
697:                            //}
698:                            else if (nextChar == '"') {
699:                                inWriter.write(QUOTE_XML);
700:                            } else if (nextChar == NBSP_CHAR) {
701:                                inWriter.write(NBSP_XML);
702:                            } else {
703:                                throw new UnexpectedSystemException(
704:                                        "Character '"
705:                                                + nextChar
706:                                                + "' cannot be converted to an XML"
707:                                                + " escape character");
708:                            }
709:                        }
710:                    } else {
711:                        inWriter.write(nextToken);
712:                    }
713:                }
714:            }
715:
716:            public final static String getText(Element inElement) {
717:
718:                String outValue = "";
719:
720:                NodeList nl = inElement.getChildNodes();
721:
722:                for (int i = 0; i < nl.getLength(); i++) {
723:                    Node nextNode = nl.item(i);
724:
725:                    if (nextNode instanceof  CharacterData) {
726:                        if (!(nextNode instanceof  Comment)) {
727:                            CharacterData cd = (CharacterData) nextNode;
728:                            outValue += cd.getData();
729:                        }
730:                    }
731:                }
732:
733:                return outValue;
734:            }
735:
736:            public final static String convertAttrs(HashMap inProps) {
737:
738:                StringBuffer outValue = new StringBuffer();
739:
740:                Iterator attrMap = inProps.keySet().iterator();
741:                while (attrMap.hasNext()) {
742:                    String propName = (String) attrMap.next();
743:                    String propValue = (String) inProps.get(propName);
744:                    propValue = fromStringToCData(propValue, false);
745:                    outValue.append(" ");
746:                    outValue.append(propName);
747:                    outValue.append("=\"");
748:                    outValue.append(propValue);
749:                    outValue.append("\"");
750:                }
751:
752:                return outValue.toString();
753:            }
754:
755:            //public final static String convertStyles(HashMap inProps) {
756:            //    
757:            //    StringBuffer outValue = new StringBuffer();
758:            //    
759:            //    if (inProps.size() > 0) {
760:            //        //TO DO: 'style' attribute is assumed, will this cause problem?
761:            //        outValue.append(" style=\"");
762:            //        outValue.append(fromStylesToAttr(inProps));
763:            //        outValue.append("\"");
764:            //    }
765:            //    
766:            //    return outValue.toString();
767:            //}
768:
769:            public final static String fromStylesToAttr(HashMap inProps) {
770:
771:                StringBuffer outValue = new StringBuffer();
772:
773:                Iterator attrMap = inProps.keySet().iterator();
774:                while (attrMap.hasNext()) {
775:                    String propName = (String) attrMap.next();
776:                    String propValue = (String) inProps.get(propName);
777:                    outValue.append(" ");
778:                    outValue.append(propName);
779:                    outValue.append(": ");
780:                    outValue.append(propValue);
781:                    outValue.append(";");
782:                }
783:
784:                return outValue.toString();
785:            }
786:
787:            //
788:            //
789:            //
790:            public final static String reformatTextForWebPage(String inValue) {
791:                //
792:
793:                StringBuffer outValue = new StringBuffer();
794:
795:                //make sure Windows newline doesn't show up twice
796:                //String nextData = inCtchar.getValueAsString(inIndex, null);
797:                inValue = inValue.replaceAll("\\r\\n", "\n");
798:                //
799:                //inCtchar.removeValueWithLocalFiles(inIndex);
800:
801:                //
802:                ArrayList futureData = new ArrayList();
803:                StringTokenizer st = new StringTokenizer(inValue, "\r\n", true);
804:                while (st.hasMoreTokens()) {
805:                    futureData.add(st.nextToken());
806:                }
807:
808:                //
809:                for (int i = futureData.size() - 1; i >= 0; i--) {
810:                    //
811:                    String updateData = ((String) futureData.get(i));
812:                    //
813:                    if (updateData.equals("\n") || updateData.equals("\r")) {
814:                        //
815:                        outValue.insert(0, "<br/>");
816:                        //ConceptTag unknownCt = new UnknownWebPageTag(null, "br");
817:                        //inCtchar.addValue(inIndex, unknownCt, true);
818:                    } else {
819:                        updateData = updateData.replaceAll("  ", " "
820:                                + XMLHelper.NBSP_XML);
821:                        outValue.insert(0, updateData);
822:                        //updateData = //updateData.replace(' ', XMLHelper.NBSP_CHAR);
823:                        //inCtchar.addValue(inIndex, updateData);
824:                    }
825:                }
826:
827:                return outValue.toString();
828:            }
829:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.