Parses the input stream and returns DocumentType built without loading
any external DTD schemas.
@param body The body to be parsed.
@return The documents DocumentType.
@throws IOException if something goes wrong.
432309
private static DocumentType getDocType(final InputStream body)
        throws IOException {
        final DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        try {
            factory.setFeature(RsPrettyXml.LOAD_EXTERNAL_DTD, false);
            final DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(body).getDoctype();
        } catch (final ParserConfigurationException | SAXException ex) {
            throw new IOException(ex);
        }
    }
124867
protected Document responseToDoc(String response){
        Document doc = null;
        try {
            DocumentBuilderFactory factory = new DocumentBuilderFactoryImpl();
            DocumentBuilderFactory.newInstance();
            DocumentBuilder builder =  factory.newDocumentBuilder();
            ByteArrayInputStream input =  new ByteArrayInputStream(response.getBytes("UTF-8"));
            doc = builder.parse(input);
        } catch (ParserConfigurationException e) {
            LOG.error("ParserConfigurationException", e);
        } catch (UnsupportedEncodingException e) {
            LOG.error("UnsupportedEncodingException", e);
        } catch (IOException e) {
            LOG.error("IOException", e);
        } catch (SAXException e) {
            LOG.error("SAXException", e);
        }

        return doc;
    }
802101
@NotNull
    public static Document parseDocument(@NotNull final DocumentBuilder builder,
            @NotNull final InputStream inputStream) {
        Contract.requireArgNotNull("builder", builder);
        Contract.requireArgNotNull("inputStream", inputStream);
        try {
            return builder.parse(inputStream);
        } catch (final SAXException | IOException ex) {
            throw new RuntimeException("Failed to parse XML", ex);
        }
    }
34336
final Document getPayloadDocument() throws ParserConfigurationException,
                                             IOException {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.entry(this, tc, "getPayloadDocument");

    Document domDoc = null;

    // If we already have a Document cached we don't need to build it again
    if (softRefToDocument != null) {
      domDoc = softRefToDocument.get();
    }

    // Otherwise we'll take the hit of building it, and cache it in a soft reference
    if (domDoc == null) {
      byte[] bytes = getBytes();
      if ((bytes != null) && (bytes.length > 0)) {
        DocumentBuilderFactory domFactory =DocumentBuilderFactory.newInstance();
        // We need to tell the factory that we want it to be namespace aware.
        domFactory.setNamespaceAware(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        InputStream is = new ByteArrayInputStream(bytes);
        try {
          domDoc = builder.parse(is);
          softRefToDocument = new SoftReference<Document>(domDoc);
        }
        catch (SAXException e) {
          // No FFDC code needed
          // This is not really an error - it just means the data is not XML.
          // As the XPath Expression can't evaluate to True we just set the document to null;
          domDoc = null;
        }
      }
      else {
        domDoc = null;
      }
    }

    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled()) SibTr.exit(this, tc, "getPayloadDocument", domDoc);
    return domDoc;
  }
344752
@GET
    @Produces(MediaType.TEXT_PLAIN)
    public InputStream doc() throws IOException {
        //noinspection ConstantConditions
        return getClass().getClassLoader().getResource("SnomedCoderService_help.txt").openStream();
    }
252363
public static Map<String, Object> bodyOfSelectedRevision(Document doc) {
        byte[] body;
        try {
            body = doc.getSelectedBody();
        } catch (ForestException e) {
            return null;
        }
        Map<String, Object> properties = null;
        if (body != null && body.length > 0) {
            try {
                properties = Manager.getObjectMapper().readValue(body, Map.class);
            } catch (IOException e) {
                Log.w(TAG, "Failed to parse body: [%s]", new String(body));
            }
        }
        return properties;
    }
1179606
private void readFromStream(InputStream in) throws IOException {
        document = DocumentFactory.newDocument(Json.read(in));
    }
176438
private static byte[] transform(final InputStream body) throws IOException {
        final SAXSource source = new SAXSource(new InputSource(body));
        final ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            final XMLReader xmlreader = SAXParserFactory.newInstance()
                .newSAXParser().getXMLReader();
            source.setXMLReader(xmlreader);
            xmlreader.setFeature(
                RsPrettyXml.LOAD_EXTERNAL_DTD, false
            );
            final String yes = "yes";
            final Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();
            // @checkstyle MultipleStringLiteralsCheck (2 line)
            transformer.setOutputProperty(
                OutputKeys.OMIT_XML_DECLARATION, yes
            );
            RsPrettyXml.prepareDocType(body, transformer);
            transformer.setOutputProperty(OutputKeys.INDENT, yes);
            transformer.transform(source, new StreamResult(result));
        } catch (final TransformerException
            | ParserConfigurationException
            | SAXException ex) {
            throw new IOException(ex);
        }
        return result.toByteArray();
    }
