Project

General

Profile

« Previous | Next » 

Revision 1383

Added by Jing Tao almost 22 years ago

Make the paser knew namespace and schema validation.

View differences:

src/edu/ucsb/nceas/metacat/DocumentImpl.java
67 67
 */
68 68
public class DocumentImpl {
69 69

  
70
   /* Constents */
71
   public static final String SCHEMA                 = "schema";
72
   public static final String DTD                    = "dtd";
73
   public static final String EXTERNALSCHEMALOCATIONPROPERTY = 
74
              "http://apache.org/xml/properties/schema/external-schemaLocation";
75
   public static final String EXTERNALSCHEMALOCATION = 
76
     "eml://ecoinformatics.org/eml-2.0.0 http://dev.nceas.ucsb.edu/tao/schema/eml.xsd";
77
   public static final String DECLARATIONHANDLERPROPERTY =
78
                            "http://xml.org/sax/properties/declaration-handler";
79
   public static final String LEXICALPROPERTY =
80
                             "http://xml.org/sax/properties/lexical-handler";
81
   public static final String VALIDATIONFEATURE = 
82
                             "http://xml.org/sax/features/validation";
83
   public static final String SCHEMAVALIDATIONFEATURE = 
84
                             "http://apache.org/xml/features/validation/schema";
85
   public static final String NAMESPACEFEATURE = 
86
                              "http://xml.org/sax/features/namespaces";
87
   public static final String NAMESPACEPREFIXESFEATURE = 
88
                              "http://xml.org/sax/features/namespace-prefixes";
89
  
90
  
70 91
  static final int ALL = 1;
71 92
  static final int WRITE = 2;
72 93
  static final int READ = 4;
......
1377 1398
   * @param user the user that owns the document
1378 1399
   * @param groups the groups to which user belongs
1379 1400
   */
1380
  public static String write(DBConnection conn,String filename,
1401
  /*public static String write(DBConnection conn,String filename,
1381 1402
                             String pub, String dtdfilename,
1382 1403
                             String action, String docid, String user,
1383 1404
                             String[] groups )
......
1389 1410
    }
1390 1411
    return write ( conn, new FileReader(new File(filename).toString()),
1391 1412
                   pub, dtd, action, docid, user, groups, false);
1392
  }
1413
  }*/
1393 1414

  
1394 1415
  public static String write(DBConnection conn,Reader xml,String pub,Reader dtd,
1395 1416
                             String action, String docid, String user,
1396
                             String[] groups, boolean validate)
1417
                             String[] groups, String ruleBase, 
1418
                             boolean needValidation)
1397 1419
                throws Exception {
1398 1420
    //this method will be called in handleUpdateOrInsert method 
1399
    //in MetacatServlet class
1421
    //in MetacatServlet class and now is wrapper into documentImple
1400 1422
    // get server location for this doc
1401 1423
    int serverLocation=getServerLocationNumber(docid);
1402
    //System.out.println("server location: "+serverLocation);
1403 1424
    return write(conn,xml,pub,dtd,action,docid,user,groups,serverLocation,false,
1404
                 validate);
1425
                 ruleBase, needValidation);
1405 1426
  }
1406 1427

  
1407
  public static String write(DBConnection conn, Reader xml, String pub,
1408
                             String action, String docid, String user,
1409
                             String[] groups )
1410
                throws Exception {
1411
    if(action.equals("UPDATE"))
1412
    {//if the document is being updated then use the servercode from the 
1413
     //originally inserted document.
1414
      DocumentImpl doc = new DocumentImpl(docid);
1415
      int servercode = doc.getServerlocation();
1416
      return write(conn, xml, pub, action, docid, user, groups, servercode);
1417
    }
1418
    else
1419
    {//if the file is being inserted then the servercode is always 1
1420
      return write(conn, xml, pub, action, docid, user, groups, 1);
1421
    }
1422
  }
1428
 
1423 1429
  
1424
  public static String write(DBConnection conn, Reader xml,
1425
                              String action, String docid, String user,
1426
                              String[] groups, int serverCode )
1427
                throws Exception
1428
  {
1429
    return write(conn,xml,null,action,docid,user,groups,serverCode);
1430
  }
1431
  
1432
  public static String write(DBConnection conn, Reader xml, String pub,
1433
                              String action, String docid, String user,
1434
                              String[] groups, int serverCode) 
1435
                throws Exception
1436
  {
1437
    return write(conn,xml,pub,null,action,docid,user,groups,
1438
                 serverCode,false,false);
1439
  }
1440
  
1441
  public static String write(DBConnection conn, Reader xml, String pub,
1442
                              String action, String docid, String user,
1443
                              String[] groups, int serverCode, boolean override)
1444
                throws Exception
1445
  {
1446
    return write(conn,xml,pub,null,action,docid,user,groups,
1447
                 serverCode,override,false);
1448
  }
1449
  
1450 1430
  /**
1451 1431
   * Write an XML file to the database, given a Reader
1452 1432
   *
......
1470 1450
  public static String write(DBConnection conn, Reader xml,String pub,
1471 1451
                         Reader dtd, String action, String accnum, String user,
1472 1452
                         String[] groups, int serverCode, boolean override,
1473
                         boolean validate)
1453
                         String ruleBase, boolean needValidation)
1474 1454
                throws Exception
1475 1455
  {
1476 1456
    // NEW - WHEN CLIENT ALWAYS PROVIDE ACCESSION NUMBER INCLUDING REV IN IT
......
1482 1462
    String rev = ac.getRev();
1483 1463
    MetaCatUtil.debugMessage("action: " + action + " servercode: " + 
1484 1464
                             serverCode + " override: " + override, 10);
1485
                        
1465
                     
1486 1466
    if((serverCode != 1 && action.equals("UPDATE")) && !override)
1487 1467
    { //if this document being written is not a resident of this server then
1488 1468
      //we need to try to get a lock from it's resident server.  If the
......
1515 1495
          //System.out.println("In lockgranted");
1516 1496
          MetacatReplication.replLog("lock granted for " + accnum + " from " +
1517 1497
                                      server);
1498
          /*XMLReader parser = initializeParser(conn, action, docid, updaterev,
1499
                               validate, user, groups, pub, serverCode, dtd);*/
1518 1500
          XMLReader parser = initializeParser(conn, action, docid, updaterev,
1519
                                  validate, user, groups, pub, serverCode, dtd);
1501
                                        user, groups, pub, serverCode, 
1502
                                        dtd,ruleBase, needValidation); 
1520 1503
          conn.setAutoCommit(false);
1521 1504
          parser.parse(new InputSource(xml)); 
1522 1505
          conn.commit();
......
1574 1557
    try 
1575 1558
    { 
1576 1559
      
1577
      XMLReader parser = initializeParser(conn, action, docid, rev, validate,
1578
                                          user, groups, pub, serverCode, dtd);
1560
      XMLReader parser = initializeParser(conn, action, docid, rev, 
1561
                                          user, groups, pub, serverCode, 
1562
                                          dtd, ruleBase, needValidation);
1579 1563
   
1580 1564
      conn.setAutoCommit(false);
1581 1565
      parser.parse(new InputSource(xml));
......
1620 1604
   */
1621 1605

  
1622 1606
  public static String writeReplication(DBConnection conn, Reader xml, 
1623
            String pub, Reader dtd, String action, String accnum, String user,
1624
     String[] groups,String homeServer, boolean validate, String notifyServer)
1625
                throws Exception
1607
                                        String pub, Reader dtd, String action, 
1608
                                        String accnum, String user,
1609
                                        String[] groups,String homeServer, 
1610
                                        String notifyServer,
1611
                                        String ruleBase, boolean needValidation)
1612
                                        throws Exception
