예제 #1
0
  private void readSchema(final InputStream is) {
    log.info("Reading Solr Schema");

    try {
      // pass the config resource loader to avoid building an empty one for no reason:
      // in the current case though, the stream is valid so we wont load the resource by name
      final Config schemaConf = new Config(loader, "schema", is, "/schema/");
      final Document document = schemaConf.getDocument();
      final XPath xpath = schemaConf.getXPath();
      final List<SchemaAware> schemaAware = new ArrayList<SchemaAware>();
      final Node nd = (Node) xpath.evaluate("/schema/@name", document, XPathConstants.NODE);
      if (nd == null) {
        log.warn("schema has no name!");
      } else {
        name = nd.getNodeValue();
        log.info("Schema name=" + name);
      }

      version = schemaConf.getFloat("/schema/@version", 1.0f);

      final AbstractPluginLoader<FieldType> fieldLoader =
          new AbstractPluginLoader<FieldType>("[schema.xml] fieldType", true, true) {

            @Override
            protected FieldType create(
                final ResourceLoader loader,
                final String name,
                final String className,
                final Node node)
                throws Exception {
              final FieldType ft = (FieldType) loader.newInstance(className);
              if (!(ft instanceof SubTextField)) {
                throw new SolrException(ErrorCode.FORBIDDEN, "Subschema must use SubTextField");
              }
              ((SubTextField) ft).setTypeName(name);

              String expression = "./analyzer[@type='query']";
              Node anode = (Node) xpath.evaluate(expression, node, XPathConstants.NODE);
              Analyzer queryAnalyzer = SubIndexSchema.this.readAnalyzer(anode);

              // An analyzer without a type specified, or with type="index"
              expression = "./analyzer[not(@type)] | ./analyzer[@type='index']";
              anode = (Node) xpath.evaluate(expression, node, XPathConstants.NODE);
              Analyzer analyzer = SubIndexSchema.this.readAnalyzer(anode);

              if (queryAnalyzer == null) queryAnalyzer = analyzer;
              if (analyzer == null) analyzer = queryAnalyzer;
              if (analyzer != null) {
                ft.setAnalyzer(analyzer);
                ft.setQueryAnalyzer(queryAnalyzer);
              }
              return ft;
            }

            @Override
            protected void init(final FieldType plugin, final Node node) throws Exception {}

            @Override
            protected FieldType register(final String name, final FieldType plugin)
                throws Exception {
              log.trace("fieldtype defined: " + plugin);
              return fieldTypes.put(name, plugin);
            }
          };

      final String expression = "/schema/types/fieldtype | /schema/types/fieldType";
      final NodeList nodes =
          (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
      fieldLoader.load(loader, nodes);
    } catch (final SolrException e) {
      SolrConfig.severeErrors.add(e);
      throw e;
    } catch (final Exception e) {
      // unexpected exception...
      SolrConfig.severeErrors.add(e);
      throw new SolrException(
          SolrException.ErrorCode.SERVER_ERROR, "Schema Parsing Failed", e, false);
    }
  }