1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package org.apache.openoffice.ooxml.framework.part; 23 24 /** Operations around part names. 25 */ 26 public class PartName 27 implements Comparable<PartName> 28 { PartName(final String sPath)29 public PartName (final String sPath) 30 { 31 if ( ! (sPath.isEmpty() || sPath.startsWith("/"))) 32 { 33 assert(sPath.isEmpty() || sPath.startsWith("/")); 34 } 35 assert(sPath.indexOf('\\') == -1); 36 37 msPath = sPath; 38 } 39 40 41 42 PartName( final String sPath, final PartName aParentName, final String sMode)43 public PartName ( 44 final String sPath, 45 final PartName aParentName, 46 final String sMode) 47 { 48 switch(sMode) 49 { 50 case "External": 51 msPath = sPath; 52 break; 53 54 case "Internal": 55 msPath = Cleanup(aParentName.GetPathname() + "/" + sPath); 56 break; 57 58 default: 59 throw new RuntimeException(); 60 } 61 } 62 63 64 65 getRelationshipsPartName()66 public PartName getRelationshipsPartName () 67 { 68 return new PartName(GetPathname() + "/_rels/" + GetBasename() + ".rels"); 69 } 70 71 72 73 GetPathname()74 private String GetPathname () 75 { 76 if (msPath.isEmpty()) 77 return ""; 78 else 79 { 80 final int nPathnameEnd = msPath.lastIndexOf('/'); 81 assert(nPathnameEnd>=0); 82 return msPath.substring(0, nPathnameEnd); 83 } 84 } 85 86 87 88 GetBasename()89 public String GetBasename () 90 { 91 if (msPath.isEmpty()) 92 return ""; 93 else 94 { 95 final int nBasenameStart = msPath.lastIndexOf('/'); 96 assert(nBasenameStart>=0); 97 return msPath.substring(nBasenameStart+1); 98 } 99 } 100 101 102 103 GetExtension()104 public String GetExtension () 105 { 106 final int nExtensionStart = msPath.lastIndexOf('.'); 107 if (nExtensionStart < 0) 108 return null; 109 else 110 return msPath.substring(nExtensionStart+1); 111 } 112 113 114 115 GetFullname()116 public String GetFullname() 117 { 118 return msPath; 119 } 120 121 122 123 124 @Override compareTo(final PartName aOther)125 public int compareTo (final PartName aOther) 126 { 127 return msPath.compareTo(aOther.msPath); 128 } 129 130 131 132 Cleanup(final String sName)133 private String Cleanup (final String sName) 134 { 135 return sName.replaceAll("/[^/]+/\\.\\./", "/"); 136 } 137 138 139 140 141 @Override toString()142 public String toString () 143 { 144 return msPath; 145 } 146 147 148 149 150 private final String msPath; 151 } 152