Static utility method for facilitating writes on input object

@param parent the source object
@param matchedElement the current spec (leaf) element that was matched with input
@param value to write
@param opMode to determine if write is applicable
1678
@SuppressWarnings( "unchecked" )
    protected static void setData(Object parent, MatchedElement matchedElement, Object value, OpMode opMode) {
        if(parent instanceof Map) {
            Map source = (Map) parent;
            String key = matchedElement.getRawKey();
            if(opMode.isApplicable( source, key )) {
                source.put( key, value );
            }
        }
        else if (parent instanceof List && matchedElement instanceof ArrayMatchedElement ) {
            List source = (List) parent;
            int origSize = ( (ArrayMatchedElement) matchedElement ).getOrigSize();
            int reqIndex = ( (ArrayMatchedElement) matchedElement ).getRawIndex();
            if(opMode.isApplicable( source, reqIndex, origSize )) {
                source.set( reqIndex, value );
            }
        }
        else {
            throw new RuntimeException( "Should not come here!" );
        }
    }
580884
public void writeResult(Map<BioPAXElement, List<Match>> matches, OutputStream out)
		throws IOException
	{
		writeResultDetailed(matches, out, 5);
	}
541598
@SuppressWarnings("unchecked")
   protected void replaceXpp3DOM(final Element parent, final Xpp3Dom parentDom, final Counter counter)
   {
      if (parentDom.getChildCount() > 0)
      {
         Xpp3Dom[] childs = parentDom.getChildren();
         Collection<Xpp3Dom> domChilds = new ArrayList<>();
         for (int i = 0; i < childs.length; i++)
         {
            domChilds.add(childs[i]);
         }

         ListIterator<Element> it = parent.getChildren().listIterator();
         while (it.hasNext())
         {
            Element elem = it.next();
            Iterator<Xpp3Dom> it2 = domChilds.iterator();
            Xpp3Dom corrDom = null;
            while (it2.hasNext())
            {
               Xpp3Dom dm = it2.next();
               if (dm.getName().equals(elem.getName()))
               {
                  corrDom = dm;
                  break;
               }
            }
            if (corrDom != null)
            {
               domChilds.remove(corrDom);
               replaceXpp3DOM(elem, corrDom, new Counter(counter.getDepth() + 1));
               counter.increaseCount();
            }
            else
            {
               it.remove();
            }
         }

         Iterator<Xpp3Dom> it2 = domChilds.iterator();
         while (it2.hasNext())
         {
            Xpp3Dom dm = it2.next();
            Element elem = factory.element(dm.getName(), parent.getNamespace());
            for (String attName : dm.getAttributeNames())
            {
               elem.setAttribute(attName, dm.getAttribute(attName));
            }
            insertAtPreferredLocation(parent, elem, counter);
            counter.increaseCount();
            replaceXpp3DOM(elem, dm, new Counter(counter.getDepth() + 1));
         }
      }
      else if (parentDom.getValue() != null)
      {
         parent.setText(parentDom.getValue());
      }
   }
1277587
function(parent, text)
	{
		var p = document.createElement('p');
		mxUtils.write(p, text);

		if (parent != null)
		{
			parent.appendChild(p);
		}
		
		return p;
	}
626294
public void write(Writer out)
         throws IOException
    {
        for (int i=0; i <elements.size() ; i++)
        {
            Object element = elements.get(i);
          
            if (element instanceof Element)
                ((Element)element).write(out);
            else if (element==null)
                out.write("null");
            else 
                out.write(element.toString());
        }
    }
