KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > xml > XPSSourceInformation


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
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  */

17
18 /* $Id: XPSSourceInformation.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.xml;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 import org.apache.log4j.Category;
29
30 public class XPSSourceInformation {
31     static Category log = Category.getInstance(XPSSourceInformation.class);
32     public int lineNumber = -1;
33     public URL JavaDoc url = null;
34     public XPSSourceInformation parentInfo = null;
35     public Vector JavaDoc children = null;
36     String JavaDoc offset = null;
37     String JavaDoc cocoon = null;
38
39     /**
40      * Creates a new XPSSourceInformation object.
41      *
42      * @param fileURL DOCUMENT ME!
43      * @param cocoon DOCUMENT ME!
44      */

45     public XPSSourceInformation(String JavaDoc fileURL, String JavaDoc cocoon) {
46         this.cocoon = cocoon;
47
48         parentInfo = null;
49         offset = "++";
50         children = new Vector JavaDoc();
51
52         try {
53             url = new URL JavaDoc(fileURL);
54         } catch (MalformedURLException JavaDoc e) {
55             log.error(e);
56         }
57     }
58
59     /**
60      * Creates a new XPSSourceInformation object.
61      *
62      * @param urlString DOCUMENT ME!
63      * @param parentInfo DOCUMENT ME!
64      * @param cocoon DOCUMENT ME!
65      */

66     public XPSSourceInformation(String JavaDoc urlString, XPSSourceInformation parentInfo, String JavaDoc cocoon) {
67         this.cocoon = cocoon;
68
69         this.parentInfo = parentInfo;
70         offset = "++";
71         children = new Vector JavaDoc();
72
73         try {
74             if (urlString.indexOf("/") == 0) {
75                 url = new URL JavaDoc("file:" + urlString);
76             } else if (urlString.indexOf("cocoon:") == 0) {
77                 if (cocoon != null) {
78                     url = new URL JavaDoc(cocoon + "/" + urlString.substring(7)); // remove "cocoon:" protocol
79
log.warn("Protocol 7789: COCOON (" + urlString +
80                         ") -- will be transformed into http: " + url);
81                 } else {
82                     log.error("No cocoon base set!");
83                 }
84             } else {
85                 url = new URL JavaDoc(urlString);
86             }
87
88             String JavaDoc p = url.getProtocol();
89
90             // Does not make sense, because it will be either file or http, and else an Exception will be thrown!
91
if (!(p.equals("http") || p.equals("file") || p.equals("class"))) {
92                 log.error("This type of protocol is not supported yet: " + p);
93             }
94         } catch (MalformedURLException JavaDoc e) // let's hope it's a relative path
95
{
96             log.debug("1079: " + e + " -- Let's hope it's a relative path!");
97
98             File JavaDoc parent = new File JavaDoc(parentInfo.url.getFile());
99
100             // transform URI to system-independent path and create absolute path
101
try {
102                 int index = urlString.indexOf("#");
103                 String JavaDoc xpointer = "";
104                 String JavaDoc relativePath = urlString;
105
106                 if (index != -1) {
107                     relativePath = urlString.substring(0, index);
108                     xpointer = urlString.substring(index);
109                 }
110
111                 relativePath = relativePath.replace('/', File.separatorChar);
112
113                 File JavaDoc file = new File JavaDoc(parent.getParentFile(), relativePath);
114
115                 url = new URL JavaDoc("file", null, -1, file.getCanonicalPath() + xpointer);
116
117                 log.info("Concatenated URL: " + url);
118             } catch (MalformedURLException JavaDoc exception) {
119                 log.error(exception);
120             } catch (IOException JavaDoc exception) {
121                 log.error(exception);
122             }
123         }
124
125         if (url.getProtocol().equals("http")) {
126         } else if (url.getProtocol().equals("file")) {
127             if (parentInfo.url.getProtocol().equals("http")) {
128                 String JavaDoc protocol = parentInfo.url.getProtocol();
129                 String JavaDoc host = parentInfo.url.getHost();
130                 int port = parentInfo.url.getPort();
131
132                 try {
133                     if (url.getRef() != null) {
134                         url = new URL JavaDoc(protocol, host, port, url.getFile() + "#" + url.getRef());
135                     } else {
136                         url = new URL JavaDoc(protocol, host, port, url.getFile());
137                     }
138                 } catch (MalformedURLException JavaDoc e) {
139                     log.error(e);
140                 }
141             }
142         } else {
143             log.error("EXCEPTION: 0.2.21");
144         }
145     }
146
147     /**
148      * DOCUMENT ME!
149      *
150      * @param args DOCUMENT ME!
151      */

152     public static void main(String JavaDoc[] args) {
153         String JavaDoc cocoon = null;
154         XPSSourceInformation xpssf = new XPSSourceInformation(args[0], cocoon);
155         System.out.println(xpssf);
156     }
157
158     /**
159      * DOCUMENT ME!
160      *
161      * @param child DOCUMENT ME!
162      */

163     public void addChild(XPSSourceInformation child) {
164         children.addElement(child);
165     }
166
167     /**
168      * DOCUMENT ME!
169      *
170      * @return DOCUMENT ME!
171      */

172     public String JavaDoc toString() {
173         return toString("", offset);
174     }
175
176     /**
177      * DOCUMENT ME!
178      *
179      * @param index DOCUMENT ME!
180      * @param offset DOCUMENT ME!
181      *
182      * @return DOCUMENT ME!
183      */

184     public String JavaDoc toString(String JavaDoc index, String JavaDoc offset) {
185         String JavaDoc s = index + url.toString() + "\n";
186
187         for (int i = 0; i < children.size(); i++) {
188             XPSSourceInformation child = (XPSSourceInformation) children.elementAt(i);
189             s = s + child.toString(index + offset, offset);
190         }
191
192         return s;
193     }
194
195     /**
196      * Check for XInclude Loops
197      *
198      * @param xpssf DOCUMENT ME!
199      * @param url DOCUMENT ME!
200      *
201      * @return DOCUMENT ME!
202      */

203     public boolean checkLoop(XPSSourceInformation xpssf, URL JavaDoc url) {
204         if (xpssf.url.getFile().equals(url.getFile())) {
205             if (xpssf.parentInfo != null) {
206                 return true;
207             } else {
208                 return false; // This is just the request (it can be dummy.xml but also something real)
209
}
210         }
211
212         if (xpssf.parentInfo != null) {
213             return checkLoop(xpssf.parentInfo, url);
214         }
215
216         return false;
217     }
218 }
219
Popular Tags