Testy komentářů, XML.
1.1 --- a/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java Tue Jun 08 10:42:58 2010 +0200
1.2 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java Tue Jun 08 12:56:46 2010 +0200
1.3 @@ -8,6 +8,8 @@
1.4 import cz.frantovo.nekurak.ext.Geo.Souradnice;
1.5 import cz.frantovo.nekurak.ext.Texy;
1.6 import cz.frantovo.nekurak.util.Komentare;
1.7 +import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka;
1.8 +import cz.frantovo.nekurak.vyjimky.TexyVyjimka;
1.9 import java.util.Collection;
1.10 import java.util.Date;
1.11 import java.util.logging.Logger;
1.12 @@ -80,7 +82,7 @@
1.13 }
1.14
1.15 @RolesAllowed("opravneny")
1.16 - public void komentuj(Komentar k) {
1.17 + public void komentuj(Komentar k) throws KomentarovaVyjimka, TexyVyjimka {
1.18 k.setUzivatel(ctx.getCallerPrincipal().getName());
1.19 k.setDatum(new Date());
1.20
1.21 @@ -98,9 +100,10 @@
1.22 }
1.23
1.24 /** Zkontrolujeme XML */
1.25 -
1.26 + Komentare.zkontroluj(k.getKomentar());
1.27
1.28
1.29 + /** TODO: Uložíme do databáze */
1.30 log.severe("Komentář: " + k.getNadpis() + " | " + k.getKomentar() + " | " + k.getTyp());
1.31 }
1.32 }
2.1 --- a/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ext/Texy.java Tue Jun 08 10:42:58 2010 +0200
2.2 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ext/Texy.java Tue Jun 08 12:56:46 2010 +0200
2.3 @@ -1,50 +1,62 @@
2.4 package cz.frantovo.nekurak.ext;
2.5
2.6 +import cz.frantovo.nekurak.vyjimky.TexyVyjimka;
2.7 import java.io.BufferedReader;
2.8 +import java.io.IOException;
2.9 import java.io.InputStreamReader;
2.10 import java.io.OutputStreamWriter;
2.11 import java.net.URL;
2.12 import java.net.URLConnection;
2.13 import java.net.URLEncoder;
2.14 +import java.util.logging.Level;
2.15 +import java.util.logging.Logger;
2.16
2.17 /**
2.18 * Neprovádí vlastní interpretaci Texy syntaxe, ale volá HTTP službu napsanou v PHP.
2.19 + * TODO: použít webové služby, nebo něco lepšího než ruční práci s URL.
2.20 * @author fiki
2.21 */
2.22 public class Texy {
2.23
2.24 - /** TODO: parametrizovatelnost/localhost ? */
2.25 + /** TODO: parametrizovatelnost */
2.26 private static final String URL_SLUZBY = "http://nekurak.net/texy/http/";
2.27 + private static final Logger log = Logger.getLogger(Texy.class.getSimpleName());
2.28
2.29 - public String preved(String text) {
2.30 + public String preved(String text) throws TexyVyjimka {
2.31 + OutputStreamWriter wr = null;
2.32 + BufferedReader rd = null;
2.33 try {
2.34 URL url = new URL(URL_SLUZBY);
2.35 URLConnection spojeni = url.openConnection();
2.36 spojeni.setDoOutput(true);
2.37
2.38 /** Odešleme data */
2.39 - OutputStreamWriter wr = new OutputStreamWriter(spojeni.getOutputStream());
2.40 + wr = new OutputStreamWriter(spojeni.getOutputStream());
2.41 wr.write(URLEncoder.encode(text, "UTF-8"));
2.42 wr.flush();
2.43
2.44 /** Přijmeme odpověď */
2.45 - BufferedReader rd = new BufferedReader(new InputStreamReader(spojeni.getInputStream()));
2.46 + rd = new BufferedReader(new InputStreamReader(spojeni.getInputStream()));
2.47 StringBuffer vysledek = new StringBuffer();
2.48 String radka;
2.49 while ((radka = rd.readLine()) != null) {
2.50 vysledek.append(radka);
2.51 }
2.52
2.53 - // TODO: → finally
2.54 - wr.close();
2.55 - rd.close();
2.56 -
2.57 return vysledek.toString();
2.58 } catch (Exception e) {
2.59 - // TODO: vyhazovat výjimku
2.60 - return "TODO:chyba";
2.61 + throw new TexyVyjimka("Chyba při zpracovávání textu: " + text, e);
2.62 } finally {
2.63 - // TODO: finally close();
2.64 + try {
2.65 + wr.close();
2.66 + } catch (IOException e) {
2.67 + log.log(Level.WARNING, "Selhalo zavírání OutputStreamWriteru", e);
2.68 + }
2.69 + try {
2.70 + rd.close();
2.71 + } catch (IOException e) {
2.72 + log.log(Level.WARNING, "Selhalo zavírání BufferedReaderu", e);
2.73 + }
2.74 }
2.75 }
2.76 }
3.1 --- a/java/nekurak.net-ejb/test/cz/frantovo/nekurak/ext/TexyTest.java Tue Jun 08 10:42:58 2010 +0200
3.2 +++ b/java/nekurak.net-ejb/test/cz/frantovo/nekurak/ext/TexyTest.java Tue Jun 08 12:56:46 2010 +0200
3.3 @@ -1,5 +1,6 @@
3.4 package cz.frantovo.nekurak.ext;
3.5
3.6 +import cz.frantovo.nekurak.vyjimky.TexyVyjimka;
3.7 import org.junit.Test;
3.8 import static org.junit.Assert.*;
3.9
3.10 @@ -15,17 +16,17 @@
3.11 private static final int CASOVY_LIMIT = 1000;
3.12
3.13 @Test(timeout = CASOVY_LIMIT)
3.14 - public void prostyText() {
3.15 + public void prostyText() throws TexyVyjimka {
3.16 testuj("", "");
3.17 testuj("ahoj", "<p>ahoj</p>");
3.18 }
3.19
3.20 @Test(timeout = CASOVY_LIMIT)
3.21 - public void nadpisy() {
3.22 + public void nadpisy() throws TexyVyjimka {
3.23 testuj("===Nadpis===", "<h1>Nadpis</h1>");
3.24 }
3.25
3.26 - private void testuj(String vstup, String pozadovanyVystup) {
3.27 + private void testuj(String vstup, String pozadovanyVystup) throws TexyVyjimka {
3.28 String vystup = texy.preved(vstup);
3.29 assertEquals(pozadovanyVystup, vystup);
3.30 }
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/java/nekurak.net-lib/build.xml Tue Jun 08 12:56:46 2010 +0200
4.3 @@ -0,0 +1,74 @@
4.4 +<?xml version="1.0" encoding="UTF-8"?>
4.5 +<!-- You may freely edit this file. See commented blocks below for -->
4.6 +<!-- some examples of how to customize the build. -->
4.7 +<!-- (If you delete it and reopen the project it will be recreated.) -->
4.8 +<!-- By default, only the Clean and Build commands use this build script. -->
4.9 +<!-- Commands such as Run, Debug, and Test only use this build script if -->
4.10 +<!-- the Compile on Save feature is turned off for the project. -->
4.11 +<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
4.12 +<!-- in the project's Project Properties dialog box.-->
4.13 +<project name="nekurak.net-lib" default="default" basedir=".">
4.14 + <description>Builds, tests, and runs the project nekurak.net-lib.</description>
4.15 + <import file="nbproject/build-impl.xml"/>
4.16 + <!--
4.17 +
4.18 + There exist several targets which are by default empty and which can be
4.19 + used for execution of your tasks. These targets are usually executed
4.20 + before and after some main targets. They are:
4.21 +
4.22 + -pre-init: called before initialization of project properties
4.23 + -post-init: called after initialization of project properties
4.24 + -pre-compile: called before javac compilation
4.25 + -post-compile: called after javac compilation
4.26 + -pre-compile-single: called before javac compilation of single file
4.27 + -post-compile-single: called after javac compilation of single file
4.28 + -pre-compile-test: called before javac compilation of JUnit tests
4.29 + -post-compile-test: called after javac compilation of JUnit tests
4.30 + -pre-compile-test-single: called before javac compilation of single JUnit test
4.31 + -post-compile-test-single: called after javac compilation of single JUunit test
4.32 + -pre-jar: called before JAR building
4.33 + -post-jar: called after JAR building
4.34 + -post-clean: called after cleaning build products
4.35 +
4.36 + (Targets beginning with '-' are not intended to be called on their own.)
4.37 +
4.38 + Example of inserting an obfuscator after compilation could look like this:
4.39 +
4.40 + <target name="-post-compile">
4.41 + <obfuscate>
4.42 + <fileset dir="${build.classes.dir}"/>
4.43 + </obfuscate>
4.44 + </target>
4.45 +
4.46 + For list of available properties check the imported
4.47 + nbproject/build-impl.xml file.
4.48 +
4.49 +
4.50 + Another way to customize the build is by overriding existing main targets.
4.51 + The targets of interest are:
4.52 +
4.53 + -init-macrodef-javac: defines macro for javac compilation
4.54 + -init-macrodef-junit: defines macro for junit execution
4.55 + -init-macrodef-debug: defines macro for class debugging
4.56 + -init-macrodef-java: defines macro for class execution
4.57 + -do-jar-with-manifest: JAR building (if you are using a manifest)
4.58 + -do-jar-without-manifest: JAR building (if you are not using a manifest)
4.59 + run: execution of project
4.60 + -javadoc-build: Javadoc generation
4.61 + test-report: JUnit report generation
4.62 +
4.63 + An example of overriding the target for project execution could look like this:
4.64 +
4.65 + <target name="run" depends="nekurak.net-lib-impl.jar">
4.66 + <exec dir="bin" executable="launcher.exe">
4.67 + <arg file="${dist.jar}"/>
4.68 + </exec>
4.69 + </target>
4.70 +
4.71 + Notice that the overridden target depends on the jar target and not only on
4.72 + the compile target as the regular run target does. Again, for a list of available
4.73 + properties which you can use, check the target you are overriding in the
4.74 + nbproject/build-impl.xml file.
4.75 +
4.76 + -->
4.77 +</project>
5.1 --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/ejb/PodnikRemote.java Tue Jun 08 10:42:58 2010 +0200
5.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/ejb/PodnikRemote.java Tue Jun 08 12:56:46 2010 +0200
5.3 @@ -3,6 +3,8 @@
5.4 import cz.frantovo.nekurak.dto.Komentar;
5.5 import cz.frantovo.nekurak.dto.Podnik;
5.6 import cz.frantovo.nekurak.dto.VysledekHlasovani;
5.7 +import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka;
5.8 +import cz.frantovo.nekurak.vyjimky.TexyVyjimka;
5.9 import java.util.Collection;
5.10 import javax.ejb.Remote;
5.11
5.12 @@ -40,5 +42,5 @@
5.13 /**
5.14 * @param k komentář, který se má přidat k podniku
5.15 */
5.16 - public void komentuj(Komentar k);
5.17 + public void komentuj(Komentar k) throws KomentarovaVyjimka, TexyVyjimka;
5.18 }
6.1 --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/preklady_cs.properties Tue Jun 08 10:42:58 2010 +0200
6.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/preklady_cs.properties Tue Jun 08 12:56:46 2010 +0200
6.3 @@ -90,6 +90,7 @@
6.4 komentar.odeslat.nadpis=Vlo\u017Een\u00ED koment\u00E1\u0159e
6.5 komentar.odeslat.ok=Koment\u00E1\u0159 byl \u00FAsp\u011B\u0161n\u011B vlo\u017Een
6.6 komentar.odeslat.pokracujte=pokra\u010Dujte na str\u00E1nku podniku
6.7 +komentar.odeslat.chyba=Koment\u00E1\u0159 obsahuje nepovolen\u00E9 form\u00E1tov\u00E1n\u00ED.
6.8 komentar.typ.prostyText=Prost\u00FD text
6.9 komentar.typ.xhtml=XHTML form\u00E1tovan\u00FD text
6.10 komentar.typ.texy=Texy form\u00E1tovan\u00FD text
7.1 --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/util/Komentare.java Tue Jun 08 10:42:58 2010 +0200
7.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/util/Komentare.java Tue Jun 08 12:56:46 2010 +0200
7.3 @@ -1,11 +1,28 @@
7.4 package cz.frantovo.nekurak.util;
7.5
7.6 +import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka;
7.7 +import java.io.ByteArrayInputStream;
7.8 +import java.net.URL;
7.9 +import java.util.logging.Level;
7.10 +import java.util.logging.Logger;
7.11 +import javax.xml.XMLConstants;
7.12 +import javax.xml.parsers.DocumentBuilder;
7.13 +import javax.xml.parsers.DocumentBuilderFactory;
7.14 +import javax.xml.validation.Schema;
7.15 +import javax.xml.validation.SchemaFactory;
7.16 +import org.w3c.dom.Document;
7.17 +import org.xml.sax.ErrorHandler;
7.18 +import org.xml.sax.SAXException;
7.19 +import org.xml.sax.SAXParseException;
7.20 +
7.21 /**
7.22 * Validátor komentářů
7.23 * @author fiki
7.24 */
7.25 public class Komentare {
7.26
7.27 + private static final Logger log = Logger.getLogger(Komentare.class.getSimpleName());
7.28 +
7.29 /**
7.30 * Escapuje XML a doplní XHTML zalomení na konce řádků.
7.31 * @param komentar prostý text zadaný uživatelem
7.32 @@ -30,8 +47,42 @@
7.33 * @param komentar
7.34 * @return jestli komentář odpovídá
7.35 */
7.36 - public boolean isValidniXHTML(String komentar) {
7.37 - return false;
7.38 + public static Document zkontroluj(String komentar) throws KomentarovaVyjimka {
7.39 +
7.40 +
7.41 +
7.42 +
7.43 + try {
7.44 + URL soubor = ClassLoader.getSystemResource("cz/frantovo/nekurak/util/komentář.xsd");
7.45 + SchemaFactory tovarnaSchemat = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
7.46 + Schema schema = tovarnaSchemat.newSchema(soubor);
7.47 +
7.48 + DocumentBuilderFactory tovarnaDB = DocumentBuilderFactory.newInstance();
7.49 + tovarnaDB.setSchema(schema);
7.50 +
7.51 + DocumentBuilder db = tovarnaDB.newDocumentBuilder();
7.52 + db.setErrorHandler(new ErrorHandler() {
7.53 +
7.54 + public void warning(SAXParseException e) throws SAXException {
7.55 + throw e;
7.56 + }
7.57 +
7.58 + public void error(SAXParseException e) throws SAXException {
7.59 + throw e;
7.60 + }
7.61 +
7.62 + public void fatalError(SAXParseException e) throws SAXException {
7.63 + throw e;
7.64 + }
7.65 + });
7.66 + Document dokument = db.parse(new ByteArrayInputStream(komentar.getBytes("UTF-8")));
7.67 +
7.68 + return dokument;
7.69 + } catch (Exception e) {
7.70 + throw new KomentarovaVyjimka("Neplatný komentář: " + komentar, e);
7.71 + }
7.72 +
7.73 +
7.74 }
7.75
7.76 private static String escapujXML(String str) {
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/vyjimky/KomentarovaVyjimka.java Tue Jun 08 12:56:46 2010 +0200
8.3 @@ -0,0 +1,12 @@
8.4 +package cz.frantovo.nekurak.vyjimky;
8.5 +
8.6 +/**
8.7 + * Komentář neodpovídá XML schématu
8.8 + * @author fiki
8.9 + */
8.10 +public class KomentarovaVyjimka extends NekurakVyjimka {
8.11 +
8.12 + public KomentarovaVyjimka(String chyba, Exception e) {
8.13 + super(chyba, e);
8.14 + }
8.15 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/vyjimky/TexyVyjimka.java Tue Jun 08 12:56:46 2010 +0200
9.3 @@ -0,0 +1,12 @@
9.4 +package cz.frantovo.nekurak.vyjimky;
9.5 +
9.6 +/**
9.7 + *
9.8 + * @author fiki
9.9 + */
9.10 +public class TexyVyjimka extends NekurakVyjimka {
9.11 +
9.12 + public TexyVyjimka(String chyba, Exception e) {
9.13 + super(chyba, e);
9.14 + }
9.15 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/java/nekurak.net-lib/test/cz/frantovo/nekurak/util/KomentareTest.java Tue Jun 08 12:56:46 2010 +0200
10.3 @@ -0,0 +1,54 @@
10.4 +package cz.frantovo.nekurak.util;
10.5 +
10.6 +import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka;
10.7 +import org.junit.Test;
10.8 +import static org.junit.Assert.*;
10.9 +
10.10 +public class KomentareTest {
10.11 +
10.12 + @Test
10.13 + public void obal() {
10.14 + String vystup = Komentare.obal("asdf");
10.15 + String pozadovanyVystup = "<div>asdf</div>";
10.16 + assertEquals(pozadovanyVystup, vystup);
10.17 + }
10.18 +
10.19 + @Test
10.20 + public void upravProstýTextObal() {
10.21 + String vystup = Komentare.upravProstyText("as\ndf");
10.22 + vystup = Komentare.obal(vystup);
10.23 + String pozadovanyVystup = "<div><p>as<br/>df</p></div>";
10.24 + assertEquals(pozadovanyVystup, vystup);
10.25 + }
10.26 +
10.27 + @Test
10.28 + public void zvláštníZnaky() {
10.29 + String vystup = Komentare.upravProstyText("nbusr123 & | and < … > uvo\"zovky'");
10.30 + String pozadovanyVystup = "<p>nbusr123 & | and < … > uvo"zovky'</p>";
10.31 + System.out.println(vystup);
10.32 + assertEquals(pozadovanyVystup, vystup);
10.33 + }
10.34 +
10.35 + @Test
10.36 + public void xmlJednoduché() throws Exception {
10.37 + Komentare.zkontroluj("<div><p>xxx</p></div>");
10.38 + Komentare.zkontroluj("<div><p>xxx</p><p>xxx</p></div>");
10.39 + Komentare.zkontroluj("<div><p>xxx<strong>tučně</strong>xxx</p></div>");
10.40 + Komentare.zkontroluj("<div><p>xxx<em>tučně</em>xxx</p></div>");
10.41 + }
10.42 +
10.43 + @Test(expected = KomentarovaVyjimka.class)
10.44 + public void xmlChybaÚplněŠpatnéXML() throws Exception {
10.45 + Komentare.zkontroluj("<div>xxx</p></div>");
10.46 + }
10.47 +
10.48 + @Test(expected = KomentarovaVyjimka.class)
10.49 + public void xmlChybaChybíOdstavec() throws Exception {
10.50 + Komentare.zkontroluj("<div>xxx</div>");
10.51 + }
10.52 +
10.53 + @Test(expected = KomentarovaVyjimka.class)
10.54 + public void xmlChybaPřebýváAtribut() throws Exception {
10.55 + Komentare.zkontroluj("<div omfg='xxx'><p>xxx</p></div>");
10.56 + }
10.57 +}
11.1 --- a/java/nekurak.net-web/src/java/cz/frantovo/nekurak/web/OdeslatKomentar.java Tue Jun 08 10:42:58 2010 +0200
11.2 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/web/OdeslatKomentar.java Tue Jun 08 12:56:46 2010 +0200
11.3 @@ -1,6 +1,9 @@
11.4 package cz.frantovo.nekurak.web;
11.5
11.6 import cz.frantovo.nekurak.dto.Komentar;
11.7 +import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka;
11.8 +import cz.frantovo.nekurak.vyjimky.TexyVyjimka;
11.9 +import java.util.logging.Level;
11.10 import java.util.logging.Logger;
11.11
11.12 /**
11.13 @@ -11,8 +14,18 @@
11.14
11.15 private HledacSluzby hledac = new HledacSluzby();
11.16 private static final Logger log = Logger.getLogger(OdeslatKomentar.class.getSimpleName());
11.17 + private boolean ok = false;
11.18
11.19 - public void setKomentar(Komentar k) {
11.20 - hledac.getPodnikEJB().komentuj(k);
11.21 + public void setKomentar(Komentar k) throws TexyVyjimka {
11.22 + try {
11.23 + hledac.getPodnikEJB().komentuj(k);
11.24 + ok = true;
11.25 + } catch (KomentarovaVyjimka e) {
11.26 + log.log(Level.WARNING, "chyba při vkládání komentáře", e);
11.27 + }
11.28 + }
11.29 +
11.30 + public boolean isOk() {
11.31 + return ok;
11.32 }
11.33 }
12.1 --- a/java/nekurak.net-web/web/WEB-INF/casti/odeslatKomentar.jsp Tue Jun 08 10:42:58 2010 +0200
12.2 +++ b/java/nekurak.net-web/web/WEB-INF/casti/odeslatKomentar.jsp Tue Jun 08 12:56:46 2010 +0200
12.3 @@ -21,10 +21,19 @@
12.4 <jsp:setProperty name="komentar" property="typ" value="${param.typ}"/>
12.5 <jsp:setProperty name="odeslatKomentar" property="komentar" value="${komentar}"/>
12.6
12.7 - <p>
12.8 - <fmt:message key="komentar.odeslat.ok"/>,
12.9 - <a href="?akce=detail&amp;podnik=${komentar.podnik}"><fmt:message key="komentar.odeslat.pokracujte"/></a>.
12.10 - </p>
12.11 + <c:choose>
12.12 + <c:when test="${odeslatKomentar.ok}">
12.13 + <p>
12.14 + <fmt:message key="komentar.odeslat.ok"/>,
12.15 + <a href="?akce=detail&amp;podnik=${komentar.podnik}"><fmt:message key="komentar.odeslat.pokracujte"/></a>.
12.16 + </p>
12.17 + </c:when>
12.18 + <c:otherwise>
12.19 + <p><fmt:message key="komentar.odeslat.chyba"/></p>
12.20 + </c:otherwise>
12.21 + </c:choose>
12.22 +
12.23 +
12.24
12.25
12.26 </nk:stranka>
13.1 --- a/xml/komentář.xsd Tue Jun 08 10:42:58 2010 +0200
13.2 +++ b/xml/komentář.xsd Tue Jun 08 12:56:46 2010 +0200
13.3 @@ -14,7 +14,6 @@
13.4 </xs:complexType>
13.5 </xs:element>
13.6 </xs:choice>
13.7 -
13.8 </xs:complexType>
13.9 </xs:element>
13.10