Project

General

Profile

« Previous | Next » 

Revision 4424

Added by daigle almost 16 years ago

Move xml schema specific functionality to XMLSchemaService

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
82 82
import edu.ucsb.nceas.metacat.service.ServiceException;
83 83
import edu.ucsb.nceas.metacat.service.SessionService;
84 84
import edu.ucsb.nceas.metacat.service.SkinPropertyService;
85
import edu.ucsb.nceas.metacat.service.XMLSchemaService;
85 86
import edu.ucsb.nceas.metacat.spatial.SpatialHarvester;
86 87
import edu.ucsb.nceas.metacat.spatial.SpatialQuery;
87 88
import edu.ucsb.nceas.metacat.util.LDAPUtil;
......
158 159
    private static final String ERRORCLOSE = "</error>";
159 160
    public static final String SCHEMALOCATIONKEYWORD = ":schemaLocation";
160 161
    public static final String NONAMESPACELOCATION = ":noNamespaceSchemaLocation";
161
    public static final String NAMESPACEKEYWORD = "xmlns";
162 162
    public static final String EML2KEYWORD = ":eml";
163 163
    public static final String XMLFORMAT = "xml";
164 164
    private static final String CONFIG_DIR = "WEB-INF";
......
226 226
			// initialize DBConnection pool
227 227
			DBConnectionPool connPool = DBConnectionPool.getInstance();
228 228
			logMetacat.debug("DBConnection pool initialized: " + connPool.toString());
229
			
230
			XMLSchemaService xmlService = XMLSchemaService.getInstance();
231
			logMetacat.debug("XMLService initialized: " + xmlService.toString());
229 232

  
230 233
			// check if eml201 document were corrected or not. if not, correct
231 234
			// eml201 documents.
......
2119 2122
                    documentWrapper = new DocumentImplWrapper(rule, validate);
2120 2123
                } else {
2121 2124
                    
2122
                    String namespace = findNamespace(xml);
2125
                    String namespace = XMLSchemaService.findDocumentNamespace(xml);
2123 2126
                    
2124 2127
                    if (namespace != null) {
2125 2128
                        if (namespace.compareTo(DocumentImpl.EML2_0_0NAMESPACE) == 0
......
2323 2326
    
2324 2327
    // END OF INSERT/UPDATE SECTION
2325 2328
    
2326
    /* check if the xml string contains key words to specify schema loocation */
2327
    private String findNamespace(StringReader xml) throws IOException {
2328
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2329
        String namespace = null;
2330
        
2331
        String eml2_0_0NameSpace = DocumentImpl.EML2_0_0NAMESPACE;
2332
        String eml2_0_1NameSpace = DocumentImpl.EML2_0_1NAMESPACE;
2333
        String eml2_1_0NameSpace = DocumentImpl.EML2_1_0NAMESPACE;
2334
        
2335
        if (xml == null) {
2336
            logMetacat.debug("Validation for schema is "
2337
                    + namespace);
2338
            return namespace;
2339
        }
2340
        String targetLine = getSchemaLine(xml);
2341
        
2342
        if (targetLine != null) {
2343
            
2344
            // find if the root element has prefix
2345
            String prefix = getPrefix(targetLine);
2346
            logMetacat.info("prefix is:" + prefix);
2347
            int startIndex = 0;
2348
            
2349
            
2350
            if(prefix != null) {
2351
                // if prefix found then look for xmlns:prefix
2352
                // element to find the ns
2353
                String namespaceWithPrefix = NAMESPACEKEYWORD
2354
                        + ":" + prefix;
2355
                startIndex = targetLine.indexOf(namespaceWithPrefix);
2356
                logMetacat.debug("namespaceWithPrefix is:" + namespaceWithPrefix+":");
2357
                logMetacat.debug("startIndex is:" + startIndex);
2358
                
2359
            } else {
2360
                // if prefix not found then look for xmlns
2361
                // attribute to find the ns
2362
                startIndex = targetLine.indexOf(NAMESPACEKEYWORD);
2363
                logMetacat.debug("startIndex is:" + startIndex);
2364
            }
2365
            
2366
            int start = 1;
2367
            int end = 1;
2368
            String namespaceString = null;
2369
            int count = 0;
2370
            if (startIndex != -1) {
2371
                for (int i = startIndex; i < targetLine.length(); i++) {
2372
                    if (targetLine.charAt(i) == '"') {
2373
                        count++;
2374
                    }
2375
                    if (targetLine.charAt(i) == '"' && count == 1) {
2376
                        start = i;
2377
                    }
2378
                    if (targetLine.charAt(i) == '"' && count == 2) {
2379
                        end = i;
2380
                        break;
2381
                    }
2382
                }
2383
            }
2384
            // else: xmlns not found. namespace = null will be returned
2385
            
2386
            logMetacat.debug("targetLine is " + targetLine);
2387
            logMetacat.debug("start is " + end);
2388
            logMetacat.debug("end is " + end);
2389
            
2390
            if(start < end){
2391
                namespaceString = targetLine.substring(start + 1, end);
2392
                logMetacat.debug("namespaceString is " + namespaceString);
2393
            }
2394
            logMetacat.debug("namespace in xml is: "
2395
                    + namespaceString);
2396
            if(namespaceString != null){
2397
                if (namespaceString.indexOf(eml2_0_0NameSpace) != -1) {
2398
                    namespace = eml2_0_0NameSpace;
2399
                } else if (namespaceString.indexOf(eml2_0_1NameSpace) != -1) {
2400
                    namespace = eml2_0_1NameSpace;
2401
                } else if (namespaceString.indexOf(eml2_1_0NameSpace) != -1) {
2402
                    namespace = eml2_1_0NameSpace;
2403
                } else {
2404
                    namespace = namespaceString;
2405
                }
2406
            }
2407
        }
2408
        
2409
        logMetacat.debug("Validation for eml is " + namespace);
2410
        
2411
        return namespace;
2412
        
2413
    }
2414
    
2415
    private String getSchemaLine(StringReader xml) throws IOException {
2416
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2417
        // find the line
2418
        String secondLine = null;
2419
        int count = 0;
2420
        int endIndex = 0;
2421
        int startIndex = 0;
2422
        final int TARGETNUM = 1;
2423
        StringBuffer buffer = new StringBuffer();
2424
        boolean comment = false;
2425
        boolean processingInstruction = false;
2426
        char thirdPreviousCharacter = '?';
2427
        char secondPreviousCharacter = '?';
2428
        char previousCharacter = '?';
2429
        char currentCharacter = '?';
2430
        int tmp = xml.read();
2431
        while (tmp != -1) {
2432
            currentCharacter = (char)tmp;
2433
            //in a comment
2434
            if (currentCharacter == '-' && previousCharacter == '-'
2435
                    && secondPreviousCharacter == '!'
2436
                    && thirdPreviousCharacter == '<') {
2437
                comment = true;
2438
            }
2439
            //out of comment
2440
            if (comment && currentCharacter == '>' && previousCharacter == '-'
2441
                    && secondPreviousCharacter == '-') {
2442
                comment = false;
2443
            }
2444
            
2445
            //in a processingInstruction
2446
            if (currentCharacter == '?' && previousCharacter == '<') {
2447
                processingInstruction = true;
2448
            }
2449
            
2450
            //out of processingInstruction
2451
            if (processingInstruction && currentCharacter == '>'
2452
                    && previousCharacter == '?') {
2453
                processingInstruction = false;
2454
            }
2455
            
2456
            //this is not comment or a processingInstruction
2457
            if (currentCharacter != '!' && previousCharacter == '<'
2458
                    && !comment && !processingInstruction) {
2459
                count++;
2460
            }
2461
            
2462
            // get target line
2463
            if (count == TARGETNUM && currentCharacter != '>') {
2464
                buffer.append(currentCharacter);
2465
            }
2466
            if (count == TARGETNUM && currentCharacter == '>') {
2467
                break;
2468
            }
2469
            thirdPreviousCharacter = secondPreviousCharacter;
2470
            secondPreviousCharacter = previousCharacter;
2471
            previousCharacter = currentCharacter;
2472
            tmp = xml.read();
2473
        }
2474
        secondLine = buffer.toString();
2475
        logMetacat.debug("the second line string is: " + secondLine);
2476
        
2477
        xml.reset();
2478
        return secondLine;
2479
    }
2480
    
2481
    private String getPrefix(String schemaLine) {
2482
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2483
        String prefix = null;
2484
        
2485
        if(schemaLine.indexOf(" ") > 0){
2486
            String rootElement = "";
2487
            try {
2488
                rootElement = schemaLine.substring(0, schemaLine.indexOf(" "));
2489
            } catch (StringIndexOutOfBoundsException sioobe) {
2490
                rootElement = schemaLine;
2491
            }
2492
            
2493
            logMetacat.debug("rootElement:" + rootElement);
2494
            
2495
            if(rootElement.indexOf(":") > 0){
2496
                prefix = rootElement.substring(rootElement.indexOf(":") + 1,
2497
                        rootElement.length());
2498
            }
2499
            
2500
            if(prefix != null){
2501
                return prefix.trim();
2502
            }
2503
        }
2504
        return null;
2505
    }
2506
    
2507 2329
    /**
2508 2330
     * Handle the database delete request and delete an XML document from the
2509 2331
     * database connection

Also available in: Unified diff