Build facets from document object by extracting all fields and values

@param document Document object

@return List of facets
543533
private List<Facet> buildFacetsFromObject(@NotNull Object document) {
        List<Facet> facets = new ArrayList<>();

        for (Field field : document.getClass().getDeclaredFields()) {
            if (isFacetField(field)) {
                Object fieldValue = getFieldValue(document, field);

                if (fieldValue != null) {
                    List<Serializable> values = transformValue(fieldValue);

                    values.forEach(value -> {
                        Facet key = new Facet(new FacetType(field.getName(), value.getClass()), value);

                        facets.add(key);
                    });
                }
            }
        }

        return facets;
    }
1269460
public SimpleOrderedMap<Object> create(ComponentDocument document,
      Boolean encode) throws IOException {
    SimpleOrderedMap<Object> mtasDocumentResponse = new SimpleOrderedMap<>();
    mtasDocumentResponse.add("key", document.key);
    ArrayList<NamedList<Object>> mtasDocumentItemResponses = new ArrayList<>();
    for (int docId : document.statsData.keySet()) {
      NamedList<Object> mtasDocumentItemResponse = new SimpleOrderedMap<>();
      MtasDataCollector<?, ?> stats = document.statsData.get(docId);
      MtasDataCollector<?, ?> list = null;
      if (document.statsList != null) {
        list = document.statsList.get(docId);
      }
      mtasDocumentItemResponse.add("stats",
          new MtasSolrMtasResult(stats, stats.getDataType(),
              stats.getStatsType(), stats.getStatsItems(), null, null));
      mtasDocumentItemResponse.add("documentKey",
          document.uniqueKey.get(docId));
      if (list != null) {
        if (document.listExpand) {
          mtasDocumentItemResponse.add("list",
              new MtasSolrMtasResult(list,
                  new String[] { list.getDataType(), list.getDataType() },
                  new String[] { list.getStatsType(), list.getStatsType() },
                  new SortedSet[] { list.getStatsItems(),
                      list.getStatsItems() },
                  new List[] { null, null }, new String[] { null, null },
                  new String[] { null, null }, new Integer[] { 0, 0 },
                  new Integer[] { 1, 1 }, null));
        } else {
          mtasDocumentItemResponse.add("list",
              new MtasSolrMtasResult(list, list.getDataType(),
                  list.getStatsType(), list.getStatsItems(), null, null));
        }

      }
      // add
      mtasDocumentItemResponses.add(mtasDocumentItemResponse);
    }
    mtasDocumentResponse.add("list", mtasDocumentItemResponses);
    MtasSolrResultUtil.rewrite(mtasDocumentResponse, searchComponent);
    return mtasDocumentResponse;
  }
850845
public static Attr[] toAttrArray(Document doc, Object o) throws PageException {
	// Node[]
	if (o instanceof Node[]) {
	    Node[] nodes = (Node[]) o;
	    if (_isAllOfSameType(nodes, Node.ATTRIBUTE_NODE)) return (Attr[]) nodes;

	    Attr[] attres = new Attr[nodes.length];
	    for (int i = 0; i < nodes.length; i++) {
		attres[i] = toAttr(doc, nodes[i]);
	    }
	    return attres;
	}
	// Collection
	else if (o instanceof Collection) {
	    Collection coll = (Collection) o;
	    Iterator<Entry<Key, Object>> it = coll.entryIterator();
	    Entry<Key, Object> e;
	    List<Attr> attres = new ArrayList<Attr>();
	    Attr attr;
	    Collection.Key k;
	    while (it.hasNext()) {
		e = it.next();
		k = e.getKey();
		attr = doc.createAttribute(Decision.isNumber(k.getString()) ? "attribute-" + k.getString() : k.getString());
		attr.setValue(Caster.toString(e.getValue()));
		attres.add(attr);
	    }
	    return attres.toArray(new Attr[attres.size()]);
	}
	// Node Map and List
	Node[] nodes = _toNodeArray(doc, o);
	if (nodes != null) return toAttrArray(doc, nodes);
	// Single Text Node
	try {
	    return new Attr[] { toAttr(doc, o) };
	}
	catch (ExpressionException e) {
	    throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Attributes Array");
	}
    }
815238
public static Layout parseDocument(Document document) {
        return new Layout(instance.parseChildren(document, null, Tag.LAYOUT));
    }
199908
function(doc) {
        var way_list = doc.getElementsByTagName("way");
        var return_ways = [];
        for (var i = 0; i < way_list.length; i++) {
            var way = way_list[i];
            var way_object = {
              id: way.getAttribute("id")
            };
            
            way_object.tags = this.getTags(way);
            
            var node_list = way.getElementsByTagName("nd");
            
            way_object.nodes = new Array(node_list.length);
            
            for (var j = 0; j < node_list.length; j++) {
                way_object.nodes[j] = node_list[j].getAttribute("ref");
            }  
            return_ways.push(way_object);
        }
        return return_ways; 
        
    }
839512
private Map<String, Object> documentToPackableMap(Map<String, Object> document) {
        if (compatibleValues) {
            try {
                return jsonMapper.readValue(jsonMapper.writeValueAsBytes(document), new TypeReference<Map<String, Object>>() {
                });
            } catch (Exception e) {
                throw new RuntimeException("Cannot convert document to json and back to Map " + e.getMessage());
            }
        }

        /**
         * A document in MongoDB has a special field "_id" of type ObjectId
         * This object is not "packable" by Neo4jPacker so it must be converted to a value that Neo4j can deal with
         *
         * If the document is not null we simply override the ObjectId instance with its string representation
         */

        if (document != null) {
            Object objectId = document.get("_id");
            if (objectId != null) {
                document.put("_id", objectId.toString());
            }

        }

        return document;
    }
602938
public static DocumentPath from(List<String> elements) {
    List<String> copy = new ArrayList<>();
    copy.add(ROOT_ELEMENT);
    copy.addAll(elements);
    return new DocumentPath(copy);
  }
574564
public Pair<int[][][][], int[][]> documentsToDataAndLabels(Collection<List<IN>> documents) {

    // first index is the number of the document
    // second index is position in the document also the index of the
    // clique/factor table
    // third index is the number of elements in the clique/window these features
    // are for (starting with last element)
    // fourth index is position of the feature in the array that holds them
    // element in data[i][j][k][m] is the index of the mth feature occurring in
    // position k of the jth clique of the ith document
    // int[][][][] data = new int[documentsSize][][][];
    List<int[][][]> data = new ArrayList<int[][][]>();

    // first index is the number of the document
    // second index is the position in the document
    // element in labels[i][j] is the index of the correct label (if it exists)
    // at position j in document i
    // int[][] labels = new int[documentsSize][];
    List<int[]> labels = new ArrayList<int[]>();

    int numDatums = 0;

    for (List<IN> doc : documents) {
      Pair<int[][][], int[]> docPair = documentToDataAndLabels(doc);
      data.add(docPair.first());
      labels.add(docPair.second());
      numDatums += doc.size();
    }

    System.err.println("numClasses: " + classIndex.size() + ' ' + classIndex);
    System.err.println("numDocuments: " + data.size());
    System.err.println("numDatums: " + numDatums);
    System.err.println("numFeatures: " + featureIndex.size());
    printFeatures();

    int[][][][] dataA = new int[0][][][];
    int[][] labelsA = new int[0][];

    return new Pair<int[][][][], int[][]>(data.toArray(dataA), labels.toArray(labelsA));
  }