60878
private Type getType(Map<String, Model> definitions, DocumentResolver definitionDocumentResolver) {
        Validate.notNull(parameter, "parameter must not be null!");
        Type type = null;

        if (parameter instanceof BodyParameter) {
            BodyParameter bodyParameter = (BodyParameter) parameter;
            Model model = bodyParameter.getSchema();

            if (model != null) {
                type = ModelUtils.getType(model, definitions, definitionDocumentResolver);
            } else {
                type = new BasicType("string", bodyParameter.getName());
            }

        } else if (parameter instanceof AbstractSerializableParameter) {
            AbstractSerializableParameter serializableParameter = (AbstractSerializableParameter) parameter;
            @SuppressWarnings("unchecked")
            List<String> enums = serializableParameter.getEnum();

            if (CollectionUtils.isNotEmpty(enums)) {
                type = new EnumType(serializableParameter.getName(), enums);
            } else {
                type = new BasicType(serializableParameter.getType(), serializableParameter.getName(), serializableParameter.getFormat());
            }
            if (serializableParameter.getType().equals("array")) {
                String collectionFormat = serializableParameter.getCollectionFormat();

                type = new ArrayType(serializableParameter.getName(), new PropertyAdapter(serializableParameter.getItems()).getType(definitionDocumentResolver), collectionFormat);
            }
        } else if (parameter instanceof RefParameter) {
            String refName = ((RefParameter) parameter).getSimpleRef();

            type = new RefType(definitionDocumentResolver.apply(refName), new ObjectType(refName, null /* FIXME, not used for now */));
        }
        return type;
    }
804530
@SuppressWarnings("unchecked")
    public <T> T deserialize(String body, Type returnType) {
        try {
            return gson.fromJson(body, returnType);
        } catch (JsonParseException e) {
            // Fallback processing when failed to parse JSON form response body:
            // return the response body string directly for the String return
            // type;
            // parse response body into date or datetime for the Date return
            // type.
            if (returnType.equals(String.class))
                return (T) body;
            // else if (returnType.equals(Date.class))
            // return (T) apiClient.parseDateOrDatetime(body);
            else
                throw (e);
        }
    }
772350
public void processDocument(BufferedReader document) throws IOException {
        int docIndex = documentCounter.getAndIncrement();

        Iterator<String> documentTokens = 
            IteratorFactory.tokenizeOrdered(document);

        // As we read in the document, generate a compressed version of it,
        // which we will use during the process space method to recompute all of
        // the word vectors' semantics
        ByteArrayOutputStream compressedDocument = 
            new ByteArrayOutputStream(4096);
        DataOutputStream dos = new DataOutputStream(compressedDocument);
        int tokens = 0; // count how many are in this document
        int unfilteredTokens = 0; // how many tokens remained after filtering

        IntegerVector docVector = createVector();
        docToVector.put(docIndex, docVector);

        while (documentTokens.hasNext()) {
            tokens++;
            String focusWord = documentTokens.next();

            // If we are filtering the semantic vectors, check whether this word
            // should have its semantics calculated.  In addition, if there is a
            // filter and it would have excluded the word, do not keep its
            // semantics around
            boolean calculateSemantics =
                semanticFilter.isEmpty() || semanticFilter.contains(focusWord)
                && !focusWord.equals(IteratorFactory.EMPTY_TOKEN);

	    // If the filter does not accept this word, skip the semantic
	    // processing, continue with the next word
            if (!calculateSemantics) {
                // Do not write out any removed tokens to save space
		continue;
	    }

            // Update the occurrences of this token
            unfilteredTokens++;
            add(docVector, getTermIndexVector(focusWord));

            // Update the compress version of the document with the token.
            //
            // NOTE: this call to termToIndex *must* come after the
            // getTermIndexVector() call, which is responsible for adding this
            // mapping if it doesn't already exist.
	    int focusIndex = termToIndex.get(focusWord);

            // write the term index into the compressed for the document for
            // later corpus reprocessing
            dos.writeInt(focusIndex);
        }

        document.close();
        
        dos.close();
        byte[] docAsBytes = compressedDocument.toByteArray();

        // Once the document is finished, write the compressed contents to the
        // corpus stream
        synchronized(compressedDocumentsWriter) {
            // Write how many terms were in this document after filtering
            compressedDocumentsWriter.writeInt(unfilteredTokens);
            compressedDocumentsWriter.write(docAsBytes, 0, docAsBytes.length);
        }
    }
991274
public static Document stringToDoc(String str) throws IOException {
        if (StringUtils.isNotEmpty(str)) {
            try (Reader reader = new StringReader(str)) {
                DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document doc = db.parse(new InputSource(reader));

                return doc;
            } catch (Exception ex) {
                log.debug("String: {}", str);
                throw new IOException(String.format("Error converting from string to doc %s", ex.getMessage()));
            }
        } else {
            throw new IOException("Error - could not convert empty string to doc");
        }
    }
