src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/tools/TransformInputOutput.java

changeset 286
f50545b5e2f1
child 384
8f2986ff0235
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 *
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26 */
27
28 package com.sun.xml.internal.fastinfoset.tools;
29
30 import java.io.BufferedInputStream;
31 import java.io.BufferedOutputStream;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
39 import java.net.URI;
40 import java.net.URISyntaxException;
41 import org.xml.sax.EntityResolver;
42 import org.xml.sax.InputSource;
43 import org.xml.sax.SAXException;
44
45 public abstract class TransformInputOutput {
46
47 /** Creates a new instance of TransformInputOutput */
48 public TransformInputOutput() {
49 }
50
51 public void parse(String[] args) throws Exception {
52 InputStream in = null;
53 OutputStream out = null;
54 if (args.length == 0) {
55 in = new BufferedInputStream(System.in);
56 out = new BufferedOutputStream(System.out);
57 } else if (args.length == 1) {
58 in = new BufferedInputStream(new FileInputStream(args[0]));
59 out = new BufferedOutputStream(System.out);
60 } else if (args.length == 2) {
61 in = new BufferedInputStream(new FileInputStream(args[0]));
62 out = new BufferedOutputStream(new FileOutputStream(args[1]));
63 } else {
64 throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.optinalFileNotSpecified"));
65 }
66
67 parse(in, out);
68 }
69
70 abstract public void parse(InputStream in, OutputStream out) throws Exception;
71
72 // parse alternative with current working directory parameter
73 // is used to process xml documents, which have external imported entities
74 public void parse(InputStream in, OutputStream out, String workingDirectory) throws Exception {
75 throw new UnsupportedOperationException();
76 }
77
78 private static URI currentJavaWorkingDirectory;
79 static {
80 currentJavaWorkingDirectory = new File(System.getProperty("user.dir")).toURI();
81 }
82
83 protected static EntityResolver createRelativePathResolver(final String workingDirectory) {
84 return new EntityResolver() {
85 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
86 if (systemId != null && systemId.startsWith("file:/")) {
87 URI workingDirectoryURI = new File(workingDirectory).toURI();
88 URI workingFile;
89 try {
90 // Construction new File(new URI(String)).toURI() is used to be sure URI has correct representation without redundant '/'
91 workingFile = convertToNewWorkingDirectory(currentJavaWorkingDirectory, workingDirectoryURI, new File(new URI(systemId)).toURI());
92 return new InputSource(workingFile.toString());
93 } catch (URISyntaxException ex) {
94 //Should not get here
95 }
96 }
97 return null;
98 }
99 };
100 }
101
102 private static URI convertToNewWorkingDirectory(URI oldwd, URI newwd, URI file) throws IOException, URISyntaxException {
103 String oldwdStr = oldwd.toString();
104 String newwdStr = newwd.toString();
105 String fileStr = file.toString();
106
107 String cmpStr = null;
108 // In simpliest case <user.dir>/file.xml - do it faster
109 if (fileStr.startsWith(oldwdStr) && (cmpStr = fileStr.substring(oldwdStr.length())).indexOf('/') == -1) {
110 return new URI(newwdStr + '/' + cmpStr);
111 }
112
113 String[] oldwdSplit = oldwdStr.split("/");
114 String[] newwdSplit = newwdStr.split("/");
115 String[] fileSplit = fileStr.split("/");
116
117 int diff;
118 for(diff = 0; diff<oldwdSplit.length & diff<fileSplit.length; diff++) {
119 if (!oldwdSplit[diff].equals(fileSplit[diff])) {
120 break;
121 }
122 }
123
124 int diffNew;
125 for(diffNew=0; diffNew<newwdSplit.length && diffNew<fileSplit.length; diffNew++) {
126 if (!newwdSplit[diffNew].equals(fileSplit[diffNew])) {
127 break;
128 }
129 }
130
131 //Workaround for case, when extrnal imported entity has imports other entity
132 //in that case systemId has correct path, not based on user.dir
133 if (diffNew > diff) {
134 return file;
135 }
136
137 int elemsToSub = oldwdSplit.length - diff;
138 StringBuffer resultStr = new StringBuffer(100);
139 for(int i=0; i<newwdSplit.length - elemsToSub; i++) {
140 resultStr.append(newwdSplit[i]);
141 resultStr.append('/');
142 }
143
144 for(int i=diff; i<fileSplit.length; i++) {
145 resultStr.append(fileSplit[i]);
146 if (i < fileSplit.length - 1) resultStr.append('/');
147 }
148
149 return new URI(resultStr.toString());
150 }
151 }

mercurial