Commit 42db4170 by Patryk Czarnik

Klient Dispatcher z XSLT po stronie klienta

parent 59970b7b
...@@ -32,4 +32,5 @@ build/ ...@@ -32,4 +32,5 @@ build/
.vscode/ .vscode/
### Mac OS ### ### Mac OS ###
.DS_Store .DS_Store
\ No newline at end of file /wynik.html
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">
<html>
<head>
<style type="text/css">
<xsl:call-template name="css"/>
</style>
</head>
<body><xsl:apply-templates/></body>
</html>
</xsl:template>
<xsl:template match="ogloszenie">
<div class="ogloszenie">
<h2>
<xsl:apply-templates select="marka"/>
<xsl:text> </xsl:text>
<xsl:apply-templates select="model"/>
<xsl:text> </xsl:text>
<xsl:apply-templates select="generacja"/>
</h2>
<p>Rocznik: <b><xsl:value-of select="rok-produkcji"/></b></p>
<p><b><xsl:value-of select="cena"/></b></p>
<p class="tytul"><xsl:apply-templates select="opis"/></p>
<p>Licznik: <b><xsl:value-of select="stan-licznika"/></b></p>
<xsl:call-template name="silnik" />
<xsl:apply-templates select="sprzedawca" />
</div>
</xsl:template>
<xsl:template name="silnik">
<div>
Silnik: <b><xsl:value-of select="pojemnosc"/></b>
<xsl:text> </xsl:text>
<b><xsl:value-of select="moc"/></b>
(<xsl:value-of select="paliwo"/>)
</div>
</xsl:template>
<xsl:template match="sprzedawca">
<div>
<h3>Sprzedawca</h3>
<p><xsl:value-of select="nazwa"/></p>
<p><xsl:value-of select="adres/ulica"/></p>
<p><xsl:value-of select="adres/kod-pocztowy"/>
<xsl:text> </xsl:text>
<xsl:value-of select="adres/miasto"/>
</p>
<p>Kontakt: <xsl:value-of select="telefon"/>
<xsl:text> </xsl:text>
<xsl:value-of select="email"/></p>
</div>
</xsl:template>
<xsl:template name="css"><![CDATA[
body {
background-color: #FFFFDD;
}
.ogloszenie {
border: outset 4px #FF4488;
padding: 0.5em;
margin: 1em;
background-color: #EEFFFF;
}
]]>
</xsl:template>
</xsl:stylesheet>
\ No newline at end of file
package ogloszenia.klient_niskopoziomowy;
import jakarta.xml.ws.Dispatch;
import jakarta.xml.ws.Service;
import ogloszenia.generated.OgloszeniaService;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
public class Klient5_DispatcherXSLT {
static final String trescXML = """
<o:readOne xmlns:o="http://soap.ogloszenia/">
<id>1</id>
</o:readOne>""";
public static void main(String[] args) {
System.out.println("Startujemy...");
OgloszeniaService service = new OgloszeniaService();
QName portName = OgloszeniaService.OgloszeniaPort;
Dispatch<Source> dispatch = service.createDispatch(portName, Source.class, Service.Mode.PAYLOAD);
System.out.println("Mam Dispatchera " + dispatch);
Source src = new StreamSource(new StringReader(trescXML));
System.out.println("Wysyłam zapytanie");
Source result = dispatch.invoke(src);
System.out.println("Mam wynik: " + result);
Utils utils = new Utils();
utils.xslt(result, "arkusz.xsl", "wynik.html");
System.out.println("Gotowe");
}
}
...@@ -2,6 +2,7 @@ package ogloszenia.klient_niskopoziomowy; ...@@ -2,6 +2,7 @@ package ogloszenia.klient_niskopoziomowy;
import javax.xml.transform.*; import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class Utils { public class Utils {
private TransformerFactory tf = TransformerFactory.newInstance(); private TransformerFactory tf = TransformerFactory.newInstance();
...@@ -16,4 +17,15 @@ public class Utils { ...@@ -16,4 +17,15 @@ public class Utils {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public void xslt(Source src, String arkusz, String wyjscie) {
try {
Transformer transformer = tf.newTransformer(new StreamSource(arkusz));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult res = new StreamResult(wyjscie);
transformer.transform(src, res);
} catch (TransformerException e) {
throw new RuntimeException(e);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment