| 144 | } |
| 145 | |
| 146 | void flattenFilenames(DocumentPtr doc, const FileSearchPath& searchPath, StringResolverPtr customResolver) |
| 147 | { |
| 148 | for (ElementPtr elem : doc->traverseTree()) |
| 149 | { |
| 150 | ValueElementPtr valueElem = elem->asA<ValueElement>(); |
| 151 | if (!valueElem || valueElem->getType() != FILENAME_TYPE_STRING) |
| 152 | { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | FilePath unresolvedValue(valueElem->getValueString()); |
| 157 | if (unresolvedValue.isEmpty()) |
| 158 | { |
| 159 | continue; |
| 160 | } |
| 161 | StringResolverPtr elementResolver = elem->createStringResolver(); |
| 162 | // If the path is already absolute then don't allow an additional prefix |
| 163 | // as this would make the path invalid. |
| 164 | if (unresolvedValue.isAbsolute()) |
| 165 | { |
| 166 | elementResolver->setFilePrefix(EMPTY_STRING); |
| 167 | } |
| 168 | string resolvedString = valueElem->getResolvedValueString(elementResolver); |
| 169 | |
| 170 | // Convert relative to absolute pathing if the file is not already found |
| 171 | if (!searchPath.isEmpty()) |
| 172 | { |
| 173 | FilePath resolvedValue(resolvedString); |
| 174 | if (!resolvedValue.isAbsolute()) |
| 175 | { |
| 176 | for (size_t i = 0; i < searchPath.size(); i++) |
| 177 | { |
| 178 | FilePath testPath = searchPath[i] / resolvedValue; |
| 179 | testPath = testPath.getNormalized(); |
| 180 | if (testPath.exists()) |
| 181 | { |
| 182 | resolvedString = testPath.asString(); |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // Apply any custom filename resolver |
| 190 | if (customResolver && customResolver->isResolvedType(FILENAME_TYPE_STRING)) |
| 191 | { |
| 192 | resolvedString = customResolver->resolve(resolvedString, FILENAME_TYPE_STRING); |
| 193 | } |
| 194 | |
| 195 | valueElem->setValueString(resolvedString); |
| 196 | } |
| 197 | |
| 198 | // Remove any file prefix attributes |
| 199 | for (ElementPtr elem : doc->traverseTree()) |
| 200 | { |
| 201 | if (elem->hasFilePrefix()) |
| 202 | { |
| 203 | elem->removeAttribute(Element::FILE_PREFIX_ATTRIBUTE); |
no test coverage detected