java/nekurak.net-lib/src/cz/frantovo/nekurak/util/Komentare.java
changeset 132 1ca0d7fdbe51
parent 131 0d1cba59734b
child 133 2acdbc74bc24
     1.1 --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/util/Komentare.java	Tue Jun 08 10:42:58 2010 +0200
     1.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/util/Komentare.java	Tue Jun 08 12:56:46 2010 +0200
     1.3 @@ -1,11 +1,28 @@
     1.4  package cz.frantovo.nekurak.util;
     1.5  
     1.6 +import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka;
     1.7 +import java.io.ByteArrayInputStream;
     1.8 +import java.net.URL;
     1.9 +import java.util.logging.Level;
    1.10 +import java.util.logging.Logger;
    1.11 +import javax.xml.XMLConstants;
    1.12 +import javax.xml.parsers.DocumentBuilder;
    1.13 +import javax.xml.parsers.DocumentBuilderFactory;
    1.14 +import javax.xml.validation.Schema;
    1.15 +import javax.xml.validation.SchemaFactory;
    1.16 +import org.w3c.dom.Document;
    1.17 +import org.xml.sax.ErrorHandler;
    1.18 +import org.xml.sax.SAXException;
    1.19 +import org.xml.sax.SAXParseException;
    1.20 +
    1.21  /**
    1.22   * Validátor komentářů
    1.23   * @author fiki
    1.24   */
    1.25  public class Komentare {
    1.26  
    1.27 +    private static final Logger log = Logger.getLogger(Komentare.class.getSimpleName());
    1.28 +
    1.29      /**
    1.30       * Escapuje XML a doplní XHTML zalomení na konce řádků.
    1.31       * @param komentar prostý text zadaný uživatelem
    1.32 @@ -30,8 +47,42 @@
    1.33       * @param komentar
    1.34       * @return jestli komentář odpovídá
    1.35       */
    1.36 -    public boolean isValidniXHTML(String komentar) {
    1.37 -	return false;
    1.38 +    public static Document zkontroluj(String komentar) throws KomentarovaVyjimka {
    1.39 +
    1.40 +
    1.41 +
    1.42 +
    1.43 +	try {
    1.44 +	    URL soubor = ClassLoader.getSystemResource("cz/frantovo/nekurak/util/komentář.xsd");
    1.45 +	    SchemaFactory tovarnaSchemat = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    1.46 +	    Schema schema = tovarnaSchemat.newSchema(soubor);
    1.47 +
    1.48 +	    DocumentBuilderFactory tovarnaDB = DocumentBuilderFactory.newInstance();
    1.49 +	    tovarnaDB.setSchema(schema);
    1.50 +
    1.51 +	    DocumentBuilder db = tovarnaDB.newDocumentBuilder();
    1.52 +	    db.setErrorHandler(new ErrorHandler() {
    1.53 +
    1.54 +		public void warning(SAXParseException e) throws SAXException {
    1.55 +		    throw e;
    1.56 +		}
    1.57 +
    1.58 +		public void error(SAXParseException e) throws SAXException {
    1.59 +		    throw e;
    1.60 +		}
    1.61 +
    1.62 +		public void fatalError(SAXParseException e) throws SAXException {
    1.63 +		    throw e;
    1.64 +		}
    1.65 +	    });
    1.66 +	    Document dokument = db.parse(new ByteArrayInputStream(komentar.getBytes("UTF-8")));
    1.67 +
    1.68 +	    return dokument;
    1.69 +	} catch (Exception e) {
    1.70 +	    throw new KomentarovaVyjimka("Neplatný komentář: " + komentar, e);
    1.71 +	}
    1.72 +
    1.73 +
    1.74      }
    1.75  
    1.76      private static String escapujXML(String str) {