ohair@286: /* ohair@286: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: * ohair@286: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.fastinfoset.tools; ohair@286: ohair@286: import java.io.BufferedInputStream; ohair@286: import java.io.BufferedOutputStream; ohair@286: import java.io.File; ohair@286: import java.io.FileInputStream; ohair@286: import java.io.FileOutputStream; ohair@286: import java.io.IOException; ohair@286: import java.io.InputStream; ohair@286: import java.io.OutputStream; ohair@286: import com.sun.xml.internal.fastinfoset.CommonResourceBundle; ohair@286: import java.net.URI; ohair@286: import java.net.URISyntaxException; ohair@286: import org.xml.sax.EntityResolver; ohair@286: import org.xml.sax.InputSource; ohair@286: import org.xml.sax.SAXException; ohair@286: ohair@286: public abstract class TransformInputOutput { ohair@286: ohair@286: /** Creates a new instance of TransformInputOutput */ ohair@286: public TransformInputOutput() { ohair@286: } ohair@286: ohair@286: public void parse(String[] args) throws Exception { ohair@286: InputStream in = null; ohair@286: OutputStream out = null; ohair@286: if (args.length == 0) { ohair@286: in = new BufferedInputStream(System.in); ohair@286: out = new BufferedOutputStream(System.out); ohair@286: } else if (args.length == 1) { ohair@286: in = new BufferedInputStream(new FileInputStream(args[0])); ohair@286: out = new BufferedOutputStream(System.out); ohair@286: } else if (args.length == 2) { ohair@286: in = new BufferedInputStream(new FileInputStream(args[0])); ohair@286: out = new BufferedOutputStream(new FileOutputStream(args[1])); ohair@286: } else { ohair@286: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.optinalFileNotSpecified")); ohair@286: } ohair@286: ohair@286: parse(in, out); ohair@286: } ohair@286: ohair@286: abstract public void parse(InputStream in, OutputStream out) throws Exception; ohair@286: ohair@286: // parse alternative with current working directory parameter ohair@286: // is used to process xml documents, which have external imported entities ohair@286: public void parse(InputStream in, OutputStream out, String workingDirectory) throws Exception { ohair@286: throw new UnsupportedOperationException(); ohair@286: } ohair@286: ohair@286: private static URI currentJavaWorkingDirectory; ohair@286: static { ohair@286: currentJavaWorkingDirectory = new File(System.getProperty("user.dir")).toURI(); ohair@286: } ohair@286: ohair@286: protected static EntityResolver createRelativePathResolver(final String workingDirectory) { ohair@286: return new EntityResolver() { ohair@286: public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { ohair@286: if (systemId != null && systemId.startsWith("file:/")) { ohair@286: URI workingDirectoryURI = new File(workingDirectory).toURI(); ohair@286: URI workingFile; ohair@286: try { ohair@286: // Construction new File(new URI(String)).toURI() is used to be sure URI has correct representation without redundant '/' ohair@286: workingFile = convertToNewWorkingDirectory(currentJavaWorkingDirectory, workingDirectoryURI, new File(new URI(systemId)).toURI()); ohair@286: return new InputSource(workingFile.toString()); ohair@286: } catch (URISyntaxException ex) { ohair@286: //Should not get here ohair@286: } ohair@286: } ohair@286: return null; ohair@286: } ohair@286: }; ohair@286: } ohair@286: ohair@286: private static URI convertToNewWorkingDirectory(URI oldwd, URI newwd, URI file) throws IOException, URISyntaxException { ohair@286: String oldwdStr = oldwd.toString(); ohair@286: String newwdStr = newwd.toString(); ohair@286: String fileStr = file.toString(); ohair@286: ohair@286: String cmpStr = null; ohair@286: // In simpliest case /file.xml - do it faster ohair@286: if (fileStr.startsWith(oldwdStr) && (cmpStr = fileStr.substring(oldwdStr.length())).indexOf('/') == -1) { ohair@286: return new URI(newwdStr + '/' + cmpStr); ohair@286: } ohair@286: ohair@286: String[] oldwdSplit = oldwdStr.split("/"); ohair@286: String[] newwdSplit = newwdStr.split("/"); ohair@286: String[] fileSplit = fileStr.split("/"); ohair@286: ohair@286: int diff; ohair@286: for(diff = 0; diff diff) { ohair@286: return file; ohair@286: } ohair@286: ohair@286: int elemsToSub = oldwdSplit.length - diff; ohair@286: StringBuffer resultStr = new StringBuffer(100); ohair@286: for(int i=0; i