use of com.yahoo.io.reader.NamedReader in project vespa by vespa-engine.
the class PageTemplateConfigurer method toRegistry.
/**
* Creates a new page template registry from the content of a config and returns it.
* The returned registry will <b>not</b> be frozen. This should be done, by calling freeze(), before it is used.
*/
public static PageTemplateRegistry toRegistry(PageTemplatesConfig config) {
List<NamedReader> pageReaders = new ArrayList<>();
int pageNumber = 0;
for (String pageString : config.page()) pageReaders.add(new NamedReader("page[" + pageNumber++ + "]", new StringReader(pageString)));
return new PageTemplateXMLReader().read(pageReaders, false);
}
use of com.yahoo.io.reader.NamedReader in project vespa by vespa-engine.
the class PageTemplateXMLReader method readFile.
/**
* Reads a single page template file.
*
* @throws RuntimeException if <code>fileName</code> is not a readable file, or if there is some error in the XML
*/
public PageTemplate readFile(String fileName) {
NamedReader pageReader = null;
try {
File file = new File(fileName);
pageReader = new NamedReader(fileName, new FileReader(file));
String firstName = file.getName().substring(0, file.getName().length() - 4);
return read(Collections.singletonList(pageReader), true).getComponent(firstName);
} catch (IOException e) {
throw new IllegalArgumentException("Could not read the page template '" + fileName + "'", e);
} finally {
if (pageReader != null)
try {
pageReader.close();
} catch (IOException e) {
}
}
}
use of com.yahoo.io.reader.NamedReader in project vespa by vespa-engine.
the class QueryProfileXMLReader method createQueryProfiles.
public List<Element> createQueryProfiles(List<NamedReader> queryProfileReaders, QueryProfileRegistry registry) {
List<Element> queryProfileElements = new ArrayList<>(queryProfileReaders.size());
for (NamedReader reader : queryProfileReaders) {
Element root = XML.getDocument(reader).getDocumentElement();
if (!root.getNodeName().equals("query-profile")) {
throw new IllegalArgumentException("Root tag in '" + reader.getName() + "' must be 'query-profile', not '" + root.getNodeName() + "'");
}
String idString = root.getAttribute("id");
if (idString == null || idString.equals(""))
throw new IllegalArgumentException("Query profile '" + reader.getName() + "' has no 'id' attribute in the root element");
ComponentId id = new ComponentId(idString);
validateFileNameToId(reader.getName(), id, "query profile");
QueryProfile queryProfile = new QueryProfile(id);
String typeId = root.getAttribute("type");
if (typeId != null && !typeId.equals("")) {
QueryProfileType type = registry.getType(typeId);
if (type == null)
throw new IllegalArgumentException("Query profile '" + reader.getName() + "': Type id '" + typeId + "' can not be resolved");
queryProfile.setType(type);
}
Element dimensions = XML.getChild(root, "dimensions");
if (dimensions != null)
queryProfile.setDimensions(toArray(XML.getValue(dimensions)));
registry.register(queryProfile);
queryProfileElements.add(root);
}
return queryProfileElements;
}
use of com.yahoo.io.reader.NamedReader in project vespa by vespa-engine.
the class PageTemplatesTestCase method testExport.
@Test
public void testExport() throws IOException {
List<NamedReader> pageFiles = new ArrayList<>(2);
pageFiles.add(new NamedReader(root + "/slottingSerp.xml", IOUtils.createReader(root + "/slottingSerp.xml")));
pageFiles.add(new NamedReader(root + "/richSerp.xml", IOUtils.createReader(root + "/richSerp.xml")));
pageFiles.add(new NamedReader(root + "/footer.xml", IOUtils.createReader(root + "/footer.xml")));
pageFiles.add(new NamedReader(root + "/richerSerp.xml", IOUtils.createReader(root + "/richerSerp.xml")));
pageFiles.add(new NamedReader(root + "/header.xml", IOUtils.createReader(root + "/header.xml")));
assertEquals(IOUtils.readFile(new File(root, "/pages.cfg")), StringUtilities.implodeMultiline(ConfigInstance.serialize(new PageTemplates(pageFiles).getConfig())));
}
use of com.yahoo.io.reader.NamedReader in project vespa by vespa-engine.
the class PageTemplateXMLReader method createPages.
private Map<ComponentId, Element> createPages(List<NamedReader> pageReaders, boolean validateReaderNames) {
Map<ComponentId, Element> pageElementsByPageId = new LinkedHashMap<>();
for (NamedReader reader : pageReaders) {
Element pageElement = XML.getDocument(reader).getDocumentElement();
if (!pageElement.getNodeName().equals("page")) {
logger.info("Ignoring '" + reader.getName() + "': Expected XML root element 'page' but was '" + pageElement.getNodeName() + "'");
continue;
}
String idString = pageElement.getAttribute("id");
if (idString == null || idString.isEmpty())
throw new IllegalArgumentException("Page template '" + reader.getName() + "' has no 'id' attribute in the root element");
ComponentId id = new ComponentId(idString);
if (validateReaderNames)
validateFileName(reader.getName(), id, "page template");
registry.register(new PageTemplate(id));
pageElementsByPageId.put(id, pageElement);
}
return pageElementsByPageId;
}
Aggregations