253159
@Override
    public void writeContent(XMLExtendedStreamWriter writer, ModuleConfig value) throws XMLStreamException {

        writer.writeStartDocument();
        writer.writeStartElement(MODULE);
        writer.writeDefaultNamespace(MODULE_NS);

        if(moduleName == null) {
            throw new XMLStreamException("Module name is missing.");
        }
        writer.writeAttribute(NAME, moduleName);

        if (slotName != null) {
            writer.writeAttribute(SLOT, slotName);
        }

        if(properties != null) {
            writeNewLine(writer);
            writer.writeStartElement(PROPERTIES);
            for(Map.Entry<String, String> entry: properties.entrySet()) {
                writer.writeStartElement(PROPERTY);
                writer.writeAttribute(NAME, entry.getKey());
                writer.writeAttribute(VALUE, entry.getValue());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        if(mainClass != null) {
            writeNewLine(writer);
            writer.writeStartElement(MAIN_CLASS);
            writer.writeAttribute(NAME, mainClass);
            writer.writeEndElement();
        }

        if(resources != null) {
            writeNewLine(writer);
            writer.writeStartElement(RESOURCES);
            for(Resource res : resources) {
                res.writeContent(writer, res);
            }
            writer.writeEndElement();
        }

        if(dependencies != null) {
            writeNewLine(writer);
            writer.writeStartElement(DEPENDENCIES);
            for(Dependency dep : dependencies) {
                dep.writeContent(writer, dep);
            }
            writer.writeEndElement();
        }

        writeNewLine(writer);
        writer.writeEndElement();
        writer.writeEndDocument();
    }
1036756
private void parseAnnotatableText(String text, Element parent) {
	  AnnotationNode annotation = null;
	  Matcher matcher = AnnotationParser.WIDGET_ANNOTATION_REGEX.matcher(text);

	  int previousEnd = 0;
	  while (matcher.find()){
		  int start = matcher.start();

		  // build a new text node for what is between last index and current annotation
		  if (start > previousEnd)	{
			  String segment = text.substring(previousEnd, start);
			  // ignore empty white space
			  if (segment.trim().length() > 0){
				  addTextNodeToParent (segment, parent, annotation);
				  annotation = null;
			  }
		  }

		  // parse the annotation
		  String annotationText = matcher.group().trim();
		  if (null != annotationText) {
		      annotation = new AnnotationNode(annotationText);
		      lines(annotation, annotationText);
		  }
		  previousEnd = matcher.end();
	  }
	  
	  // handle leftover text if we parsed some segment
	  if (previousEnd > 0 && previousEnd < text.length()){
		  String segment = text.substring(previousEnd);
		  if (segment.trim().length() > 0){
			  addTextNodeToParent (segment, parent, annotation);
			  annotation = null;
		  }
	  }
	  
	  // store the remaining annotation for use by whatever is parsed next
	  if (annotation != null)
		  add(annotation);
	  
	  // handle no annotations being found
	  if (previousEnd == 0){
		  Node dataNode;
		  if (parent.tagName().equals(titleTag) || parent.tagName().equals(textareaTag))
	        dataNode = TextNode.createFromEncoded(text, baseUri);
	      else // data not encoded but raw (for " in script)
	        dataNode = new DataNode(text, baseUri);
	      lines(dataNode, text);
	      
	      if (pendingAnnotation != null)
	          pendingAnnotation.apply(dataNode);
	      
		  // put the text node on the parent
		  parent.appendChild(dataNode);
	  }
  }
490900
public static void rewriteContent(@NotNull Element parent, @NotNull RewriteContentHandler rewriteContentHandler) {

    // iterate through content list and build new content list
    List<Content> originalContent = parent.getContent();
    List<Content> newContent = new ArrayList<Content>();
    for (Content contentElement : originalContent) {

      // handle element
      if (contentElement instanceof Element) {
        Element element = (Element)contentElement;

        // check if rewrite is needed for element
        List<Content> rewriteContent = rewriteContentHandler.rewriteElement(element);
        if (rewriteContent != null) {
          // element was removed
          if (rewriteContent.isEmpty()) {
            // do not add to newContent
          }

          // element is the same - rewrite child elements
          else if (rewriteContent.size() == 1 && rewriteContent.get(0) == element) { //NOPMD
            rewriteContent(element, rewriteContentHandler);
            newContent.add(element);
          }

          // element was replaced with other content - rewrite and add instead
          else {
            for (Content newContentItem : rewriteContent) {
              if (newContentItem instanceof Element) {
                Element newElement = (Element)newContentItem;
                rewriteContent(newElement, rewriteContentHandler);
              }
              newContent.add(newContentItem.clone());
            }
          }
        }

        // nothing to rewrite - do nothing, but rewrite child element
        else {
          rewriteContent(element, rewriteContentHandler);
          newContent.add(element);
        }

      }

      // handle text node
      else if (contentElement instanceof Text) {
        Text text = (Text)contentElement;

        // check if rewrite is needed for text node
        List<Content> rewriteContent = rewriteContentHandler.rewriteText(text);
        if (rewriteContent != null) {
          // element was removed
          if (rewriteContent.isEmpty()) {
            // do not add to newContent
          }

          // element is the same - ignore
          else if (rewriteContent.size() == 1 && rewriteContent.get(0) == text) { //NOPMD
            // add original element
            newContent.add(text);
          }

          // element was replaced with other content - add instead
          else {
            for (Content newContentItem : rewriteContent) {
              newContent.add(newContentItem.clone());
            }
          }
        }

        // nothing to rewrite - do nothing, but add original text element
        else {
          newContent.add(text);
        }

      }

      // unknown element - just add to new content
      else {
        newContent.add(contentElement);
      }

    }

    // replace original content with new content
    parent.removeContent();
    parent.addContent(newContent);

  }
229627
private int writeExtensions(
      TypeElement type,
      ExtensionContext context,
      ImmutableList<AutoValueExtension> applicableExtensions) {
    int writtenSoFar = 0;
    for (AutoValueExtension extension : applicableExtensions) {
      String parentFqName = generatedSubclassName(type, writtenSoFar + 1);
      String parentSimpleName = TypeSimplifier.simpleNameOf(parentFqName);
      String classFqName = generatedSubclassName(type, writtenSoFar);
      String classSimpleName = TypeSimplifier.simpleNameOf(classFqName);
      boolean isFinal = (writtenSoFar == 0);
      String source = extension.generateClass(context, classSimpleName, parentSimpleName, isFinal);
      if (source != null) {
        source = Reformatter.fixup(source);
        writeSourceFile(classFqName, source, type);
        writtenSoFar++;
      }
    }
    return writtenSoFar;
  }
1077996
public void write(Object object, Object value) {
        if(object instanceof Map)
            mapUpdate((Map)object, _identifier, value);
        else beanUpdate(object, _identifier, value);
    }
319443
public void readChild(Element element, PrintWriter ps, int level,
			String name) {
		String nbTab = tabMaker(level);

		if (element instanceof Resource) {
			Resource resource = (Resource) element;

			writeBegin(ps, nbTab, name, level, element.getTypeAsString());

			for (Resource.Entry entry : resource) {
				Property key = entry.getKey();
				Element value = entry.getValue();

				level++;
				readChild(value, ps, level, key.toString());
				level--;
			}

			writeEnd(ps, nbTab, element.getTypeAsString());

		} else {
			Property elem = (Property) element;
			writeProperties(ps, nbTab, name, element.getTypeAsString(),
					elem.toString());
		}
	}
1034076
public static void write(JsonElement element, JsonWriter writer) throws IOException {
    TypeAdapters.JSON_ELEMENT.write(writer, element);
  }
67129
function _matchesSelector (element, selector, boundElement) {
  // no selector means this event was bound directly to this element
  if (selector === '_root') {
    return boundElement;
  }

  // if we have moved up to the element you bound the event to
  // then we have come too far
  if (element === boundElement) {
    return;
  }

  if (_getMatcher(element).call(element, selector)) {
    return element;
  }

  // if this element did not match but has a parent we should try
  // going up the tree to see if any of the parent elements match
  // for example if you are looking for a click on an <a> tag but there
  // is a <span> inside of the a tag that it is the target,
  // it should still work
  if (element.parentNode) {
    _level++;
    return _matchesSelector(element.parentNode, selector, boundElement);
  }
}
154674
protected void writeElementContent(char[] buf, int off, int len, Writer out) throws IOException {
		// esccape '<', '&', ']]>'
		final int end = off + len;
		int pos = off;
		for (int i = off; i < end; i++) {
			final char ch = buf[i];
			if (ch == ']') {
				if (seenBracket) {
					seenBracketBracket = true;
				} else {
					seenBracket = true;
				}
			} else {
				if (ch == '&') {
					if (i > pos) {
						out.write(buf, pos, i - pos);
					}
					out.write("&amp;");
					pos = i + 1;
				} else if (ch == '<') {
					if (i > pos) {
						out.write(buf, pos, i - pos);
					}
					out.write("&lt;");
					pos = i + 1;

				} else if (seenBracketBracket && ch == '>') {
					if (i > pos) {
						out.write(buf, pos, i - pos);
					}
					out.write("&gt;");
					pos = i + 1;
				} else if (ch < 32) {
					// in XML 1.0 only legal character are #x9 | #xA | #xD
					if (ch == 9 || ch == 10 || ch == 13) {
						// pass through

						// } else if(ch == 13 ) { //if(ch == '\r') {
						// if(i > pos) {
						// out.write(buf, pos, i - pos);
						// }
						// out.write("&#");
						// out.write(Integer.toString(ch));
						// out.write(';');
						// pos = i + 1;
					} else {
						if (TRACE_ESCAPING)
							System.err.println(getClass().getName() + " DEBUG TEXT value.len=" + len + " " + printable(new String(buf, off, len)));
						throw new IllegalStateException("character " + printable(ch) + " (" + Integer.toString(ch) + ") is not allowed in output" + getLocation());
						// in XML 1.1 legal are [#x1-#xD7FF]
						// if(ch > 0) {
						// if(i > pos) out.write(text.substring(pos, i));
						// out.write("&#");
						// out.write(Integer.toString(ch));
						// out.write(';');
						// pos = i + 1;
						// } else {
						// throw new IllegalStateException(
						// "character zero is not allowed in XML 1.1
						// output"+getLocation());
						// }
					}
				}
				if (seenBracket) {
					seenBracketBracket = seenBracket = false;
				}
				// assert seenBracketBracket == seenBracket == false;
			}
		}
		if (end > pos) {
			out.write(buf, pos, end - pos);
		}
	}
847647
private void writeObject(Class<?> declaredType, Object obj, SerIterator parentIterator) throws IOException {
        if (obj == null) {
            output.writeNull();
        } else if (settings.getConverter().isConvertible(obj.getClass())) {
            writeSimple(declaredType, obj);
        } else if (obj instanceof Bean) {
            writeBean((Bean) obj, declaredType);
        } else if (parentIterator != null) {
            SerIterator childIterator = settings.getIteratorFactory().createChild(obj, parentIterator);
            if (childIterator != null) {
                writeElements(childIterator);
            } else {
                writeSimple(declaredType, obj);
            }
        } else {
            writeSimple(declaredType, obj);
        }
    }
1145965
protected void writeElementContent(String text, Writer out) throws IOException {
		// esccape '<', '&', ']]>', <32 if necessary
		int pos = 0;
		for (int i = 0; i < text.length(); i++) {
			// TODO: check if doing char[] text.getChars() would be faster than
			// getCharAt(i) ...
			char ch = text.charAt(i);
			if (ch == ']') {
				if (seenBracket) {
					seenBracketBracket = true;
				} else {
					seenBracket = true;
				}
			} else {
				if (ch == '&') {
					if (i > pos)
						out.write(text.substring(pos, i));
					out.write("&amp;");
					pos = i + 1;
				} else if (ch == '<') {
					if (i > pos)
						out.write(text.substring(pos, i));
					out.write("&lt;");
					pos = i + 1;
				} else if (seenBracketBracket && ch == '>') {
					if (i > pos)
						out.write(text.substring(pos, i));
					out.write("&gt;");
					pos = i + 1;
				} else if (ch < 32) {
					// in XML 1.0 only legal character are #x9 | #xA | #xD
					if (ch == 9 || ch == 10 || ch == 13) {
						// pass through

						// } else if(ch == 13) { //escape
						// if(i > pos) out.write(text.substring(pos, i));
						// out.write("&#");
						// out.write(Integer.toString(ch));
						// out.write(';');
						// pos = i + 1;
					} else {
						if (TRACE_ESCAPING)
							System.err.println(getClass().getName() + " DEBUG TEXT value.len=" + text.length() + " " + printable(text));
						throw new IllegalStateException("character " + Integer.toString(ch) + " is not allowed in output" + getLocation() + " (text value=" + printable(text) + ")");
						// in XML 1.1 legal are [#x1-#xD7FF]
						// if(ch > 0) {
						// if(i > pos) out.write(text.substring(pos, i));
						// out.write("&#");
						// out.write(Integer.toString(ch));
						// out.write(';');
						// pos = i + 1;
						// } else {
						// throw new IllegalStateException(
						// "character zero is not allowed in XML 1.1
						// output"+getLocation());
						// }
					}
				}
				if (seenBracket) {
					seenBracketBracket = seenBracket = false;
				}

			}
		}
		if (pos > 0) {
			out.write(text.substring(pos));
		} else {
			out.write(text); // this is shortcut to the most common case
		}

	}
670878
public static void writeOutSilent(Expression value, BytecodeContext bc, int mode) throws TransformerException {
	Position start = value.getStart();
	Position end = value.getEnd();
	value.setStart(null);
	value.setEnd(null);
	value.writeOut(bc, mode);
	value.setStart(start);
	value.setEnd(end);
    }