1626 1613
  {
1627 1614
    // Docid without revision
1628 1615
    String docid=MetaCatUtil.getDocIdFromString(accnum);
......
1686 1673
    try 
1687 1674
    { 
1688 1675
      
1689
      XMLReader parser = initializeParser(conn, action, docid, rev, validate,
1690
                                          user, groups, pub, serverCode, dtd);
1676
      XMLReader parser = initializeParser(conn, action, docid, rev,
1677
                                          user, groups, pub, serverCode, dtd,
1678
                                          ruleBase, needValidation);
1691 1679
      conn.setAutoCommit(false);
1692 1680
      parser.parse(new InputSource(xml));
1693 1681
      conn.commit();
......
1842 1830
    return AccessControlList.hasPermission("READ", user, groups, docId);
1843 1831
  }  
1844 1832

  
1833
  
1834
   /**
1835
   * Set up the parser handlers for writing the document to the database
1836
   */
1837
  private static XMLReader initializeParser(DBConnection dbconn, String action,
1838
                                            String docid, String rev, 
1839
                                            String user, 
1840
                                            String[] groups, String pub, 
1841
                                            int serverCode, Reader dtd,
1842
                                            String ruleBase, 
1843
                                            boolean needValidation) 
1844
                                            throws Exception 
1845
  {
1846
    XMLReader parser = null;
1847
    try 
1848
    {
1849
 
1850
      //create a DBSAXHandler object which has the revision specification
1851
      ContentHandler chandler = new DBSAXHandler(dbconn, action, 
1852
                                    docid, rev, user, groups, pub, serverCode);
1853
      EntityResolver eresolver;
1854
      DTDHandler dtdhandler;  
1855
    
1856
      // Get an instance of the parser
1857
      String parserName = MetaCatUtil.getOption("saxparser");
1858
      parser = XMLReaderFactory.createXMLReader(parserName);
1859
      parser.setContentHandler((ContentHandler)chandler);
1860
      parser.setErrorHandler((ErrorHandler)chandler);
1861
      parser.setProperty(DECLARATIONHANDLERPROPERTY, chandler);
1862
      parser.setProperty(LEXICALPROPERTY, chandler);
1863
      
1864
      if (ruleBase != null && ruleBase.equals(SCHEMA) && needValidation)
1865
      {
1866
        // turn on schema validation feature
1867
        parser.setFeature(VALIDATIONFEATURE, true);
1868
        parser.setFeature(NAMESPACEFEATURE, true);
1869
        parser.setFeature(NAMESPACEPREFIXESFEATURE, true);
1870
        parser.setFeature(SCHEMAVALIDATIONFEATURE, true);
1871
        parser.setProperty(EXTERNALSCHEMALOCATIONPROPERTY,
1872
                           EXTERNALSCHEMALOCATION);
1873
     
1874
      }
1875
      else if (ruleBase != null && ruleBase.equals(DTD) && needValidation)
1876
      {
1877
        // turn on dtd validaton feature
1878
        parser.setFeature(VALIDATIONFEATURE, true);
1879
        eresolver= new DBEntityResolver(dbconn, (DBSAXHandler)chandler, dtd);
1880
        dtdhandler = new DBDTDHandler(dbconn);
1881
        parser.setEntityResolver((EntityResolver)eresolver);
1882
        parser.setDTDHandler((DTDHandler)dtdhandler);
1883
      }
1884
      else
1885
      {
1886
        // non validation
1887
         parser.setFeature(VALIDATIONFEATURE, false);
1888
         eresolver= new DBEntityResolver(dbconn, (DBSAXHandler)chandler, dtd);
1889
         dtdhandler = new DBDTDHandler(dbconn);
1890
         parser.setEntityResolver((EntityResolver)eresolver);
1891
         parser.setDTDHandler((DTDHandler)dtdhandler);
1892
      }
1893
  
1894
    } 
1895
    catch (Exception e) 
1896
    {
1897
      throw e;
1898
    }
1899
    return parser;
1900
  }
1901

  
1902
  
1845 1903
  /**
1846 1904
   * Set up the parser handlers for writing the document to the database
1847 1905
   */
1848
  private static XMLReader initializeParser(DBConnection dbconn, String action,
1906
  /*private static XMLReader initializeParser(DBConnection dbconn, String action,
1849 1907
                               String docid, String rev, boolean validate, 
1850 1908
                                   String user, String[] groups, String pub, 
1851 1909
                                   int serverCode, Reader dtd) 
......
1859 1917
    //
1860 1918
    try {
1861 1919
       //check out DBConnection
1862
      /*conn=DBConnectionPool.
1863
                    getDBConnection("DocumentImpl.initializeParse");
1864
      serialNumber=conn.getCheckOutSerialNumber();*/
1865
      
1920
     
1866 1921
      //create a DBSAXHandler object which has the revision specification
1867 1922
      ContentHandler chandler = new DBSAXHandler(dbconn, action, 
1868 1923
                                    docid, rev, user, groups, pub, serverCode);
......
1901 1956
    //}
1902 1957

  
1903 1958
    return parser;
1904
  }
1959
  }*/
1905 1960

  
1906 1961
  /**
1907 1962
   * Save a document entry in the xml_revisions table 
......
2450 2505
        DocumentImpl.delete(docid, null, null);
2451 2506
        System.out.println("Document deleted: " + docid);
2452 2507
      } else {
2453
        String newdocid = DocumentImpl.write(dbconn, filename, null,
2508
        /*String newdocid = DocumentImpl.write(dbconn, filename, null,
2454 2509
                                             dtdfilename, action, docid,
2455 2510
                                             null, null);
2456 2511
        if ((docid != null) && (!docid.equals(newdocid))) {
......
2463 2518
          System.out.println("ERROR: Couldn't update document!!! ");
2464 2519
        }
2465 2520
        System.out.println("Document processing finished for: " + filename
2466
              + " (" + newdocid + ")");
2521
              + " (" + newdocid + ")");*/
2467 2522
      }
2468 2523

  
2469 2524
      double stopTime = System.currentTimeMillis();

Also available in: Unified diff