424045
private void parseDoctypedecl() throws Exception {
        String rootName;
        ExternalIdentifiers ids;

        // Read the document type name.
        requireWhitespace();
        rootName = readNmtoken(true);

        // Read the External subset's IDs
        skipWhitespace();
        ids = readExternalIds(false, true);

        // report (a) declaration of name, (b) lexical info (ids)
        handler.doctypeDecl(rootName, ids.publicId, ids.systemId);

        // Internal subset is parsed first, if present
        skipWhitespace();
        if (tryRead('[')) {

            // loop until the subset ends
            while (true) {
                doReport = expandPE = true;
                skipWhitespace();
                doReport = expandPE = false;
                if (tryRead(']')) {
                    break; // end of subset
                } else {
                    // WFC, PEs in internal subset (only between decls)
                    peIsError = expandPE = true;
                    parseMarkupdecl();
                    peIsError = expandPE = false;
                }
            }
        }
        skipWhitespace();
        require('>');

        // Read the external subset, if any
        InputSource subset;

        if (ids.systemId == null) {
            subset = handler.getExternalSubset(rootName, handler.getSystemId());
        } else {
            subset = null;
        }
        if ((ids.systemId != null) || (subset != null)) {
            pushString(null, ">");

            // NOTE: [dtd] is so we say what SAX2 expects,
            // though it's misleading (subset, not entire dtd)
            if (ids.systemId != null) {
                pushURL(true, "[dtd]", ids, null, null, null, true);
            } else {
                handler.warn("modifying document by adding external subset");
                pushURL(true, "[dtd]", new ExternalIdentifiers(
                        subset.getPublicId(), subset.getSystemId(), null),
                        subset.getCharacterStream(), subset.getByteStream(),
                        subset.getEncoding(), false);
            }

            // Loop until we end up back at '>'
            while (true) {
                doReport = expandPE = true;
                skipWhitespace();
                doReport = expandPE = false;
                if (tryRead('>')) {
                    break;
                } else {
                    expandPE = true;
                    parseMarkupdecl();
                    expandPE = false;
                }
            }

            // the ">" string isn't popped yet
            if (inputStack.size() != 1) {
                fatal("external subset has unmatched '>'");
            }
        }

        // done dtd
        handler.endDoctype();
        expandPE = false;
        doReport = true;
    }
297989
public static DomDocument parseInputStream(DocumentBuilderFactory documentBuilderFactory, InputStream inputStream) {

    try {
      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
      documentBuilder.setErrorHandler(new DomErrorHandler());
      return new DomDocumentImpl(documentBuilder.parse(inputStream));
    } catch (ParserConfigurationException e) {
      throw new ModelParseException("ParserConfigurationException while parsing input stream", e);

    } catch (SAXException e) {
      throw new ModelParseException("SAXException while parsing input stream", e);

    } catch (IOException e) {
      throw new ModelParseException("IOException while parsing input stream", e);

    }
  }
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;
  }
338926
protected Map<String, Object> readDocument() throws IOException {
		ObjectCodec codec = getCodec();
		if (codec == null) {
			throw new IllegalStateException("Could not parse embedded document " +
					"because BSON parser has no codec");
		}
		_currToken = handleNewDocument(false);
		return codec.readValue(this, new TypeReference<Map<String, Object>>() {});
	}
1353022
private InternalDocumentRevision createDocumentBody(SQLDatabase db, String docId, final DocumentBody
            body)
            throws AttachmentException, ConflictException, DocumentStoreException {
        Misc.checkState(this.isOpen(), "Database is closed");
        CouchUtils.validateDocumentId(docId);
        Misc.checkNotNull(body, "Input document body");
        this.validateDBBody(body);

        // check if the docid exists first:

        // if it does exist:
        // * if winning leaf deleted, root the 'created' document there
        // * else raise error
        // if it does not exist:
        // * normal insert logic for a new document

        InsertRevisionCallable callable = new InsertRevisionCallable();
        InternalDocumentRevision potentialParent = null;

        try {
            potentialParent = new GetDocumentCallable(docId, null, this.attachmentsDir, this.attachmentStreamFactory).call(db);
        } catch (DocumentNotFoundException e) {
            //this is an expected exception, it just means we are
            // resurrecting the document
        }

        if (potentialParent != null) {
            if (!potentialParent.isDeleted()) {
                // current winner not deleted, can't insert
                throw new ConflictException(String.format("Cannot create doc, document with id %s" +
                        " already exists "
                        , docId));
            }
            // if we got here, parent rev was deleted
            new SetCurrentCallable(potentialParent.getSequence(), false).call(db);
            callable.revId = CouchUtils.generateNextRevisionId(potentialParent.getRevision());
            callable.docNumericId = potentialParent.getInternalNumericId();
            callable.parentSequence = potentialParent.getSequence();
        } else {
            // otherwise we are doing a normal create document
            long docNumericId = new InsertDocumentIDCallable(docId).call(db);
            callable.revId = CouchUtils.getFirstRevisionId();
            callable.docNumericId = docNumericId;
            callable.parentSequence = -1l;
        }
        callable.deleted = false;
        callable.current = true;
        callable.data = body.asBytes();
        callable.available = true;
        callable.call(db);

        try {
            InternalDocumentRevision doc = new GetDocumentCallable(docId, callable.revId, this.attachmentsDir, this.attachmentStreamFactory).call(db);
            logger.finer("New document created: " + doc.toString());
            return doc;
        } catch (DocumentNotFoundException e) {
            throw new RuntimeException(String.format("Could not get document we just inserted " +
                    "(id: %s); this should not happen, please file an issue with as much detail " +
                    "as possible.", docId), e);
        }

    }