/*
(non-Javadoc)

@see net.jawr.web.resource.bundle.handler.ResourceBundlesHandler#
getBundlePaths (java.lang.String,
net.jawr.web.resource.bundle.iterator.ConditionalCommentCallbackHandler,
java.util.Map)
345157
@Override
	public ResourceBundlePathsIterator getBundlePaths(String bundleId,
			ConditionalCommentCallbackHandler commentCallbackHandler, Map<String, String> variants) {
		return rsHandler.getBundlePaths(bundleId, commentCallbackHandler, variants);
	}
1138348
@Override
	public List<PathMapping> getPathMappings(JoinableResourceBundle bundle, String resourcePath,
			ResourceReaderHandler rsReader) {

		List<PathMapping> pathMappings = new ArrayList<>();
		PathMapping pathMapping = null;
		GeneratorMappingHelper helper = new GeneratorMappingHelper(resourcePath);
		String path = resolver.getResourcePath(helper.getPath());
		String[] names = path.split(RESOURCE_BUNDLE_SEPARATOR);

		for (String resourceName : names) {

			path = resourceName.replace(PACKAGE_SEPARATOR, JawrConstant.URL_SEPARATOR_CHAR);
			path = control.toBundleName(path, new Locale("", "")) + PROPERTIES_FILE_SUFFIX;
			URL rbURL = LocaleUtils.getResourceBundleURL(path, config.getContext());
			if (rbURL != null) {
				String strURL = rbURL.toString();
				// returns the path mapping only if the MessageBundle are on a
				// filesystem
				if (strURL.startsWith(JawrConstant.FILE_URL_PREFIX)) {
					String parentPath = "/WEB-INF/classes/" + PathNormalizer.getParentPath(path);
					String resourceBundleName = FileNameUtils.getBaseName(path);
					MessageBundleFileFilter filter = new MessageBundleFileFilter(resourceBundleName);
					pathMapping = new PathMapping(bundle, parentPath, filter);
					pathMappings.add(pathMapping);
				}
			}
		}
		return pathMappings;
	}
17880
@Override
	public ResourceBundlePathsIterator getGlobalResourceBundlePaths(String bundleId,
			ConditionalCommentCallbackHandler commentCallbackHandler, Map<String, String> variants) {

		List<JoinableResourceBundle> currentBundles = new ArrayList<>();
		for (JoinableResourceBundle bundle : globalBundles) {
			if (bundle.getId().equals(bundleId)) {
				currentBundles.add(bundle);
				break;
			}
		}
		return getBundleIterator(getDebugMode(), currentBundles, commentCallbackHandler, variants);
	}
1119471
@Override
	public Reader getResource(JoinableResourceBundle bundle, String resourceName, boolean processingBundle) {

		Locale locale = null;
		String path = generator.getResolver().getResourcePath(resourceName);

		Map<String, String> contextVariants = new HashMap<>();
		Map<String, VariantSet> variantSets = new HashMap<>();
		if (generator instanceof VariantResourceGenerator || generator instanceof LocaleAwareResourceGenerator) {

			int variantSuffixIdx = path.indexOf("@");
			if (variantSuffixIdx != -1) {

				String variantKey = path.substring(path.indexOf('@') + 1);

				// Remove variant suffix
				path = path.substring(0, variantSuffixIdx);

				String[] variants = variantKey.split("@");
				if (generator instanceof VariantResourceGenerator) {
					variantSets = ((VariantResourceGenerator) generator).getAvailableVariants(path);
				} else { // instanceof LocaleAwareResourceGenerator
					List<String> availableLocales = ((LocaleAwareResourceGenerator) generator)
							.getAvailableLocales(path);
					if (availableLocales != null) {
						variantSets.put(JawrConstant.LOCALE_VARIANT_TYPE,
								new VariantSet(JawrConstant.LOCALE_VARIANT_TYPE, "", availableLocales));
					}
				}

				// Sort the variant types
				List<String> variantTypes = new ArrayList<>(variantSets.keySet());
				Collections.sort(variantTypes);
				int nbVariants = variants.length;

				for (int i = 0; i < nbVariants; i++) {
					String variantType = variantTypes.get(i);
					String variantValue = variants[i];
					contextVariants.put(variantType, variantValue);
					if (variantType.equals(JawrConstant.LOCALE_VARIANT_TYPE)) {
						// Resourcebundle should be doing this for me...
						String[] params = variantValue.split("_");
						switch (params.length) {
						case 3:
							locale = new Locale(params[0], params[1], params[2]);
							break;
						case 2:
							locale = new Locale(params[0], params[1]);
							break;
						default:
							locale = new Locale(variantValue);
						}
					}
				}
			}
		}

		GeneratorContext context = new GeneratorContext(bundle, config, path);
		context.setVariantMap(contextVariants);
		context.setVariantSets(variantSets);

		context.setLocale(locale);
		context.setResourceReaderHandler(rsHandler);
		context.setProcessingBundle(processingBundle);

		return generator.createResource(context);
	}
1265909
private List<String> getResourcePaths() {

        List<String> res = new ArrayList<String>();
        for (Component c : m_resources) {
            if (c instanceof CmsRemovableFormRow<?>) {
                AbstractField<?> field = (AbstractField<?>)(((CmsRemovableFormRow)c).getInput());
                res.add((String)(field.getValue()));
            }
        }
        return res;
    }
109354
public Set<String> getConfiguredWorkplaceBundles() {

        Set<String> result = new HashSet<String>();
        for (CmsResourceTypeConfig config : internalGetResourceTypes(false)) {
            String bundlename = config.getConfiguredWorkplaceBundle();
            if (null != bundlename) {
                result.add(bundlename);
            }
        }
        return result;
    }
470668
@Override
	public String getFilePath(String resourcePath) {

		String filePath = null;
		String path = getCompletePath(resourcePath);
		URL url = null;
		try {
			url = ClassLoaderResourceUtils.getResourceURL(path, this);
			String strURL = url.toString();
			if (strURL.startsWith(JawrConstant.FILE_URL_PREFIX)) {
				filePath = new File(url.getFile()).getAbsolutePath();
			} else if (strURL.startsWith(JawrConstant.JAR_URL_PREFIX + JawrConstant.FILE_URL_PREFIX)) {
				String tmp = strURL.substring((JawrConstant.JAR_URL_PREFIX + JawrConstant.FILE_URL_PREFIX).length());
				int idxJarContentSeparator = tmp.indexOf("!");
				if (idxJarContentSeparator != -1) {
					tmp = tmp.substring(0, idxJarContentSeparator);
				}
				filePath = new File(tmp).getAbsolutePath();
			}
		} catch (ResourceNotFoundException e) {
			filePath = null;
		}

		return filePath;
	}
777030
public static List<String> getAvailableLocaleSuffixesForBundle(String messageBundlePath, String fileSuffix) {
		return getAvailableLocaleSuffixesForBundle(messageBundlePath, fileSuffix, null);
	}
1125023
@Override
	protected void performGlobalBundleLinksRendering(BundleRendererContext ctx, Writer out, boolean debugOn)
			throws IOException {

		if (isForcedToRenderIeCssBundleInDebug(ctx, debugOn)) {
			ResourceBundlePathsIterator resourceBundleIterator = bundler.getGlobalResourceBundlePaths(
					DebugMode.FORCE_NON_DEBUG_IN_IE, new ConditionalCommentRenderer(out), ctx.getVariants());
			while (resourceBundleIterator.hasNext()) {
				BundlePath globalBundlePath = resourceBundleIterator.nextPath();
				renderIeCssBundleLink(ctx, out, globalBundlePath);
			}
		} else {

			super.performGlobalBundleLinksRendering(ctx, out, debugOn);
		}
	}
1299891
protected String getClasspath() throws IOException {
        List<String> classpath = new ArrayList<>();
        classpath.add(getBundleJarPath());
        classpath.addAll(getPluginsPath());
        return StringUtils.toString(classpath.toArray(new String[classpath.size()]), " ");
    }
1012028
private void addPathMapping(BundlePathMapping bundlePathMapping, String pathMapping) {

		addFilePathMapping(bundlePathMapping, pathMapping);

		if (!bundle.getInclusionPattern().isIncludeOnlyOnDebug()) {
			bundlePathMapping.getItemPathList().add(new BundlePath(bundle.getBundlePrefix(), pathMapping));
		}

		if (!bundle.getInclusionPattern().isExcludeOnDebug()) {
			bundlePathMapping.getItemDebugPathList().add(new BundlePath(bundle.getBundlePrefix(), pathMapping));
		}

	}
1054660
protected String getBundlePrefix(BundleProcessingStatus status, JawrConfig jawrConfig, String bundleName) {

		// Generation the bundle prefix
		String bundlePrefix = status.getCurrentBundle().getBundlePrefix();
		if (bundlePrefix == null) {
			bundlePrefix = "";
		} else {
			bundlePrefix = PathNormalizer.asPath(bundlePrefix);
		}
		if (!bundleName.equals(ResourceGenerator.CSS_DEBUGPATH)) {
			bundlePrefix += FAKE_BUNDLE_PREFIX;
		}

		// Add path reference for the servlet mapping if it exists
		if (!"".equals(jawrConfig.getServletMapping())) {
			bundlePrefix = PathNormalizer.asPath(jawrConfig.getServletMapping() + bundlePrefix) + "/";
		}
		return bundlePrefix;
	}
1297159
protected List<RenderedLink> createLinkToBundle(
			ResourceBundlesHandler handler, String path, String resourceType,
			Map<String, String> variantMap) throws IOException {

		ArrayList<RenderedLink> linksToBundle = new ArrayList<RenderedLink>();

		BasicBundleRenderer bundleRenderer = new BasicBundleRenderer(handler,
				resourceType);
		StringWriter sw = new StringWriter();

		// The gzip compression will be made by the CDN server
		// So we force it to false.
		boolean useGzip = false;

		// The generation of bundle is the same in SSL and non SSL mode
		boolean isSslRequest = false;

		// First deals with the production mode
		handler.getConfig().setDebugModeOn(false);
		handler.getConfig().setGzipResourcesModeOn(useGzip);

		BundleRendererContext ctx = new BundleRendererContext("", variantMap,
				useGzip, isSslRequest);
		bundleRenderer.renderBundleLinks(path, ctx, sw);

		// Then take in account the debug mode
		handler.getConfig().setDebugModeOn(true);
		ctx = new BundleRendererContext("", variantMap, useGzip, isSslRequest);
		bundleRenderer.renderBundleLinks(path, ctx, sw);

		List<RenderedLink> renderedLinks = bundleRenderer.getRenderedLinks();
		// Remove context override path if it's defined.
		String contextPathOverride = handler.getConfig()
				.getContextPathOverride();
		for (Iterator<RenderedLink> iterator = renderedLinks.iterator(); iterator
				.hasNext();) {
			RenderedLink renderedLink = iterator.next();
			String renderedLinkPath = renderedLink.getLink();
			// Remove the context path override
			if (StringUtils.isNotEmpty(contextPathOverride)
					&& renderedLinkPath.startsWith(contextPathOverride)) {
				renderedLinkPath = renderedLinkPath
						.substring(contextPathOverride.length());
			}
			renderedLink.setLink(PathNormalizer.asPath(renderedLinkPath));
			linksToBundle.add(renderedLink);
		}

		return linksToBundle;
	}
826106
@Override
	public String getFilePath(String resourcePath) {

		String filePath = null;
		List<ResourceBrowser> list = new ArrayList<>();
		list.addAll(resourceInfoProviders);
		for (Iterator<ResourceBrowser> iterator = list.iterator(); iterator.hasNext() && filePath == null;) {
			ResourceBrowser rsBrowser = iterator.next();
			if (generatorRegistry.isPathGenerated(resourcePath)) {
				if (rsBrowser instanceof ResourceGenerator) {
					ResourceGenerator rsGeneratorBrowser = (ResourceGenerator) rsBrowser;
					if (rsGeneratorBrowser.getResolver().matchPath(resourcePath)) {
						filePath = rsBrowser.getFilePath(resourcePath);
					}
				}
			} else {
				if (!(rsBrowser instanceof ResourceGenerator)) {
					filePath = rsBrowser.getFilePath(resourcePath);
				}
			}
		}
		return filePath;
	}
183748
@Override
	public BundlePathMapping build(List<String> strPathMappings) {

		BundlePathMapping bundlePathMapping = new BundlePathMapping(bundle);
		for (JoinableResourceBundle child : ((CompositeResourceBundle) bundle).getChildBundles()) {
			if (!child.getInclusionPattern().isIncludeOnlyOnDebug()) {
				bundlePathMapping.getItemPathList().addAll(child.getItemPathList());
				addFilePathMapping(bundlePathMapping, child.getItemPathList());
			}

			if (!child.getInclusionPattern().isExcludeOnDebug()) {
				bundlePathMapping.getItemDebugPathList().addAll(child.getItemDebugPathList());
				addFilePathMapping(bundlePathMapping, child.getItemDebugPathList());
			}
			bundlePathMapping.getLicensesPathList().addAll(child.getLicensesPathList());
			addFilePathMapping(bundlePathMapping, child.getLicensesPathList());
		}
		return bundlePathMapping;
	}
864094
private ResourceBundlePathsIterator getBundleIterator(DebugMode debugMode, List<JoinableResourceBundle> bundles,
			ConditionalCommentCallbackHandler commentCallbackHandler, Map<String, String> variants) {
		ResourceBundlePathsIterator bundlesIterator;
		if (debugMode.equals(DebugMode.DEBUG)) {
			bundlesIterator = new DebugModePathsIteratorImpl(bundles, commentCallbackHandler, variants);
		} else if (debugMode.equals(DebugMode.FORCE_NON_DEBUG_IN_IE)) {
			bundlesIterator = new IECssDebugPathsIteratorImpl(bundles, commentCallbackHandler, variants);
		} else {
			bundlesIterator = new PathsIteratorImpl(bundles, commentCallbackHandler, variants);
		}
		
		return bundlesIterator;
	}
1266566
@Override
	public String getResourcePath(String basePath, String relativePath) {

		String result = null;
		if (binaryGeneratorRegistry.isGeneratedBinaryResource(relativePath)) {
			result = relativePath;
		} else if (contextPath != null && relativePath.startsWith(contextPath)) {
			result = relativePath.substring(contextPath.length() - 1);
		} else {
			result = PathNormalizer.concatWebPath(basePath, relativePath);
		}
		return result;
	}