934273
public static Document read(@NonNull Resource resource) throws IOException {
      Type mapType = new TypeToken<Map<String, Object>>() {
      }.getType();
      Gson gson = new Gson();
      //Load the document to a map
      Map<String, Object> json = gson.fromJson(resource.readToString(), mapType);

      //Create the initial document using raw content
      Document doc = DocumentFactory.getInstance().createRaw(json.getOrDefault("id", Val.NULL).toString(),
                                                             json.getOrDefault("content", "").toString());

      //Add document attributes
      if (json.containsKey("attributes")) {
         Map<String, Object> attributes = Cast.as(json.get("attributes"));
         attributes.forEach((k, av) -> {
            AttributeType attrType = Types.attribute(k);
            Object val = attrType.getValueType().decode(av);
            if (val != null) {
               doc.put(attrType, val);
            }
         });
      }

      //Set completed annotations
      if (json.containsKey("completed")) {
         Map<String, Object> completed = Cast.as(json.get("completed"));
         completed
            .forEach((k, av) -> {
               AnnotatableType type = Types.from(k);
               doc.getAnnotationSet().setIsCompleted(type, true, av.toString());
            });
      }

      //Create the annotations
      AtomicLong maxAnnotationId = new AtomicLong(-1);
      if (json.containsKey("annotations")) {
         List<Object> annotations = Cast.as(json.get("annotations"));
         annotations
            .forEach(v -> {
               Map<String, Object> vv = Cast.as(v);

               //Create new annotation
               Annotation annotation = doc.annotationBuilder()
                                          .type(AnnotationType.create(vv.get("type").toString()))
                                          .start(((Number) vv.get("start")).intValue())
                                          .end(((Number) vv.get("end")).intValue())
                                          .createAttached();

               //Read in and set id and update max id value
               annotation.setId(((Number) vv.get("id")).longValue());
               maxAnnotationId.getAndUpdate(old -> old < annotation.getId() ? annotation.getId() : old);

               //Read in the attributes (map of types and values)
               Cast.<Map<String, Object>>as(vv.getOrDefault("attributes", Collections.emptyMap())).forEach(
                  (k, av) -> {
                     AttributeType attrType = Types.attribute(k);
                     Object val = attrType.getValueType().decode(av);
                     if (val != null) {
                        annotation.put(attrType, val);
                     }
                  });

               if (vv.containsKey("relations")) {
                  //Read in the relations (a list of maps containing type, value, and target)
                  Cast.<List<Object>>as(vv.get("relations"))
                     .stream()
                     .map(Cast::<Map<String, Object>>as)
                     .forEach(rel -> annotation.add(new Relation(RelationType.create(rel.get("type").toString()),
                                                                 rel.get("value").toString(),
                                                                 ((Number) rel.get("target")).longValue())));
               }
            });

         //Set the next annotation id
         doc.idGenerator.set(maxAnnotationId.get() + 1);
      }

      return doc;
   }
1015234
public static List<String> getTopSentenceList(String document, int size)
    {
    	return getTopSentenceList(document, size, default_sentence_separator);
    }
42431
public static Map<String, String> getSchemaLocations(Document doc)
    {
        Set<String> namespaces = new HashSet<>();
        namespaces.addAll(LocationAwareXmlReader.getNamespaces(doc));

        String schemaLocation;
        try
        {
            schemaLocation = XmlUtil.xpathExtract(doc, "//@xsi:schemaLocation", objs);
        }
        catch (MarshallingException e)
        {
            throw new RuntimeException("Exception extracting xpath.", e);
        }

        Map<String, String> result = new HashMap<>();
        if (StringUtils.isNotBlank(schemaLocation))
        {
            schemaLocation = StringUtils.trim(schemaLocation);
            String[] locations = schemaLocation.split("\\s+");
            for (int i = 0, j = locations.length - 1; i < j; i++)
            {
                if (namespaces.contains(locations[i]))
                {
                    result.put(locations[i], locations[i + 1]);
                }
            }
        }

        for (String r : result.keySet())
        {
            namespaces.remove(r);
        }

        // if there are namespaces without namespace locations, load here.
        if (namespaces.size() > 0)
        {
            for (String namespace : namespaces)
            {
                result.put(namespace, null);
            }
        }

        return result;
    }
518800
public static Text[] toTextArray(Document doc, Object o) throws PageException {
	// Node[]
	if (o instanceof Node[]) {
	    Node[] nodes = (Node[]) o;
	    if (_isAllOfSameType(nodes, Node.TEXT_NODE)) return (Text[]) nodes;

	    Text[] textes = new Text[nodes.length];
	    for (int i = 0; i < nodes.length; i++) {
		textes[i] = toText(doc, nodes[i]);
	    }
	    return textes;
	}
	// Collection
	else if (o instanceof Collection) {
	    Collection coll = (Collection) o;
	    Iterator<Object> it = coll.valueIterator();
	    List<Text> textes = new ArrayList<Text>();
	    while (it.hasNext()) {
		textes.add(toText(doc, it.next()));
	    }
	    return textes.toArray(new Text[textes.size()]);
	}
	// Node Map and List
	Node[] nodes = _toNodeArray(doc, o);
	if (nodes != null) return toTextArray(doc, nodes);
	// Single Text Node
	try {
	    return new Text[] { toText(doc, o) };
	}
	catch (ExpressionException e) {
	    throw new XMLException("can't cast Object of type " + Caster.toClassName(o) + " to a XML Text Array");
	}
    }
915256
public void setDocuments(java.util.Collection<DocumentMetadata> documents) {
        if (documents == null) {
            this.documents = null;
            return;
        }

        this.documents = new java.util.ArrayList<DocumentMetadata>(documents);
    }
1337791
@SuppressWarnings("unchecked")
    public static org.bson.Document decodeMongoDoc(org.bson.Document doc) {
        org.bson.Document newDoc = new org.bson.Document();
        for (String key : doc.keySet()) {
            Object value = doc.get(key);
            if (value instanceof org.bson.Document)
                value = decodeMongoDoc(doc.get(key, org.bson.Document.class));
            else if (value instanceof List<?>) {
                for (int i=0; i < ((List<?>)value).size(); i++) {
                    Object obj = ((List<?>)value).get(i);
                    if (obj instanceof org.bson.Document)
                        ((List<org.bson.Document>)value).set(i, decodeMongoDoc((org.bson.Document)obj));
                }
            }

            String newKey = key;
            if (key.startsWith("\\uff04"))
                newKey = "$" + key.substring(6);
            if (key.contains("\\uff0e")) {
                newKey = newKey.replace("\\uff0e", ".");
            }
            newDoc.put(newKey, value);
        }
        return newDoc;
    }
1085758
protected List<String> getDocumentTypes() {

        List<String> names = new ArrayList<String>();
        for (Iterator<I_CmsDocumentFactory> i = m_documentTypes.values().iterator(); i.hasNext();) {
            I_CmsDocumentFactory factory = i.next();
            names.add(factory.getName());
        }
        return names;
    }
1016121
private List<Annotation> getDocMetaData(String document)
  {
    // lookup up meta data in the cache
    if (!docMetaDataCache.containsKey(docBrowserPanel.getCorpus()))
    {
      // get the metadata for the corpus
      WebResource res = Helper.getAnnisWebResource();
      res = res.path("meta/corpus/").path(
        urlPathEscape.escape(docBrowserPanel.getCorpus())).path("closure");
      
      Map<String, List<Annotation>> metaDataMap = new HashMap<>();
      
      // create a document -> metadata map
      for (Annotation a : res.get(new Helper.AnnotationListType()))
      {
        if (a.getAnnotationPath() != null
          && !a.getAnnotationPath().isEmpty()
          && a.getType().equals("DOCUMENT"))
        {
          String docName = a.getAnnotationPath().get(0);
          if (!metaDataMap.containsKey(docName)) {
            metaDataMap.put(docName, new ArrayList<Annotation>());
          }
          metaDataMap.get(docName).add(a);
        }
      }
      docMetaDataCache.put(docBrowserPanel.getCorpus(), metaDataMap);
    }
    
    if (docMetaDataCache.get(docBrowserPanel.getCorpus()).containsKey(document))
    {
      return docMetaDataCache.get(docBrowserPanel.getCorpus()).get(document);
    }
    else
    {
      return new ArrayList<Annotation>();
    }
  }
1359674
public static List<String> extractKeyword(String document, int size)
    {
        return TextRankKeyword.getKeywordList(document, size);
    }