1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/tools/TransformInputOutput.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,151 @@ 1.4 +/* 1.5 + * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + * 1.28 + * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. 1.29 + */ 1.30 + 1.31 +package com.sun.xml.internal.fastinfoset.tools; 1.32 + 1.33 +import java.io.BufferedInputStream; 1.34 +import java.io.BufferedOutputStream; 1.35 +import java.io.File; 1.36 +import java.io.FileInputStream; 1.37 +import java.io.FileOutputStream; 1.38 +import java.io.IOException; 1.39 +import java.io.InputStream; 1.40 +import java.io.OutputStream; 1.41 +import com.sun.xml.internal.fastinfoset.CommonResourceBundle; 1.42 +import java.net.URI; 1.43 +import java.net.URISyntaxException; 1.44 +import org.xml.sax.EntityResolver; 1.45 +import org.xml.sax.InputSource; 1.46 +import org.xml.sax.SAXException; 1.47 + 1.48 +public abstract class TransformInputOutput { 1.49 + 1.50 + /** Creates a new instance of TransformInputOutput */ 1.51 + public TransformInputOutput() { 1.52 + } 1.53 + 1.54 + public void parse(String[] args) throws Exception { 1.55 + InputStream in = null; 1.56 + OutputStream out = null; 1.57 + if (args.length == 0) { 1.58 + in = new BufferedInputStream(System.in); 1.59 + out = new BufferedOutputStream(System.out); 1.60 + } else if (args.length == 1) { 1.61 + in = new BufferedInputStream(new FileInputStream(args[0])); 1.62 + out = new BufferedOutputStream(System.out); 1.63 + } else if (args.length == 2) { 1.64 + in = new BufferedInputStream(new FileInputStream(args[0])); 1.65 + out = new BufferedOutputStream(new FileOutputStream(args[1])); 1.66 + } else { 1.67 + throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.optinalFileNotSpecified")); 1.68 + } 1.69 + 1.70 + parse(in, out); 1.71 + } 1.72 + 1.73 + abstract public void parse(InputStream in, OutputStream out) throws Exception; 1.74 + 1.75 + // parse alternative with current working directory parameter 1.76 + // is used to process xml documents, which have external imported entities 1.77 + public void parse(InputStream in, OutputStream out, String workingDirectory) throws Exception { 1.78 + throw new UnsupportedOperationException(); 1.79 + } 1.80 + 1.81 + private static URI currentJavaWorkingDirectory; 1.82 + static { 1.83 + currentJavaWorkingDirectory = new File(System.getProperty("user.dir")).toURI(); 1.84 + } 1.85 + 1.86 + protected static EntityResolver createRelativePathResolver(final String workingDirectory) { 1.87 + return new EntityResolver() { 1.88 + public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { 1.89 + if (systemId != null && systemId.startsWith("file:/")) { 1.90 + URI workingDirectoryURI = new File(workingDirectory).toURI(); 1.91 + URI workingFile; 1.92 + try { 1.93 + // Construction new File(new URI(String)).toURI() is used to be sure URI has correct representation without redundant '/' 1.94 + workingFile = convertToNewWorkingDirectory(currentJavaWorkingDirectory, workingDirectoryURI, new File(new URI(systemId)).toURI()); 1.95 + return new InputSource(workingFile.toString()); 1.96 + } catch (URISyntaxException ex) { 1.97 + //Should not get here 1.98 + } 1.99 + } 1.100 + return null; 1.101 + } 1.102 + }; 1.103 + } 1.104 + 1.105 + private static URI convertToNewWorkingDirectory(URI oldwd, URI newwd, URI file) throws IOException, URISyntaxException { 1.106 + String oldwdStr = oldwd.toString(); 1.107 + String newwdStr = newwd.toString(); 1.108 + String fileStr = file.toString(); 1.109 + 1.110 + String cmpStr = null; 1.111 + // In simpliest case <user.dir>/file.xml - do it faster 1.112 + if (fileStr.startsWith(oldwdStr) && (cmpStr = fileStr.substring(oldwdStr.length())).indexOf('/') == -1) { 1.113 + return new URI(newwdStr + '/' + cmpStr); 1.114 + } 1.115 + 1.116 + String[] oldwdSplit = oldwdStr.split("/"); 1.117 + String[] newwdSplit = newwdStr.split("/"); 1.118 + String[] fileSplit = fileStr.split("/"); 1.119 + 1.120 + int diff; 1.121 + for (diff = 0; diff < oldwdSplit.length && diff < fileSplit.length; diff++) { 1.122 + if (!oldwdSplit[diff].equals(fileSplit[diff])) { 1.123 + break; 1.124 + } 1.125 + } 1.126 + 1.127 + int diffNew; 1.128 + for(diffNew=0; diffNew<newwdSplit.length && diffNew<fileSplit.length; diffNew++) { 1.129 + if (!newwdSplit[diffNew].equals(fileSplit[diffNew])) { 1.130 + break; 1.131 + } 1.132 + } 1.133 + 1.134 + //Workaround for case, when extrnal imported entity has imports other entity 1.135 + //in that case systemId has correct path, not based on user.dir 1.136 + if (diffNew > diff) { 1.137 + return file; 1.138 + } 1.139 + 1.140 + int elemsToSub = oldwdSplit.length - diff; 1.141 + StringBuffer resultStr = new StringBuffer(100); 1.142 + for(int i=0; i<newwdSplit.length - elemsToSub; i++) { 1.143 + resultStr.append(newwdSplit[i]); 1.144 + resultStr.append('/'); 1.145 + } 1.146 + 1.147 + for(int i=diff; i<fileSplit.length; i++) { 1.148 + resultStr.append(fileSplit[i]); 1.149 + if (i < fileSplit.length - 1) resultStr.append('/'); 1.150 + } 1.151 + 1.152 + return new URI(resultStr.toString()); 1.153 + } 1.154 +}