OpenKeyWord  Build_ID: 457, Datum: 01.02.2020 07:45:48
Dont repeat yourself. - Do it once and only once!
OKW_XmlReader.java
1 package okw;
2 
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.ArrayList;
7 
8 import javax.xml.bind.JAXBException;
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.ParserConfigurationException;
12 import javax.xml.xpath.XPath;
13 import javax.xml.xpath.XPathConstants;
14 import javax.xml.xpath.XPathExpressionException;
15 import javax.xml.xpath.XPathFactory;
16 
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Node;
19 import org.w3c.dom.NodeList;
20 import org.xml.sax.SAXException;
21 
22 import okw.exceptions.OKWMessageNotFoundException;
23 
24 public class OKW_XmlReader // extends ClassLoader
25 {
26 
27  private Document myXMLDocument;
28  private DocumentBuilderFactory mydbFactory;
29  private DocumentBuilder mydBuilder;
30  private XPath myXPath;
31 
32  private String myXMLFile;
33 
34  public String getXMLFile()
35  {
36  return myXMLFile.toString();
37  }
38 
39 
40  public OKW_XmlReader(String fpsXMLFile) throws JAXBException, ParserConfigurationException, SAXException, IOException
41  {
42  //myXMLFile = Paths.get( fpsXMLFile );
43  myXMLFile = fpsXMLFile;
44  Init();
45  }
46 
60  private void Init() throws JAXBException, ParserConfigurationException, SAXException, IOException
61  {
62  InputStream is = OKW_XmlReader.class.getResourceAsStream( myXMLFile.toString() );
63 
64  if ( is == null )
65  {
66  System.out.println(
67  "============================================================================================================");
68  System.out.println("OKW Exception: File not found! -> '" + myXMLFile.toString() + "'");
69  System.out.println(
70  "============================================================================================================");
71 
72  throw new FileNotFoundException("File not found! The File was: '" + this.myXMLFile.toString() + "'");
73  }
74  else
75  {
76  this.mydbFactory = DocumentBuilderFactory.newInstance();
77  this.mydBuilder = mydbFactory.newDocumentBuilder();
78  this.myXMLDocument = mydBuilder.parse(is);
79  this.myXPath = XPathFactory.newInstance().newXPath();
80  }
81  }
82 
103  public String getTextContentSingleValue( String fpsXPathExpression )
104  {
105  String lvsReturn = "Message Not Found!";
106 
107  try
108  {
109 
110  NodeList myNodeList = (NodeList) myXPath.compile(fpsXPathExpression).evaluate(this.myXMLDocument,
111  XPathConstants.NODESET);
112 
113 
114  if (myNodeList.getLength() == 1)
115  {
116  Node myNode = myNodeList.item(0);
117  lvsReturn = myNode.getTextContent();
118  }
119  else if (myNodeList.getLength() < 1)
120  {
121  throw new OKWMessageNotFoundException("TextContent not Found!: " + fpsXPathExpression );
122  }
123  else
124  {
125  throw new OKWMessageNotFoundException("TextContent not Unique!: " + fpsXPathExpression );
126  }
127  }
128  // OKWMessageNotFoundException
129  catch (XPathExpressionException e)
130  {
131  OKW_HandleException.StopRunning(e, this.getClass());
132  }
133 
134  return lvsReturn;
135  }
136 
164  public ArrayList<String> getTextContentAsList( String fpsXPathExpression )
165  {
166 
167  ArrayList<String> lvALReturn = new ArrayList<String>();
168 
169  try
170  {
171 
172  NodeList myNodeList = (NodeList) myXPath.compile(fpsXPathExpression).evaluate(this.myXMLDocument,
173  XPathConstants.NODESET);
174 
175  int lviCount = myNodeList.getLength();
176 
177  if (lviCount >= 1)
178  {
179  for( int i = 0; i < lviCount; i++ )
180  {
181  Node myNode = myNodeList.item(i);
182  lvALReturn.add( myNode.getTextContent() );
183  }
184  }
185  else
186  {
187  throw new OKWMessageNotFoundException("TextContent not Found!");
188  }
189  }
190 
191  catch (OKWMessageNotFoundException | XPathExpressionException e)
192  {
193  OKW_HandleException.StopRunning(e, this.getClass());
194  }
195 
196  return lvALReturn;
197  }
198 
199 }
okw.OKW_HandleException
Definition: OKW_HandleException.java:6
okw.OKW_XmlReader.Init
void Init()
Initialisiert die Klasse:
Definition: OKW_XmlReader.java:60
okw.OKW_XmlReader.getTextContentSingleValue
String getTextContentSingleValue(String fpsXPathExpression)
Liest den TextContent eines Tag.
Definition: OKW_XmlReader.java:103
okw.exceptions.OKWMessageNotFoundException
Die Ausnahme OKWMessageNotFoundException wird ausgelöst, wenn ein Nachrichten-Eintrag in einer XML/Lo...
Definition: OKWMessageNotFoundException.java:56
okw.OKW_XmlReader
Definition: OKW_XmlReader.java:24
okw.OKW_XmlReader.getTextContentAsList
ArrayList< String > getTextContentAsList(String fpsXPathExpression)
Interne Kernfunktion holt die Log-Meldung mit Platzhaltern aus der XML-Datei.
Definition: OKW_XmlReader.java:164