-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
109 changed files
with
18,376 additions
and
380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
components/fluent-builtin/src/main/java/io/fluent/builtin/XmlUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.fluent.builtin; | ||
|
||
import cn.hutool.core.util.XmlUtil; | ||
import jakarta.xml.bind.JAXBContext; | ||
import jakarta.xml.bind.Unmarshaller; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.w3c.dom.Document; | ||
|
||
import javax.xml.xpath.XPathConstants; | ||
import java.io.BufferedInputStream; | ||
import java.io.FileInputStream; | ||
|
||
@Slf4j | ||
public class XmlUtils extends XmlUtil { | ||
|
||
public static Document getDocument(String xmlFilePath) { | ||
return XmlUtil.readXML(xmlFilePath); | ||
} | ||
|
||
public static Object getValueByXpath(String xmlFilePath, | ||
String xpathExpr) { | ||
Document doc = getDocument(xmlFilePath); | ||
return XmlUtil.getByXPath(xpathExpr, doc, XPathConstants.STRING); | ||
} | ||
|
||
public static Object getValueByXpath(Document doc, | ||
String xpathExpr) { | ||
return XmlUtil.getByXPath(xpathExpr, doc, XPathConstants.STRING); | ||
} | ||
|
||
/** | ||
* JAXB Read Xml file | ||
* | ||
* @param xmlFilePath | ||
* @param clazz | ||
* @return | ||
*/ | ||
public static <T> T readXmlToObject(String xmlFilePath, Class<T> clazz) { | ||
|
||
try { | ||
JAXBContext context = JAXBContext.newInstance(clazz); | ||
Unmarshaller unmarshaller = context.createUnmarshaller(); | ||
return (T) unmarshaller.unmarshal(new BufferedInputStream(new FileInputStream(xmlFilePath))); | ||
} catch (Exception e) { | ||
log.error("parse xml failed,", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static <T> T readXmlToObject(Document doc, Class<T> clazz) { | ||
|
||
try { | ||
JAXBContext context = JAXBContext.newInstance(clazz); | ||
Unmarshaller unmarshaller = context.createUnmarshaller(); | ||
return (T) unmarshaller.unmarshal(doc); | ||
} catch (Exception e) { | ||
log.error("parse xml failed,", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea/modules.xml | ||
.idea/jarRepositories.xml | ||
.idea/compiler.xml | ||
.idea/libraries/ | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
Oops, something went wrong.