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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 384
8f2986ff0235
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial