/** * if no search xml exist create a empty one * * @param searchFile * @throws IOException */ private static final void createSearchFile(Resource searchFile) throws IOException { CFMLEngine e = CFMLEngineFactory.getInstance(); if (searchFile.isFile()) searchFile.createFile(true); InputStream in = new ByteArrayInputStream(DEFAULT_SEARCH_XML.getBytes()); // e.getClass().getResourceAsStream("/resource/search/default.xml"); e.getIOUtil().copy(in, searchFile, true); }
/** * store loaded data to xml file * * @throws SearchException */ protected final synchronized void store() throws SearchException { // Collection.Key[] keys=collections.keys(); Iterator<Key> it = collections.keyIterator(); Key k; while (it.hasNext()) { k = it.next(); Element collEl = getCollectionElement(k.getString()); SearchCollection sc = getCollectionByName(k.getString()); setAttributes(collEl, sc); } OutputFormat format = new OutputFormat(doc, null, true); format.setLineSeparator("\r\n"); format.setLineWidth(72); OutputStream os = null; try { XMLSerializer serializer = new XMLSerializer( os = engine.getIOUtil().toBufferedOutputStream(searchFile.getOutputStream()), format); serializer.serialize(doc.getDocumentElement()); } catch (IOException e) { throw new SearchException(e); } finally { engine.getIOUtil().closeSilent(os); } }
@Override public final Query getCollectionsAsQuery() { final String v = "VARCHAR"; Query query = null; String[] cols = new String[] { "external", "language", "mapped", "name", "online", "path", "registered", "lastmodified", "categories", "charset", "created", "size", "doccount" }; String[] types = new String[] { "BOOLEAN", v, "BOOLEAN", v, "BOOLEAN", v, v, "DATE", "BOOLEAN", v, "OBJECT", "DOUBLE", "DOUBLE" }; try { query = engine.getCreationUtil().createQuery(cols, types, collections.size(), "query"); } catch (PageException e) { query = engine.getCreationUtil().createQuery(cols, collections.size(), "query"); } // Collection.Key[] keys = collections.keys(); Iterator<Object> it = collections.valueIterator(); int i = -1; while (it.hasNext()) { i++; try { SearchCollection coll = (SearchCollection) it.next(); query.setAt("external", i + 1, Boolean.FALSE); query.setAt("charset", i + 1, "UTF-8"); query.setAt("created", i + 1, coll.created()); query.setAt("categories", i + 1, Boolean.TRUE); query.setAt("language", i + 1, coll.getLanguage()); query.setAt("mapped", i + 1, Boolean.FALSE); query.setAt("name", i + 1, coll.getName()); query.setAt("online", i + 1, Boolean.TRUE); query.setAt("path", i + 1, coll.getPath().getAbsolutePath()); query.setAt("registered", i + 1, "CF"); query.setAt("lastmodified", i + 1, coll.getLastUpdate()); query.setAt("size", i + 1, new Double(coll.getSize())); query.setAt("doccount", i + 1, new Double(coll.getDocumentCount())); } catch (PageException pe) { } } return query; }
/** * sets all attributes in XML Element from Search Index * * @param el * @param index * @throws SearchException */ protected final void setAttributes(Element el, SearchIndex index) throws SearchException { if (el == null) return; setAttribute(el, "categoryTree", index.getCategoryTree()); setAttribute(el, "category", engine.getListUtil().toList(index.getCategories(), ",")); setAttribute(el, "custom1", index.getCustom1()); setAttribute(el, "custom2", index.getCustom2()); setAttribute(el, "custom3", index.getCustom3()); setAttribute(el, "custom4", index.getCustom4()); setAttribute(el, "id", index.getId()); setAttribute(el, "key", index.getKey()); setAttribute(el, "language", index.getLanguage()); setAttribute(el, "title", index.getTitle()); setAttribute(el, "extensions", engine.getListUtil().toList(index.getExtensions(), ",")); setAttribute(el, "type", SearchIndexImpl.toStringType(index.getType())); setAttribute(el, "urlpath", index.getUrlpath()); setAttribute(el, "query", index.getQuery()); }
@Override public void init(GatewayEngine engine, String id, String cfcPath, Map config) throws GatewayException { this.engine = engine; cfmlEngine = CFMLEngineFactory.getInstance(); caster = cfmlEngine.getCastUtil(); creator = cfmlEngine.getCreationUtil(); this.cfcPath = cfcPath; this.id = id; // config Object oPort = config.get("port"); port = caster.toIntValue(oPort, 1225); Object oWM = config.get("welcomeMessage"); String strWM = caster.toString(oWM, "").trim(); if (strWM.length() > 0) welcomeMessage = strWM; }
/** * removes a Collection from the storage * * @param name Name of the Collection to remove * @throws SearchException */ private final synchronized void removeCollection(String name) throws SearchException { try { collections.remove(engine.getCastUtil().toKey(name)); doc.getDocumentElement().removeChild(getCollectionElement(name)); store(); } catch (PageException e) { throw new SearchException("can't remove collection " + name + ", collection doesn't exist"); } }
/** * read in a single Index * * @param sc * @param el * @throws SearchException * @throws PageException */ protected void readIndex(SearchCollection sc, Element el) throws SearchException { // Index SearchIndex si = new SearchIndexImpl( _attr(el, "id"), _attr(el, "title"), _attr(el, "key"), SearchIndexImpl.toType(_attr(el, "type")), _attr(el, "query"), engine.getListUtil().toStringArray(_attr(el, "extensions"), ","), _attr(el, "language"), _attr(el, "urlpath"), _attr(el, "categoryTree"), engine.getListUtil().toStringArray(_attr(el, "category"), ","), _attr(el, "custom1"), _attr(el, "custom2"), _attr(el, "custom3"), _attr(el, "custom4")); sc.addIndex(si); }
public Struct validate(InputSource xml) throws PageException { CFMLEngine engine = CFMLEngineFactory.getInstance(); warnings = engine.getCreationUtil().createArray(); errors = engine.getCreationUtil().createArray(); fatals = engine.getCreationUtil().createArray(); try { XMLReader parser = new XMLUtilImpl().createXMLReader("org.apache.xerces.parsers.SAXParser"); parser.setContentHandler(this); parser.setErrorHandler(this); parser.setEntityResolver(this); parser.setFeature("http://xml.org/sax/features/validation", true); parser.setFeature("http://apache.org/xml/features/validation/schema", true); parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); // if(!validateNamespace) if (!Util.isEmpty(strSchema)) parser.setProperty( "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", strSchema); parser.parse(xml); } catch (SAXException e) { } catch (IOException e) { throw engine.getExceptionUtil().createXMLException(e.getMessage()); } // result Struct result = engine.getCreationUtil().createStruct(); result.setEL("warnings", warnings); result.setEL("errors", errors); result.setEL("fatalerrors", fatals); result.setEL("status", engine.getCastUtil().toBoolean(!hasErrors)); release(); return result; }
@Override public void init(lucee.runtime.config.Config config, Resource searchDir) throws IOException, SearchException { this.config = config; this.searchDir = searchDir; this.searchFile = searchDir.getRealResource("search.xml"); if (!searchFile.exists() || searchFile.length() == 0) createSearchFile(searchFile); DOMParser parser = new DOMParser(); InputStream is = null; try { is = engine.getIOUtil().toBufferedInputStream(searchFile.getInputStream()); InputSource source = new InputSource(is); parser.parse(source); } catch (SAXException e) { throw new SearchException(e); } finally { engine.getIOUtil().closeSilent(is); } doc = parser.getDocument(); readCollections(config); }
/** * read in a single collection element * * @param config * @param el * @throws SearchException */ private final void readCollection(Config config, Element el) throws SearchException { SearchCollection sc; // try { // Collection DateTime last = engine .getCastUtil() .toDateTime(el.getAttribute("lastUpdate"), engine.getThreadTimeZone(), null); if (last == null) last = engine.getCreationUtil().now(); DateTime cre = engine .getCastUtil() .toDateTime(el.getAttribute("created"), engine.getThreadTimeZone(), null); if (cre == null) cre = engine.getCreationUtil().now(); ResourceProvider frp = engine.getResourceUtil().getFileResourceProvider(); sc = _readCollection( el.getAttribute("name"), frp.getResource(el.getAttribute("path")), el.getAttribute("language"), last, cre); collections.setEL((sc.getName()), sc); // Indexes NodeList children = el.getChildNodes(); int len = children.getLength(); for (int i = 0; i < len; i++) { Node n = children.item(i); if (n instanceof Element && n.getNodeName().equals("index")) { readIndex(sc, (Element) n); } } /*} catch (PageException e) { throw new SearchException(e); }*/ }
private String _attr(Element el, String attr) { return engine.getStringUtil().emptyIfNull(el.getAttribute(attr)); }
public SearchEngineSupport() { engine = CFMLEngineFactory.getInstance(); collections = engine.getCreationUtil().createStruct(); }