@Test public void shouldRegisterExtractorsForCustomType() { // A document type is known String newDocumentType = "project"; DocumentTypes.addDocumentType(newDocumentType); // when we register extractors for the new type ModelExtractors.getInstance().registerExtractorsForCustomTypes(newDocumentType); // then an extractor is registered by pluralized type as key assertThat(ModelExtractors.getInstance().containsKey("projects")).isTrue(); // and an extractor for published types is registered assertThat(ModelExtractors.getInstance().containsKey("published_projects")).isTrue(); }
@Test public void shouldResetToNonCustomizedExtractors() throws Exception { //given: // A document type is known String newDocumentType = "project"; DocumentTypes.addDocumentType(newDocumentType); // when we register extractors for the new type ModelExtractors.getInstance().registerExtractorsForCustomTypes(newDocumentType); //expect: assertThat(ModelExtractors.getInstance().keySet().size()).isEqualTo(17); //when: ModelExtractors.getInstance().reset(); //then: assertThat(ModelExtractors.getInstance().keySet().size()).isEqualTo(15); } }
@Override public Object get(final Object property) { if (property instanceof String || property instanceof GString) { String key = property.toString(); try { put(key, extractors.extractAndTransform(db, key, model, new TemplateEngineAdapter.NoopAdapter())); } catch (NoModelExtractorException e) { // should never happen, as we iterate over existing extractors } } return super.get(property); } };
private void loadAndRegisterEngine(String className, String... extensions) { ModelExtractor engine = tryLoadEngine(className); if (engine != null) { for (String extension : extensions) { registerEngine(extension, engine); } } }
@Override public void added(String doctype) { ModelExtractors.getInstance().registerExtractorsForCustomTypes(doctype); } }
private void resetDocumentTypesAndExtractors() { DocumentTypes.resetDocumentTypes(); ModelExtractors.getInstance().reset(); }
@Test public void shouldLoadExtractorsOnInstantiation() { ModelExtractors.getInstance(); String[] expectedKeys = new String[]{ "pages", "posts", "indexs", "archives", "feeds", "published_posts", "published_pages", "published_content", "published_date", "all_content", "alltags", "db", "tag_posts", "tags", "tagged_documents", }; for (String aKey : expectedKeys) { assertThat(ModelExtractors.getInstance().containsKey(aKey)).isTrue(); } }
public void registerExtractorsForCustomTypes(String docType) { String pluralizedDoctype = DocumentTypeUtils.pluralize(docType); if (!containsKey(pluralizedDoctype)) { LOGGER.info("register new extractors for document type: {}", docType); registerEngine(pluralizedDoctype, new TypedDocumentsExtractor()); registerEngine("published_" + pluralizedDoctype, new PublishedCustomExtractor(docType)); } }
private void initializeContext(Locale locale, Map<String, Object> model) { context.clearVariables(); context.setLocale(locale); context.setVariables(model); for (String key : extractors.keySet()) { context.setVariable(key, new ContextVariable(db,key,model)); } }
private ModelExtractors() { extractors = new TreeMap<String, ModelExtractor>(); loadEngines(); }
/** * This method is used internally to load markup engines. Markup engines are found using descriptor files on * classpath, so adding an engine is as easy as adding a jar on classpath with the descriptor file included. */ private void loadEngines() { try { ClassLoader cl = ModelExtractors.class.getClassLoader(); Enumeration<URL> resources = cl.getResources(PROPERTIES); while (resources.hasMoreElements()) { URL url = resources.nextElement(); Properties props = new Properties(); props.load(url.openStream()); for (Map.Entry<Object, Object> entry : props.entrySet()) { String className = (String) entry.getKey(); String[] extensions = ((String) entry.getValue()).split(","); loadAndRegisterEngine(className, extensions); } } } catch (IOException e) { e.printStackTrace(); } }
@After public void tearDown() throws Exception { ModelExtractors.getInstance().reset(); }
@Test public void shouldThrowAnExceptionIfDocumentTypeIsUnknown() { thrown.expect(UnsupportedOperationException.class); String unknownDocumentType = "unknown"; ModelExtractors.getInstance().registerExtractorsForCustomTypes(unknownDocumentType); }
@Test public void shouldRegisterExtractorsForCustomType() { // given: "A document type is known" String newDocumentType = "project"; DocumentTypes.addDocumentType(newDocumentType); ModelExtractorsDocumentTypeListener listener = new ModelExtractorsDocumentTypeListener(); // when: "the listener is called with that type" listener.added(newDocumentType); // then: "an extractor is registered by pluralized type as key" assertThat(ModelExtractors.getInstance().containsKey("projects")).isTrue(); // and: "an extractor for published types is registered" assertThat(ModelExtractors.getInstance().containsKey("published_projects")).isTrue(); } }
public void registerExtractorsForCustomTypes(String docType) { String pluralizedDoctype = DocumentTypeUtils.pluralize(docType); if (!containsKey(pluralizedDoctype)) { LOGGER.info("register new extractors for document type: {}", docType); registerEngine(pluralizedDoctype, new TypedDocumentsExtractor()); registerEngine("published_" + pluralizedDoctype, new PublishedCustomExtractor(docType)); } }
private void loadAndRegisterEngine(String className, String... extensions) { ModelExtractor engine = tryLoadEngine(className); if (engine != null) { for (String extension : extensions) { registerEngine(extension, engine); } } }
private void initializeContext(Locale locale, Map<String, Object> model) { context.clearVariables(); context.setLocale(locale); context.setVariables(model); for (String key : extractors.keySet()) { context.setVariable(key, new ContextVariable(db,key,model)); } }
public void reset() { extractors.clear(); loadEngines(); }
/** * This method is used internally to load markup engines. Markup engines are found using descriptor files on * classpath, so adding an engine is as easy as adding a jar on classpath with the descriptor file included. */ private void loadEngines() { try { ClassLoader cl = ModelExtractors.class.getClassLoader(); Enumeration<URL> resources = cl.getResources(PROPERTIES); while (resources.hasMoreElements()) { URL url = resources.nextElement(); Properties props = new Properties(); props.load(url.openStream()); for (Map.Entry<Object, Object> entry : props.entrySet()) { String className = (String) entry.getKey(); String[] extensions = ((String) entry.getValue()).split(","); loadAndRegisterEngine(className, extensions); } } } catch (IOException e) { e.printStackTrace(); } }
@Test public void shouldRegisterExtractorsOnlyForCustomTypes() { String knownDocumentType = "alltag"; DocumentTypes.addDocumentType(knownDocumentType); ModelExtractors.getInstance().registerExtractorsForCustomTypes(knownDocumentType); assertThat(ModelExtractors.getInstance().containsKey("published_alltags")).isFalse(); }