Search in sources :

Example 6 with NamedReader

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);
}
Also used : ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) NamedReader(com.yahoo.io.reader.NamedReader)

Example 7 with NamedReader

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) {
            }
    }
}
Also used : NamedReader(com.yahoo.io.reader.NamedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 8 with NamedReader

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;
}
Also used : QueryProfile(com.yahoo.search.query.profile.QueryProfile) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) NamedReader(com.yahoo.io.reader.NamedReader) QueryProfileType(com.yahoo.search.query.profile.types.QueryProfileType) ComponentId(com.yahoo.component.ComponentId)

Example 9 with NamedReader

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())));
}
Also used : ArrayList(java.util.ArrayList) NamedReader(com.yahoo.io.reader.NamedReader) PageTemplates(com.yahoo.vespa.model.container.search.PageTemplates) File(java.io.File) Test(org.junit.Test)

Example 10 with NamedReader

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;
}
Also used : PageTemplate(com.yahoo.search.pagetemplates.PageTemplate) Element(org.w3c.dom.Element) NamedReader(com.yahoo.io.reader.NamedReader) ComponentId(com.yahoo.component.ComponentId)

Aggregations

NamedReader (com.yahoo.io.reader.NamedReader)16 ArrayList (java.util.ArrayList)7 Element (org.w3c.dom.Element)4 ComponentId (com.yahoo.component.ComponentId)3 File (java.io.File)3 StringReader (java.io.StringReader)3 ApplicationFile (com.yahoo.config.application.api.ApplicationFile)2 QueryProfileType (com.yahoo.search.query.profile.types.QueryProfileType)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 JarFile (java.util.jar.JarFile)2 Path (com.yahoo.path.Path)1 PageTemplate (com.yahoo.search.pagetemplates.PageTemplate)1 PageTemplateXMLReader (com.yahoo.search.pagetemplates.config.PageTemplateXMLReader)1 QueryProfile (com.yahoo.search.query.profile.QueryProfile)1 ParseException (com.yahoo.searchdefinition.parser.ParseException)1 PageTemplates (com.yahoo.vespa.model.container.search.PageTemplates)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Reader (java.io.Reader)1 URISyntaxException (java.net.URISyntaxException)1