34664e848188e3636f92171738018822e0135857
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / RobustDocumentType.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
20
21 import javax.xml.transform.dom.DOMSource;
22
23 import org.wamblee.xmlrouter.common.Id;
24 import org.wamblee.xmlrouter.config.DocumentType;
25
26 /**
27  * Robust document type provides robustness towards externally provided document
28  * types.
29  * 
30  * @author Erik Brakkee
31  * 
32  */
33 public class RobustDocumentType implements DocumentType {
34
35     private static final Logger LOGGER = Logger
36         .getLogger(RobustDocumentType.class.getName());
37
38     private Id<DocumentType> id;
39     private DocumentType type;
40
41     /**
42      * Constructs the wrapper.
43      * 
44      * @param aId
45      *            Id.
46      * @param aType
47      *            Document type to wrap.
48      */
49     public RobustDocumentType(Id<DocumentType> aId, DocumentType aType) {
50         id = aId;
51         type = aType;
52     }
53
54     @Override
55     public String getName() {
56         try {
57             String name = type.getName();
58             if (name == null) {
59                 LOGGER.log(Level.WARNING, "Document type " + id +
60                     " returned null for name");
61                 return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
62             }
63             return name;
64         } catch (Exception e) {
65             LOGGER.log(Level.WARNING, "Document type " + id +
66                 " threw exception", e);
67             return Constants.UNKNOWN_DOCUMENT_TYPE.toString();
68         }
69     }
70
71     @Override
72     public boolean isInstance(DOMSource aSource) {
73         try {
74             return type.isInstance(aSource);
75         } catch (Exception e) {
76             LOGGER.log(Level.WARNING, "Document type " + id +
77                 " threw exception", e);
78             return false;
79         }
80     }
81
82     @Override
83     public boolean validate(DOMSource aSource) {
84         try {
85             return type.validate(aSource);
86         } catch (Exception e) {
87             LOGGER.log(Level.WARNING, "Document type " + id +
88                 " threw exception", e);
89             return false;
90         }
91     }
92 }