001package com.thed.zblast.parser.util;
002
003import org.w3c.dom.Document;
004import org.w3c.dom.Node;
005import org.w3c.dom.NodeList;
006import org.xml.sax.ErrorHandler;
007import org.xml.sax.SAXException;
008import org.xml.sax.SAXParseException;
009
010import javax.xml.parsers.DocumentBuilder;
011import javax.xml.parsers.DocumentBuilderFactory;
012import javax.xml.xpath.XPath;
013import javax.xml.xpath.XPathConstants;
014import javax.xml.xpath.XPathExpressionException;
015import javax.xml.xpath.XPathFactory;
016import java.io.File;
017import java.io.OutputStreamWriter;
018import java.io.PrintWriter;
019import java.util.Vector;
020
021public class ZXMLDocumentFactory {
022        private static DocumentBuilder documentBuilder;
023        private static final XPath xpathFactory = XPathFactory.newInstance().newXPath();
024        private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
025        private static OutputStreamWriter outpuStreamWriter;
026        protected static Vector<File> testcaseMessageAttachments = new Vector();
027        private static int maxSizeAttachments = 20971520; //20MB in Bytes
028
029        public static synchronized Document createXMLDoc(File paramFile) {
030                Document localDocument = null;
031                try {
032                        System.out.println("Inside Try");
033                        System.out.println("File :"+paramFile.toURI().toURL().toString());
034                        
035                        synchronized (documentBuilder) {
036                                localDocument = documentBuilder.parse(paramFile.toURI().toURL().toString());
037                        }
038                        
039                } catch (Exception localException) {
040                        System.err.println("create document exception: " + localException.toString());
041                } catch (Throwable localThrowable) {
042                        System.out.println("builder.parse: " + localThrowable);
043                }
044                return localDocument;
045        }
046
047        public static NodeList getNodeObjListFromXPath(Document paramDocument, String paramString) {
048                NodeList localNodeList = null;
049                try {
050                        localNodeList = (NodeList) xpathFactory.evaluate(paramString, paramDocument, XPathConstants.NODESET);
051                } catch (XPathExpressionException localXPathExpressionException) {
052                        System.out.println("""
053                                        evaluate: %s --> %s""".formatted(paramString, localXPathExpressionException));
054                }
055                return localNodeList;
056        }
057
058        public static void addTestcaseMessageAttachment(File paramFile) {
059                _addAttachment(paramFile, testcaseMessageAttachments);
060        }
061
062        private static void _addAttachment(File paramFile, Vector<File> paramVector) {
063                if ((paramFile != null) && (paramFile.exists()) && (paramFile.isFile()))
064                        if ((maxSizeAttachments == 0) || (paramFile.length() <= maxSizeAttachments))
065                                paramVector.add(paramFile);
066                        else
067                                System.out.println("""
068                                                attachment not added because it exceeds the limit set by the server configuration: 
069                                                %s > %s (bytes)""".formatted(paramFile.length(), maxSizeAttachments));
070        }
071
072        public static String getNodeStringValueFromXPath(Node paramNode, String paramString) {
073                Node localNode1 = getNodeFromXPath(paramNode, paramString);
074                String str = null;
075                if (localNode1 != null) {
076                        Node localNode2 = localNode1.getFirstChild();
077                        if (localNode2 != null)
078                                str = localNode2.getNodeValue();
079                }
080                return str;
081        }
082
083        public static Node getNodeFromXPath(Node paramNode, String paramString) {
084                Node localNode = null;
085                try {
086                        localNode = (Node) xpathFactory.evaluate(paramString, paramNode, XPathConstants.NODE);
087                } catch (XPathExpressionException localXPathExpressionException) {
088                        System.out.println("""
089                                        evaluate: %s --> %s""".formatted(paramString, localXPathExpressionException));
090                }
091                return localNode;
092        }
093
094        public static NodeList getNodeObjListFromXPath(Node paramNode, String paramString) {
095                NodeList localNodeList = null;
096                try {
097                        localNodeList = (NodeList) xpathFactory.evaluate(paramString, paramNode, XPathConstants.NODESET);
098                } catch (XPathExpressionException localXPathExpressionException) {
099                        System.out.println("""
100                                        evaluate: %s --> %s""".formatted(paramString, localXPathExpressionException));
101                }
102                return localNodeList;
103        }
104        
105        static
106          {
107            try
108            {
109              documentBuilder = documentBuilderFactory.newDocumentBuilder();
110              outpuStreamWriter = new OutputStreamWriter(System.err, "UTF-8");
111              documentBuilder.setErrorHandler(new CErrorHandler(new PrintWriter(outpuStreamWriter, true)));
112            }
113            catch (Exception localException)
114            {
115            }
116          }
117        
118        private static class CErrorHandler
119    implements ErrorHandler
120  {
121    private final PrintWriter out;
122
123    CErrorHandler(PrintWriter paramPrintWriter)
124    {
125      this.out = paramPrintWriter;
126    }
127
128    public void warning(SAXParseException paramSAXParseException)
129      throws SAXException
130    {
131      String str = paramSAXParseException.getSystemId();
132      this.out.println(new StringBuilder().append("Warning: Error at line ").append(paramSAXParseException.getLineNumber()).append(str == null ? "" : new StringBuilder().append(" (system id = ").append(str).append(")").toString()).append(" : ").append(paramSAXParseException.getMessage()).toString());
133    }
134
135    public void error(SAXParseException paramSAXParseException)
136      throws SAXException
137    {
138      String str1 = paramSAXParseException.getSystemId();
139      String str2 = new StringBuilder().append("Error: Error at line ").append(paramSAXParseException.getLineNumber()).append(str1 == null ? "" : new StringBuilder().append(" (system id = ").append(str1).append(")").toString()).append(" : ").append(paramSAXParseException.getMessage()).toString();
140      throw new SAXException(str2);
141    }
142
143    public void fatalError(SAXParseException paramSAXParseException)
144      throws SAXException
145    {
146      String str1 = paramSAXParseException.getSystemId();
147      String str2 = new StringBuilder().append("Fatal Error: Error at line ").append(paramSAXParseException.getLineNumber()).append(str1 == null ? "" : new StringBuilder().append(" (system id = ").append(str1).append(")").toString()).append(" : ").append(paramSAXParseException.getMessage()).toString();
148      throw new SAXException(str2);
149    }
150  }
151
152}