Project

General

Profile

« Previous | Next » 

Revision 695

Added by bojilova almost 24 years ago

Included check up for <!DOCTYPE ... PUBLIC/SYSTEM ... >
in order to set the validation parser at runtime
In case of:
- no DOCTYPE declaration
- <!DOCTYPE docname>
validation is turned "off"
In case of:
- <!DOCTYPE ... PUBLIC ...>
- <!DOCTYPE ... SYSTEM ...>
validation is turned "on"

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
1170 1170
      }
1171 1171
      
1172 1172
      StringReader xml = null;
1173
      boolean validate = false;
1173 1174
      try {
1175
        // look inside XML Document for <!DOCTYPE ... PUBLIC/SYSTEM ... > 
1176
        // in order to decide whether to use validation parser
1177
        validate = validateXML(doctext[0]);
1174 1178
        xml = new StringReader(doctext[0]);
1175 1179

  
1176 1180
        String[] action = (String[])params.get("action");
......
1195 1199
              accNumber = null;
1196 1200
            }
1197 1201
            newdocid = DocumentImpl.write(conn, xml, pub, dtd, doAction,
1198
                                          accNumber, user, group);
1202
                                          accNumber, user, group, validate);
1199 1203
          } catch (NullPointerException npe) {
1200 1204
            newdocid = DocumentImpl.write(conn, xml, pub, dtd, doAction,
1201
                                          null, user, group);
1205
                                          null, user, group, validate);
1202 1206
          }
1203 1207
        } finally {
1204 1208
          util.returnConnection(conn);
......
1235 1239
  }
1236 1240

  
1237 1241
  /** 
1242
   * Parse XML Document to look for <!DOCTYPE ... PUBLIC/SYSTEM ... > 
1243
   * in order to decide whether to use validation parser
1244
   */
1245
  private static boolean validateXML(String xmltext) throws IOException {
1246
    
1247
    StringReader xmlreader = new StringReader(xmltext);
1248
    StringBuffer cbuff = new StringBuffer();
1249
    java.util.Stack st = new java.util.Stack();
1250
    boolean validate = false;
1251
    int c;
1252
    int inx;
1253
    
1254
    // read from the stream until find the keywords
1255
    while ( (st.empty() || st.size()<4) && ((c = xmlreader.read()) != -1) ) {
1256
      cbuff.append((char)c);
1257

  
1258
      // "<!DOCTYPE" keyword is found; put it in the stack
1259
      if ( (inx = cbuff.toString().indexOf("<!DOCTYPE")) != -1 ) {
1260
        cbuff = new StringBuffer();
1261
        st.push("<!DOCTYPE");
1262
      }
1263
      // "PUBLIC" keyword is found; put it in the stack
1264
      if ( (inx = cbuff.toString().indexOf("PUBLIC")) != -1 ) {
1265
        cbuff = new StringBuffer();
1266
        st.push("PUBLIC");
1267
      }
1268
      // "SYSTEM" keyword is found; put it in the stack
1269
      if ( (inx = cbuff.toString().indexOf("SYSTEM")) != -1 ) {
1270
        cbuff = new StringBuffer();
1271
        st.push("SYSTEM");
1272
      }
1273
      // ">" character is found; put it in the stack
1274
      // ">" is found twice: fisrt from <?xml ...?> 
1275
      // and second from <!DOCTYPE ... >
1276
      if ( (inx = cbuff.toString().indexOf(">")) != -1 ) {
1277
        cbuff = new StringBuffer();
1278
        st.push(">");
1279
      }
1280
    }
1281

  
1282
    // close the stream
1283
    xmlreader.close();
1284

  
1285
    // check the stack whether it contains the keywords:
1286
    // "<!DOCTYPE", "PUBLIC" or "SYSTEM", and ">" in this order
1287
    if ( st.size() == 4 ) {
1288
      if ( ((String)st.pop()).equals(">") &&
1289
           ( ((String)st.peek()).equals("PUBLIC") |
1290
             ((String)st.pop()).equals("SYSTEM") ) &&
1291
           ((String)st.pop()).equals("<!DOCTYPE") )  {
1292
        validate = true;
1293
      }
1294
    }
1295

  
1296
System.out.println("Validation is " + validate);
1297
    return validate;
1298
  }
