KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > metadata > URLRegExpMapping


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: URLRegExpMapping.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.metadata;
25
26 import gnu.regexp.RE;
27 import gnu.regexp.REException;
28 import gnu.regexp.RESyntax;
29
30 import org.enhydra.xml.xmlc.XMLCException;
31 import org.w3c.dom.Document JavaDoc;
32
33 /**
34  * Specifies the regular expression replacement of URLs in element attributes.
35  */

36 public class URLRegExpMapping extends URLEdit {
37     /**
38      * Element name.
39      */

40     public static final String JavaDoc TAG_NAME = "urlRegExpMapping";
41
42     /**
43      * Compiled regular expression.
44      */

45     private RE regExp;
46
47     /**
48      * Cached subtitution attribute.
49      */

50     private String JavaDoc substitution;
51
52     /**
53      * Constructor.
54      */

55     public URLRegExpMapping(Document JavaDoc ownerDoc) {
56         super(ownerDoc, TAG_NAME);
57     }
58
59     /**
60      * Attribute names.
61      */

62     private static final String JavaDoc REGEXP_ATTR = "regexp";
63     private static final String JavaDoc SUBST_ATTR = "subst";
64
65     /**
66      * Get the regexp attribute value.
67      */

68     public String JavaDoc getRegexp() {
69         return getAttributeNull(REGEXP_ATTR);
70     }
71
72     /**
73      * Set the regexp attribute value.
74      */

75     public void setRegexp(String JavaDoc value) {
76         setRemoveAttribute(REGEXP_ATTR, value);
77     }
78
79     /**
80      * Get the subst attribute value.
81      */

82     public String JavaDoc getSubst() {
83         return getAttributeNull(SUBST_ATTR);
84     }
85
86     /**
87      * Set the subst attribute value.
88      */

89     public void setSubst(String JavaDoc value) {
90         setRemoveAttribute(SUBST_ATTR, value);
91     }
92
93     /**
94      * Complete modifications to DOM, compiles regular expression if set.
95      * @see MetaDataElement#completeModifications
96      */

97     protected void completeModifications() throws XMLCException {
98         super.completeModifications();
99         substitution = getSubst();
100
101         String JavaDoc regExpStr = getRegexp();
102         if (regExpStr == null) {
103             regExp = null;
104         } else {
105             try {
106                 regExp = new RE(regExpStr, 0,
107                                 RESyntax.RE_SYNTAX_POSIX_EXTENDED);
108             } catch (REException except) {
109                 throw new XMLCException("invalid URL mapping regular expression: "
110                                         + except.toString(),
111                                         except);
112             }
113             
114         }
115     }
116
117     /**
118      * Substitute a URL based on the regular expression.
119      *
120      * @return The updated URL or null if the URL doesn't match.
121      */

122     public String JavaDoc mapURL(String JavaDoc url) throws XMLCException {
123         if (regExp.getMatch(url) == null) {
124             return null;
125         }
126         return regExp.substituteAll(url, substitution);
127     }
128
129 }
130
Popular Tags