Advertisement
Guest User

server

a guest
Jul 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4. import java.nio.file.*;
  5. import javax.xml.*;
  6. import javax.xml.parsers.*;
  7. import javax.xml.transform.stream.*;
  8. import javax.xml.validation.*;
  9. import org.xml.sax.*;
  10.  
  11. /*
  12.  * To change this license header, choose License Headers in Project Properties.
  13.  * To change this template file, choose Tools | Templates
  14.  * and open the template in the editor.
  15.  */
  16.  
  17. /**
  18.  *
  19.  * @author salvik
  20.  */
  21. public class ServerLogXMLAttivita {
  22.  
  23.     /**
  24.      * @param args the command line arguments
  25.      */
  26.     public static void main(String[] args) {
  27.         try(ServerSocket servs = new ServerSocket(8080);){
  28.             while(true){
  29.                 try(Socket so = servs.accept();
  30.                     DataInputStream din = new DataInputStream(so.getInputStream());){
  31.                     String xml = din.readUTF();
  32.                     validaRigaDiLog(xml);
  33.                 }
  34.             }
  35.         }catch (IOException ex) {
  36.             ex.printStackTrace();
  37.         }
  38.     }
  39.    
  40.    
  41.     public static void validaRigaDiLog(String xml) {
  42.         try {
  43.             DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  44.             SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  45.             Schema s = sf.newSchema(new StreamSource("./messaggioDiLog.xsd"));
  46.             s.newValidator().validate(new StreamSource(new StringReader(xml)));
  47.             Files.write(Paths.get("fileDiLog.txt"), xml.getBytes(), StandardOpenOption.APPEND);
  48.             System.out.println("Messaggio di Log registrato.");
  49.         } catch (Exception ex) {
  50.             if(ex instanceof SAXException)
  51.                 System.out.println("Errore di validazione: " + ex.getMessage());
  52.             else
  53.                 System.out.println(ex.getMessage());
  54.         }
  55.     }
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement