NOTE: Do not register too much listeners as they may affect the inflation time. - * - * @param listener The listener to register. - */ - public abstract void registerInflateListener(IInflateListener listener); + /** + * Reset the {@link ContextProvider} of this inflater + * + * @param provider The new {@link ContextProvider} + */ + public abstract void resetContextProvider(ContextProvider provider); - /** - * Unregister the inflate listener if it is registered. - * - * @param listener The listener to unregister - */ - public abstract void unregisterListener(IInflateListener listener); + /** + * Register the inflate listener to this inflater. + * + *
NOTE: Do not register too much listeners as they may affect the inflation time. + * + * @param listener The listener to register. + */ + public abstract void registerInflateListener(IInflateListener listener); - /** - * Get the resource finder attached to this layout inflater. - * - * @return The attached resource finder. - */ - @NonNull - protected abstract IResourceTable requireResourceFinder(); + /** + * Unregister the inflate listener if it is registered. + * + * @param listener The listener to unregister + */ + public abstract void unregisterListener(IInflateListener listener); - /** - * Inflate the layout from the given file path - * - * @param path The file path to inflate layout from - * @return The inflated layout - * @throws InflateException when there was an error inflating the layout - */ - @NonNull - public IView inflatePath(String path, ViewGroup parent) throws InflateException { - Preconditions.assertNotBlank(path, "Layout file path is blank!"); - return inflate(new File(path), parent); + /** + * Get the resource finder attached to this layout inflater. + * + * @return The attached resource finder. + */ + @NonNull + protected abstract IResourceTable requireResourceTable(); + + /** + * Inflate the layout from the given file path + * + * @param path The file path to inflate layout from + * @return The inflated layout + * @throws InflateException when there was an error inflating the layout + */ + @NonNull + public IView inflatePath(String path, ViewGroup parent) throws InflateException { + Preconditions.assertNotBlank(path, "Layout file path is blank!"); + return inflate(new File(path), parent); + } + + /** + * Inflate the layout from the given file + * + * @param file The file to inflate layout from + * @return The inflated layout + * @throws InflateException when there was an error inflating the layout + */ + @NonNull + public IView inflate(File file, ViewGroup parent) throws InflateException { + + Preconditions.assertNotnull(file, "Cannot inflate null file"); + + // How can we inflate a non existent file? + if (!file.exists()) { + throw new InflateException("File does not exist!"); } - /** - * Inflate the layout from the given file - * - * @param file The file to inflate layout from - * @return The inflated layout - * @throws InflateException when there was an error inflating the layout - */ - @NonNull - public IView inflate(File file, ViewGroup parent) throws InflateException { - - Preconditions.assertNotnull(file, "Cannot inflate null file"); - - // How can we inflate a non existent file? - if (!file.exists()) { - throw new InflateException("File does not exist!"); - } - - if (!file.canRead()) { - throw new InflateException("Cannot read file. Permission denied!"); - } - - // Cannot inflate a non UTF-8 too. This will also be true if the file is empty. - if (!FileUtils.isUtf8(file)) { - throw new InflateException("File is not UTF-8 or is empty!"); - } - - requireResourceFinder().setInflatingFile(file); - this.currentlyInflatingFile = file; - return inflate(FileIOUtils.readFile2String(file), parent); + if (!file.canRead()) { + throw new InflateException("Cannot read file. Permission denied!"); } + // Cannot inflate a non UTF-8 too. This will also be true if the file is empty. + if (!FileUtils.isUtf8(file)) { + throw new InflateException("File is not UTF-8 or is empty!"); + } + + requireResourceTable().setInflatingFile(file); + this.currentlyInflatingFile = file; + return inflate(FileIOUtils.readFile2String(file), parent); + } + + /** + * Inflate the layout from the provided XML layout code + * + * @param layout The XML layout code + * @return The inflated layout + * @throws InflateException when there was an error inflating the layout + */ + @NonNull + protected IView inflate(String layout, ViewGroup parent) throws InflateException { + + Preconditions.assertNotBlank(layout, "Layout is blank!"); + + // Trim the layout code + layout = layout.trim(); + + final Matcher matcher = XML_DECL.matcher(layout); + if (matcher.find()) { + layout = layout.substring(matcher.end()).trim(); + } + + return doInflate(layout, parent); + } + + /** + * Get the currently inflating file. + * + * @return The file. + */ + @Nullable + protected File getFile() { + return currentlyInflatingFile; + } + + /** Provides a reference to {@link Context}. This will be used to create original view. */ + public interface ContextProvider { + /** - * Inflate the layout from the provided XML layout code + * Provide the context to this inflater. * - * @param layout The XML layout code - * @return The inflated layout - * @throws InflateException when there was an error inflating the layout + * @return The context */ - @NonNull - protected IView inflate(String layout, ViewGroup parent) throws InflateException { - - Preconditions.assertNotBlank(layout, "Layout is blank!"); - - // Trim the layout code - layout = layout.trim(); - - final Matcher matcher = XML_DECL.matcher(layout); - if (matcher.find()) { - layout = layout.substring(matcher.end()).trim(); - } - - return doInflate(layout, parent); - } - - /** - * Creates an instance of {@code ILayoutInflater} - * - * @param config The configuration of the LayoutInflater - * @return A new layout inflater instance - */ - @NonNull - public static ILayoutInflater newInstance(LayoutInflaterConfiguration config) { - return new XMLLayoutInflater(config); - } - - /** - * Get the currently inflating file. - * - * @return The file. - */ - @Nullable - protected File getFile() { - return currentlyInflatingFile; - } - - /** Provides a reference to {@link Context}. This will be used to create original view. */ - public static interface ContextProvider { - - /** - * Provide the context to this inflater. - * - * @return The context - */ - Context getContext(); - } + Context getContext(); + } } diff --git a/xml-inflater/src/main/java/com/itsaky/inflater/XMLLayoutInflater.java b/xml-inflater/src/main/java/com/itsaky/inflater/XMLLayoutInflater.java index a1ffc5a1c..a4913ebc3 100644 --- a/xml-inflater/src/main/java/com/itsaky/inflater/XMLLayoutInflater.java +++ b/xml-inflater/src/main/java/com/itsaky/inflater/XMLLayoutInflater.java @@ -130,7 +130,7 @@ class XMLLayoutInflater extends BaseLayoutInflater { @NonNull @Override - protected IResourceTable requireResourceFinder() { + protected IResourceTable requireResourceTable () { return this.resFinder; }