author | František Kučera <franta-hg@frantovo.cz> |
Tue, 08 Jun 2010 12:56:46 +0200 | |
changeset 132 | 1ca0d7fdbe51 |
parent 131 | 0d1cba59734b |
child 133 | 2acdbc74bc24 |
permissions | -rw-r--r-- |
franta-hg@130 | 1 |
package cz.frantovo.nekurak.util; |
franta-hg@130 | 2 |
|
franta-hg@132 | 3 |
import cz.frantovo.nekurak.vyjimky.KomentarovaVyjimka; |
franta-hg@132 | 4 |
import java.io.ByteArrayInputStream; |
franta-hg@132 | 5 |
import java.net.URL; |
franta-hg@132 | 6 |
import java.util.logging.Level; |
franta-hg@132 | 7 |
import java.util.logging.Logger; |
franta-hg@132 | 8 |
import javax.xml.XMLConstants; |
franta-hg@132 | 9 |
import javax.xml.parsers.DocumentBuilder; |
franta-hg@132 | 10 |
import javax.xml.parsers.DocumentBuilderFactory; |
franta-hg@132 | 11 |
import javax.xml.validation.Schema; |
franta-hg@132 | 12 |
import javax.xml.validation.SchemaFactory; |
franta-hg@132 | 13 |
import org.w3c.dom.Document; |
franta-hg@132 | 14 |
import org.xml.sax.ErrorHandler; |
franta-hg@132 | 15 |
import org.xml.sax.SAXException; |
franta-hg@132 | 16 |
import org.xml.sax.SAXParseException; |
franta-hg@132 | 17 |
|
franta-hg@130 | 18 |
/** |
franta-hg@130 | 19 |
* Validátor komentářů |
franta-hg@130 | 20 |
* @author fiki |
franta-hg@130 | 21 |
*/ |
franta-hg@130 | 22 |
public class Komentare { |
franta-hg@130 | 23 |
|
franta-hg@132 | 24 |
private static final Logger log = Logger.getLogger(Komentare.class.getSimpleName()); |
franta-hg@132 | 25 |
|
franta-hg@130 | 26 |
/** |
franta-hg@130 | 27 |
* Escapuje XML a doplní XHTML zalomení na konce řádků. |
franta-hg@130 | 28 |
* @param komentar prostý text zadaný uživatelem |
franta-hg@130 | 29 |
* @return XHTML komentář bez kořenového elementu |
franta-hg@130 | 30 |
*/ |
franta-hg@130 | 31 |
public static String upravProstyText(String komentar) { |
franta-hg@130 | 32 |
komentar = escapujXML(komentar); |
franta-hg@130 | 33 |
return "<p>" + komentar.replaceAll("\n", "<br/>") + "</p>"; |
franta-hg@130 | 34 |
} |
franta-hg@130 | 35 |
|
franta-hg@130 | 36 |
/** |
franta-hg@130 | 37 |
* |
franta-hg@130 | 38 |
* @param komentar vstupní XHTML |
franta-hg@130 | 39 |
* @return XHTML obalené kořenovým elementem |
franta-hg@130 | 40 |
*/ |
franta-hg@130 | 41 |
public static String obal(String komentar) { |
franta-hg@130 | 42 |
return "<div>" + komentar + "</div>"; |
franta-hg@130 | 43 |
} |
franta-hg@130 | 44 |
|
franta-hg@131 | 45 |
/** |
franta-hg@131 | 46 |
* zkontroluje komentář oproti schématu |
franta-hg@131 | 47 |
* @param komentar |
franta-hg@131 | 48 |
* @return jestli komentář odpovídá |
franta-hg@131 | 49 |
*/ |
franta-hg@132 | 50 |
public static Document zkontroluj(String komentar) throws KomentarovaVyjimka { |
franta-hg@132 | 51 |
|
franta-hg@132 | 52 |
|
franta-hg@132 | 53 |
|
franta-hg@132 | 54 |
|
franta-hg@132 | 55 |
try { |
franta-hg@132 | 56 |
URL soubor = ClassLoader.getSystemResource("cz/frantovo/nekurak/util/komentář.xsd"); |
franta-hg@132 | 57 |
SchemaFactory tovarnaSchemat = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
franta-hg@132 | 58 |
Schema schema = tovarnaSchemat.newSchema(soubor); |
franta-hg@132 | 59 |
|
franta-hg@132 | 60 |
DocumentBuilderFactory tovarnaDB = DocumentBuilderFactory.newInstance(); |
franta-hg@132 | 61 |
tovarnaDB.setSchema(schema); |
franta-hg@132 | 62 |
|
franta-hg@132 | 63 |
DocumentBuilder db = tovarnaDB.newDocumentBuilder(); |
franta-hg@132 | 64 |
db.setErrorHandler(new ErrorHandler() { |
franta-hg@132 | 65 |
|
franta-hg@132 | 66 |
public void warning(SAXParseException e) throws SAXException { |
franta-hg@132 | 67 |
throw e; |
franta-hg@132 | 68 |
} |
franta-hg@132 | 69 |
|
franta-hg@132 | 70 |
public void error(SAXParseException e) throws SAXException { |
franta-hg@132 | 71 |
throw e; |
franta-hg@132 | 72 |
} |
franta-hg@132 | 73 |
|
franta-hg@132 | 74 |
public void fatalError(SAXParseException e) throws SAXException { |
franta-hg@132 | 75 |
throw e; |
franta-hg@132 | 76 |
} |
franta-hg@132 | 77 |
}); |
franta-hg@132 | 78 |
Document dokument = db.parse(new ByteArrayInputStream(komentar.getBytes("UTF-8"))); |
franta-hg@132 | 79 |
|
franta-hg@132 | 80 |
return dokument; |
franta-hg@132 | 81 |
} catch (Exception e) { |
franta-hg@132 | 82 |
throw new KomentarovaVyjimka("Neplatný komentář: " + komentar, e); |
franta-hg@132 | 83 |
} |
franta-hg@132 | 84 |
|
franta-hg@132 | 85 |
|
franta-hg@131 | 86 |
} |
franta-hg@131 | 87 |
|
franta-hg@130 | 88 |
private static String escapujXML(String str) { |
franta-hg@130 | 89 |
return str.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'"); |
franta-hg@130 | 90 |
|
franta-hg@130 | 91 |
} |
franta-hg@130 | 92 |
} |