Factory.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREFACTORY_H
  28. #define ROCKETCOREFACTORY_H
  29. #include "XMLParser.h"
  30. #include "Header.h"
  31. #include <map>
  32. namespace Rocket {
  33. namespace Core {
  34. class Context;
  35. class ContextInstancer;
  36. class Decorator;
  37. class DecoratorInstancer;
  38. class Element;
  39. class ElementDocument;
  40. class ElementInstancer;
  41. class Event;
  42. class EventInstancer;
  43. class EventListener;
  44. class EventListenerInstancer;
  45. class FontEffect;
  46. class FontEffectInstancer;
  47. class StyleSheet;
  48. class PropertyDictionary;
  49. /**
  50. The Factory contains a registry of instancers for different types.
  51. All instantiation of these rocket types should go through the factory
  52. so that scripting API's can bind in new types.
  53. @author Lloyd Weehuizen
  54. */
  55. class ROCKETCORE_API Factory
  56. {
  57. public:
  58. /// Initialise the element factory
  59. static bool Initialise();
  60. /// Cleanup and shutdown the factory
  61. static void Shutdown();
  62. /// Registers the instancer to use when instancing contexts.
  63. /// @param[in] instancer The new context instancer.
  64. static ContextInstancer* RegisterContextInstancer(ContextInstancer* instancer);
  65. /// Instances a new context.
  66. /// @param[in] name The name of the new context.
  67. /// @return The new context, or NULL if no context could be created.
  68. static Context* InstanceContext(const String& name);
  69. /// Registers an element instancer that will be used to instance an element when the specified tag is encountered.
  70. /// @param[in] name Name of the instancer; elements with this as their tag will use this instancer.
  71. /// @param[in] instancer The instancer to call when the tag is encountered.
  72. /// @return The added instancer if the registration was successful, NULL otherwise.
  73. static ElementInstancer* RegisterElementInstancer(const String& name, ElementInstancer* instancer);
  74. /// Returns the element instancer for the specified tag.
  75. /// @param[in] tag Name of the tag to get the instancer for.
  76. /// @return The requested element instancer, or NULL if no such instancer is registered.
  77. static ElementInstancer* GetElementInstancer(const String& tag);
  78. /// Instances a single element.
  79. /// @param[in] parent The parent of the new element, or NULL for a root tag.
  80. /// @param[in] instancer The name of the instancer to create the element with.
  81. /// @param[in] tag The tag of the element to be instanced.
  82. /// @param[in] attributes The attributes to instance the element with.
  83. /// @return The instanced element, or NULL if the instancing failed.
  84. static Element* InstanceElement(Element* parent, const String& instancer, const String& tag, const XMLAttributes& attributes);
  85. /// Instances a single text element containing a string. The string is assumed to contain no RML markup, but will
  86. /// be translated and therefore may have some introduced. In this case more than one element may be instanced.
  87. /// @param[in] parent The element any instanced elements will be parented to.
  88. /// @param[in] text The text to instance the element (or elements) from.
  89. /// @return True if the string was parsed without error, false otherwise.
  90. static bool InstanceElementText(Element* parent, const String& text);
  91. /// Instances an element tree based on the stream.
  92. /// @param[in] parent The element the stream elements will be added to.
  93. /// @param[in] stream The stream to read the element RML from.
  94. /// @return True if the stream was parsed without error, false otherwise.
  95. static bool InstanceElementStream(Element* parent, Stream* stream);
  96. /// Instances a document from a stream.
  97. /// @param[in] context The context that is creating the document.
  98. /// @param[in] stream The stream to instance from.
  99. /// @return The instanced document, or NULL if an error occurred.
  100. static ElementDocument* InstanceDocumentStream(Rocket::Core::Context* context, Stream* stream);
  101. /// Registers an instancer that will be used to instance decorators.
  102. /// @param[in] name The name of the decorator the instancer will be called for.
  103. /// @param[in] instancer The instancer to call when the decorator name is encountered.
  104. /// @return The added instancer if the registration was successful, NULL otherwise.
  105. static DecoratorInstancer* RegisterDecoratorInstancer(const String& name, DecoratorInstancer* instancer);
  106. /// Attempts to instance a decorator from an instancer registered with the factory.
  107. /// @param[in] name The name of the desired decorator type.
  108. /// @param[in] properties The properties associated with the decorator.
  109. /// @return The newly instanced decorator, or NULL if the decorator could not be instanced.
  110. static Decorator* InstanceDecorator(const String& name, const PropertyDictionary& properties);
  111. /// Registers an instancer that will be used to instance font effects.
  112. /// @param[in] name The name of the font effect the instancer will be called for.
  113. /// @param[in] instancer The instancer to call when the font effect name is encountered.
  114. /// @return The added instancer if the registration was successful, NULL otherwise.
  115. static FontEffectInstancer* RegisterFontEffectInstancer(const String& name, FontEffectInstancer* instancer);
  116. /// Attempts to instance a font effect from an instancer registered with the factory.
  117. /// @param[in] name The name of the desired font effect type.
  118. /// @param[in] properties The properties associated with the font effect.
  119. /// @return The newly instanced font effect, or NULL if the font effect could not be instanced.
  120. static FontEffect* InstanceFontEffect(const String& name, const PropertyDictionary& properties);
  121. /// Creates a style sheet from a user-generated string.
  122. /// @param[in] string The contents of the style sheet.
  123. /// @return A pointer to the newly created style sheet.
  124. static StyleSheet* InstanceStyleSheetString(const String& string);
  125. /// Creates a style sheet from a file.
  126. /// @param[in] file_name The location of the style sheet file.
  127. /// @return A pointer to the newly created style sheet.
  128. static StyleSheet* InstanceStyleSheetFile(const String& file_name);
  129. /// Creates a style sheet from an Stream.
  130. /// @param[in] stream A pointer to the stream containing the style sheet's contents.
  131. /// @return A pointer to the newly created style sheet.
  132. static StyleSheet* InstanceStyleSheetStream(Stream* stream);
  133. /// Clears the style sheet cache. This will force style sheets to be reloaded.
  134. static void ClearStyleSheetCache();
  135. /// Clears the template cache. This will force template to be reloaded.
  136. static void ClearTemplateCache();
  137. /// Registers an instancer for all events.
  138. /// @param[in] instancer The instancer to be called.
  139. /// @return The registered instanced on success, NULL on failure.
  140. static EventInstancer* RegisterEventInstancer(EventInstancer* instancer);
  141. /// Instance and event object
  142. /// @param[in] target Target element of this event.
  143. /// @param[in] name Name of this event.
  144. /// @param[in] parameters Additional parameters for this event.
  145. /// @param[in] interruptible If the event propagation can be stopped.
  146. /// @return The instanced event.
  147. static Event* InstanceEvent(Element* target, const String& name, const Dictionary& parameters, bool interruptible);
  148. /// Register the instancer to be used for all event listeners.
  149. /// @return The registered instancer on success, NULL on failure.
  150. static EventListenerInstancer* RegisterEventListenerInstancer(EventListenerInstancer* instancer);
  151. /// Instance an event listener with the given string. This is used for instancing listeners for the on* events from
  152. /// RML.
  153. /// @param[in] value The parameters to the event listener.
  154. /// @return The instanced event listener.
  155. static EventListener* InstanceEventListener(const String& value, Element* element);
  156. private:
  157. Factory();
  158. ~Factory();
  159. };
  160. }
  161. }
  162. #endif