1299

  
1300
  /** 
1238 1301
   * Handle the database delete request and delete an XML document 
1239 1302
   * from the database connection
1240 1303
   */
src/edu/ucsb/nceas/metacat/DocumentImpl.java
775 775
      dtd = new FileReader(new File(dtdfilename).toString());
776 776
    }
777 777
    return write ( conn, new FileReader(new File(filename).toString()),
778
                   pub, dtd, action, docid, user, group);
778
                   pub, dtd, action, docid, user, group, false);
779 779
  }
780 780

  
781 781
  public static String write(Connection conn,Reader xml,String pub,Reader dtd,
782 782
                             String action, String docid, String user,
783
                             String group )
783
                             String group, boolean validate)
784 784
                throws Exception {
785
    return write ( conn, xml, pub, dtd, action, docid, user, group, 1, false);
785
    return write(conn,xml,pub,dtd,action,docid,user,group,1,false,validate);
786 786
  }
787 787

  
788 788
  public static String write(Connection conn, Reader xml, String pub,
......
815 815
                              String group, int serverCode) 
816 816
                throws Exception
817 817
  {
818
    return write(conn,xml,pub,null,action,docid,user,group,serverCode,false);
818
    return write(conn,xml,pub,null,action,docid,user,group,
819
                 serverCode,false,false);
819 820
  }
820 821
  
821 822
  public static String write( Connection conn, Reader xml, String pub,
......
823 824
                              String group, int serverCode, boolean override)
824 825
                throws Exception
825 826
  {
826
    return write(conn,xml,pub,null,action,docid,user,group,serverCode,override);
827
    return write(conn,xml,pub,null,action,docid,user,group,
828
                 serverCode,override,false);
827 829
  }
828 830
  
829 831
  /**
......
848 850

  
849 851
  public static String write( Connection conn,Reader xml,String pub,Reader dtd,
850 852
                              String action, String docid, String user,
851
                              String group, int serverCode, boolean override)
853
                              String group, int serverCode, boolean override,
854
                              boolean validate)
852 855
                throws Exception
853 856
  {
854 857
    //MOVED DOWN IN RelationHandler obj before write of relations
......
890 893
        {
891 894
          MetacatReplication.replLog("lock granted for " + docid + " from " +
892 895
                                      server);
893
          XMLReader parser = initializeParser(conn, action, newdocid, 
896
          XMLReader parser = initializeParser(conn, action, newdocid, validate,
894 897
                                              user, group, pub, serverCode, dtd);
895 898
          conn.setAutoCommit(false);
896
          parser.parse(new InputSource(xml));
899
          parser.parse(new InputSource(xml)); 
897 900
          conn.commit();
898 901
          conn.setAutoCommit(true);
899 902
        } 
......
952 955

  
953 956
    try 
954 957
    { 
955
      XMLReader parser = initializeParser(conn, action, newdocid, 
958
      XMLReader parser = initializeParser(conn, action, newdocid, validate,
956 959
                                          user, group, pub, serverCode, dtd);
957 960
      conn.setAutoCommit(false);
958 961
      parser.parse(new InputSource(xml));
......
1056 1059
   * Set up the parser handlers for writing the document to the database
1057 1060
   */
1058 1061
  private static XMLReader initializeParser(Connection conn, String action,
1059
                                   String docid, String user, String group,
1060
                                   String pub, int serverCode, Reader dtd) 
1062
                                   String docid, boolean validate, 
1063
                                   String user, String group, String pub, 
1064
                                   int serverCode, Reader dtd) 
1061 1065
                           throws Exception 
1062 1066
  {
1063 1067
    XMLReader parser = null;
......
1077 1081
      parser = XMLReaderFactory.createXMLReader(parserName);
1078 1082

  
1079 1083
      // Turn on validation
1080
      parser.setFeature("http://xml.org/sax/features/validation", true);
1084
      parser.setFeature("http://xml.org/sax/features/validation", validate);
1081 1085
      // Turn off Including all external parameter entities
1082 1086
      // (the external DTD subset also)
1083 1087
      // Doesn't work well, probably the feature name is not correct

Also available in: Unified diff