Ejemplo n.º 1
0
 public IDictionary<string, IList<string>> getHeaderFields()
 {
     IDictionary<string, IList<string>> map=new PeterO.Support.LenientDictionary<string, IList<string>>();
     if(this.response is HttpWebResponse)
         map.Add(null,PeterO.Support.Collections.UnmodifiableList(new String[]{GetStatusLine((HttpWebResponse)this.response)}));
     foreach(String k in this.response.Headers.AllKeys){
         map.Add(StringUtility.toLowerCaseAscii(k),PeterO.Support.Collections.UnmodifiableList(this.response.Headers.GetValues(k)));
     }
     return PeterO.Support.Collections.UnmodifiableMap(map);
 }
Ejemplo n.º 2
0
   /**
      *  Replaces certain blank nodes with blank nodes whose
      *  names meet the N-Triples requirements
      * @param triples A set of RDF triples
      * @param bnodeLabels A mapping of blank node names
      * already allocated.  This method will modify this
      * _object as needed to allocate new blank nodes.
      */
   internal static void replaceBlankNodes(ISet<RDFTriple> triples,
 IDictionary<string,RDFTerm> bnodeLabels)
   {
       if(bnodeLabels.Count==0)
         return;
       IDictionary<string,RDFTerm> newBlankNodes=new PeterO.Support.LenientDictionary<string,RDFTerm>();
       IList<RDFTriple[]> changedTriples=new List<RDFTriple[]>();
       int[] nodeindex=new int[]{0};
       foreach(var triple in triples){
         bool changed=false;
         RDFTerm subj=triple.getSubject();
         if(subj.getKind()==RDFTerm.BLANK){
       string oldname=subj.getValue();
       string newname=suggestBlankNodeName(oldname,nodeindex,bnodeLabels);
       if(!newname.Equals(oldname)){
         RDFTerm newNode=newBlankNodes[oldname];
         if(newNode==null){
       newNode=RDFTerm.fromBlankNode(newname);
       bnodeLabels.Add(newname, newNode);
       newBlankNodes.Add(oldname, newNode);
         }
         subj=newNode;
         changed=true;
       }
         }
         RDFTerm obj=triple.getObject();
         if(obj.getKind()==RDFTerm.BLANK){
       string oldname=obj.getValue();
       string newname=suggestBlankNodeName(oldname,nodeindex,bnodeLabels);
       if(!newname.Equals(oldname)){
         RDFTerm newNode=newBlankNodes[oldname];
         if(newNode==null){
       newNode=RDFTerm.fromBlankNode(newname);
       bnodeLabels.Add(newname, newNode);
       newBlankNodes.Add(oldname, newNode);
         }
         obj=newNode;
         changed=true;
       }
         }
         if(changed){
       RDFTriple[] newTriple=new RDFTriple[]{triple,
       new RDFTriple(subj,triple.getPredicate(),obj)
       };
       changedTriples.Add(newTriple);
         }
       }
       foreach(var triple in changedTriples){
         triples.Remove(triple[0]);
         triples.Add(triple[1]);
       }
   }
Ejemplo n.º 3
0
 private void process(IElement node, bool root)
 {
     IList<RDFa.IncompleteTriple> incompleteTriplesLocal=new List<RDFa.IncompleteTriple>();
     string localLanguage=context.language;
     RDFTerm newSubject=null;
     bool recurse=true;
     bool skipElement=false;
     RDFTerm currentObject=null;
     IDictionary<string,string> namespacesLocal=
     new PeterO.Support.LenientDictionary<string,string>(context.namespaces);
     IDictionary<string,string> iriMapLocal=
     new PeterO.Support.LenientDictionary<string,string>(context.iriMap);
     string attr=null;
     if(!xhtml){
       attr=node.getAttribute("xml:base");
       if(attr!=null){
     context.baseURI=URIUtility.relativeResolve(attr, context.baseURI);
       }
     }
     // Support XML namespaces
     foreach(var attrib in node.getAttributes()){
       string name=StringUtility.toLowerCaseAscii(attrib.getName());
       //Console.WriteLine(attrib);
       if(name.Equals("xmlns")){
     //Console.WriteLine("xmlns %s",attrib.getValue());
     iriMapLocal.Add("", attrib.getValue());
     namespacesLocal.Add("", attrib.getValue());
       } else if(name.StartsWith("xmlns:",StringComparison.Ordinal) && name.Length>6){
     string prefix=name.Substring(6);
     //Console.WriteLine("xmlns %s %s",prefix,attrib.getValue());
     if(!"_".Equals(prefix)){
       iriMapLocal.Add(prefix, attrib.getValue());
     }
     namespacesLocal.Add(prefix, attrib.getValue());
       }
     }
     attr=node.getAttribute("xml:lang");
     if(attr!=null){
       localLanguage=attr;
     }
     // Support RDF/XML metadata
     if(node.getLocalName().Equals("RDF") &&
     RDF_NAMESPACE.Equals(node.getNamespaceURI())){
       miniRdfXml(node,context);
       return;
     }
     string rel=node.getAttribute("rel");
     string rev=node.getAttribute("rev");
     string property=node.getAttribute("property");
     string content=node.getAttribute("content");
     string datatype=node.getAttribute("datatype");
     if(rel==null && rev==null){
       // Step 4
       RDFTerm resource=getSafeCurieOrCurieOrIri(
       node.getAttribute("about"),iriMapLocal);
       if(resource==null){
     resource=getSafeCurieOrCurieOrIri(
     node.getAttribute("resource"),iriMapLocal);
       }
       if(resource==null){
     resource=relativeResolve(node.getAttribute("href"));
       }
       if(resource==null){
     resource=relativeResolve(node.getAttribute("src"));
       }
       if((resource==null || resource.getKind()!=RDFTerm.IRI)){
     string rdfTypeof=getCurie(node.getAttribute("typeof"),iriMapLocal);
     if(isHtmlElement(node, "head") ||
     isHtmlElement(node, "body")){
       resource=getSafeCurieOrCurieOrIri("",iriMapLocal);
     }
     if(resource==null && !xhtml && root){
       resource=getSafeCurieOrCurieOrIri("",iriMapLocal);
     }
     if(resource==null && rdfTypeof!=null){
       resource=generateBlankNode();
     }
     if(resource==null){
       if(context.parentObject!=null) {
     resource=context.parentObject;
       }
       if(node.getAttribute("property")==null){
     skipElement=true;
       }
     }
     newSubject=resource;
       } else {
     newSubject=resource;
       }
     } else {
       // Step 5
       RDFTerm resource=getSafeCurieOrCurieOrIri(
       node.getAttribute("about"),iriMapLocal);
       if(resource==null){
     resource=relativeResolve(node.getAttribute("src"));
       }
       if((resource==null || resource.getKind()!=RDFTerm.IRI)){
     string rdfTypeof=getCurie(node.getAttribute("typeof"),iriMapLocal);
     if(isHtmlElement(node, "head") ||
     isHtmlElement(node, "body")){
       resource=getSafeCurieOrCurieOrIri("",iriMapLocal);
     }
     if(resource==null && !xhtml && root){
       resource=getSafeCurieOrCurieOrIri("",iriMapLocal);
     }
     if(resource==null && rdfTypeof!=null){
       resource=generateBlankNode();
     }
     if(resource==null){
       if(context.parentObject!=null) {
     resource=context.parentObject;
       }
     }
     newSubject=resource;
       } else {
     newSubject=resource;
       }
       resource=getSafeCurieOrCurieOrIri(
       node.getAttribute("resource"),iriMapLocal);
       if(resource==null){
     resource=relativeResolve(node.getAttribute("href"));
       }
       currentObject=resource;
     }
     // Step 6
     if(newSubject!=null){
       string[] types=StringUtility.splitAtNonFFSpaces(node.getAttribute("typeof"));
       foreach(var type in types){
     string iri=getCurie(type,iriMapLocal);
     if(iri!=null){
       outputGraph.Add(new RDFTriple(
       newSubject,RDFTerm.A,
       RDFTerm.fromIRI(iri)
       ));
     }
       }
     }
     // Step 7
     if(currentObject!=null){
       string[] types=StringUtility.splitAtNonFFSpaces(rel);
       foreach(var type in types){
     string iri=getRelTermOrCurie(type,
     iriMapLocal);
     #if DEBUG
     if(!(newSubject!=null))throw new InvalidOperationException("doesn't satisfy newSubject!=null");
     #endif
     if(iri!=null){
       outputGraph.Add(new RDFTriple(
       newSubject,
       RDFTerm.fromIRI(iri),currentObject
       ));
     }
       }
       types=StringUtility.splitAtNonFFSpaces(rev);
       foreach(var type in types){
     string iri=getRelTermOrCurie(type,
     iriMapLocal);
     if(iri!=null){
       outputGraph.Add(new RDFTriple(
       currentObject,
       RDFTerm.fromIRI(iri),
       newSubject
       ));
     }
       }
     } else {
       // Step 8
       string[] types=StringUtility.splitAtNonFFSpaces(rel);
       bool hasPredicates=false;
       // Defines predicates
       foreach(var type in types){
     string iri=getRelTermOrCurie(type,
     iriMapLocal);
     if(iri!=null){
       if(!hasPredicates){
     hasPredicates=true;
     currentObject=generateBlankNode();
       }
       RDFa.IncompleteTriple inc=new RDFa.IncompleteTriple();
       inc.predicate=RDFTerm.fromIRI(iri);
       inc.direction=RDFa.ChainingDirection.Forward;
       incompleteTriplesLocal.Add(inc);
     }
       }
       types=StringUtility.splitAtNonFFSpaces(rev);
       foreach(var type in types){
     string iri=getRelTermOrCurie(type,
     iriMapLocal);
     if(iri!=null){
       if(!hasPredicates){
     hasPredicates=true;
     currentObject=generateBlankNode();
       }
       RDFa.IncompleteTriple inc=new RDFa.IncompleteTriple();
       inc.predicate=RDFTerm.fromIRI(iri);
       inc.direction=RDFa.ChainingDirection.Reverse;
       incompleteTriplesLocal.Add(inc);
     }
       }
     }
     // Step 9
     string[] preds=StringUtility.splitAtNonFFSpaces(property);
     string datatypeValue=getCurie(datatype,
     iriMapLocal);
     if(datatype!=null && datatypeValue==null) {
       datatypeValue="";
     }
     //Console.WriteLine("datatype=[%s] prop=%s vocab=%s",
     //  datatype,property,localDefaultVocab);
     //Console.WriteLine("datatypeValue=[%s]",datatypeValue);
     RDFTerm currentProperty=null;
     foreach(var pred in preds){
       string iri=getCurie(pred,
       iriMapLocal);
       if(iri!=null){
     //Console.WriteLine("iri=[%s]",iri);
     currentProperty=null;
     if(datatypeValue!=null && datatypeValue.Length>0 &&
     !datatypeValue.Equals(RDF_XMLLITERAL)){
       string literal=content;
       if(literal==null) {
     literal=getTextNodeText(node);
       }
       currentProperty=RDFTerm.fromTypedString(literal,datatypeValue);
     } else if(node.getAttribute("content")!=null ||
     !hasNonTextChildNodes(node) ||
     (datatypeValue!=null && datatypeValue.Length==0)){
       string literal=node.getAttribute("content");
       if(literal==null) {
     literal=getTextNodeText(node);
       }
       currentProperty=(!string.IsNullOrEmpty(localLanguage)) ?
       RDFTerm.fromLangString(literal, localLanguage) :
         RDFTerm.fromTypedString(literal);
     } else if(hasNonTextChildNodes(node) &&
     (datatypeValue==null || datatypeValue.Equals(RDF_XMLLITERAL))){
       // XML literal
       recurse=false;
       if(datatypeValue==null) {
     datatypeValue=RDF_XMLLITERAL;
       }
       try {
     string literal=ExclusiveCanonicalXML.canonicalize(node,
         false, namespacesLocal);
     currentProperty=RDFTerm.fromTypedString(literal,datatypeValue);
       } catch(ArgumentException){
     // failure to canonicalize
       }
     }
     #if DEBUG
     if(!(newSubject!=null))throw new InvalidOperationException("doesn't satisfy newSubject!=null");
     #endif
     outputGraph.Add(new RDFTriple(
     newSubject,
     RDFTerm.fromIRI(iri),currentProperty
     ));
       }
     }
     // Step 10
     if(!skipElement && newSubject!=null){
       foreach(var triple in context.incompleteTriples){
     if(triple.direction==RDFa.ChainingDirection.Forward){
       outputGraph.Add(new RDFTriple(
       context.parentSubject,
       triple.predicate,
       newSubject));
     } else {
       outputGraph.Add(new RDFTriple(
       newSubject,triple.predicate,
       context.parentSubject));
     }
       }
     }
     // Step 13
     if(recurse){
       foreach(var childNode in node.getChildNodes()){
     IElement childElement;
     RDFa.EvalContext oldContext=context;
     if(childNode is IElement){
       childElement=((IElement)childNode);
       //Console.WriteLine("skip=%s vocab=%s local=%s",
       //  skipElement,context.defaultVocab,
       //localDefaultVocab);
       if(skipElement){
     RDFa.EvalContext ec=oldContext.copy();
     ec.language=localLanguage;
     ec.iriMap=iriMapLocal;
     ec.namespaces=namespacesLocal;
     context=ec;
     process(childElement,false);
       } else {
     RDFa.EvalContext ec=new RDFa.EvalContext();
     ec.baseURI=oldContext.baseURI;
     ec.iriMap=iriMapLocal;
     ec.namespaces=namespacesLocal;
     ec.incompleteTriples=incompleteTriplesLocal;
     ec.parentSubject=((newSubject==null) ? oldContext.parentSubject :
       newSubject);
     ec.parentObject=((currentObject==null) ?
         ((newSubject==null) ? oldContext.parentSubject :
           newSubject) : currentObject);
     ec.language=localLanguage;
     context=ec;
     process(childElement,false);
       }
     }
     context=oldContext;
       }
     }
 }
Ejemplo n.º 4
0
        /*
         *  Replaces certain blank nodes with blank nodes whose
         *  names meet the N-Triples requirements
         * @param triples A set of RDF triples
         * @param bnodeLabels A mapping of blank node names
         * already allocated.  This method will modify this
         * _object as needed to allocate new blank nodes.
         */
        //
        internal static void replaceBlankNodes(ISet <RDFTriple> triples,
                                               IDictionary <string, RDFTerm> bnodeLabels)
        {
            if (bnodeLabels.Count == 0)
            {
                return;
            }
            IDictionary <string, RDFTerm> newBlankNodes  = new PeterO.Support.LenientDictionary <string, RDFTerm>();
            IList <RDFTriple[]>           changedTriples = new List <RDFTriple[]>();

            int[] nodeindex = new int[] { 0 };
            foreach (var triple in triples)
            {
                bool    changed = false;
                RDFTerm subj    = triple.getSubject();
                if (subj.getKind() == RDFTerm.BLANK)
                {
                    string oldname = subj.getValue();
                    string newname = suggestBlankNodeName(oldname, nodeindex, bnodeLabels);
                    if (!newname.Equals(oldname))
                    {
                        RDFTerm newNode = newBlankNodes[oldname];
                        if (newNode == null)
                        {
                            newNode = RDFTerm.fromBlankNode(newname);
                            bnodeLabels.Add(newname, newNode);
                            newBlankNodes.Add(oldname, newNode);
                        }
                        subj    = newNode;
                        changed = true;
                    }
                }
                RDFTerm obj = triple.getObject();
                if (obj.getKind() == RDFTerm.BLANK)
                {
                    string oldname = obj.getValue();
                    string newname = suggestBlankNodeName(oldname, nodeindex, bnodeLabels);
                    if (!newname.Equals(oldname))
                    {
                        RDFTerm newNode = newBlankNodes[oldname];
                        if (newNode == null)
                        {
                            newNode = RDFTerm.fromBlankNode(newname);
                            bnodeLabels.Add(newname, newNode);
                            newBlankNodes.Add(oldname, newNode);
                        }
                        obj     = newNode;
                        changed = true;
                    }
                }
                if (changed)
                {
                    RDFTriple[] newTriple = new RDFTriple[] { triple,
                                                              new RDFTriple(subj, triple.getPredicate(), obj) };
                    changedTriples.Add(newTriple);
                }
            }
            foreach (var triple in changedTriples)
            {
                triples.Remove(triple[0]);
                triples.Add(triple[1]);
            }
        }
Ejemplo n.º 5
0
 private static IDictionary<string, string> createLegacyLabelsMap()
 {
     IDictionary<string, string> map=new PeterO.Support.LenientDictionary<string,string>();
     for(int i=0;i<legacyLabels.Length;i+=2){
       map.Add(legacyLabels[i], legacyLabels[i+1]);
     }
     return map;
 }
Ejemplo n.º 6
0
 public IDictionary<string, IList<string>> getHeaderFields()
 {
     IDictionary<string, IList<string>> map=new PeterO.Support.LenientDictionary<string, IList<string>>();
       map.Add(null,(new string[]{list[0]}));
       for(int i=1;i<list.Count;i+=2){
     string key=list[i];
     IList<string> templist=map[key];
     if(templist==null){
       templist=new List<string>();
       map.Add(key,templist);
     }
     templist.Add(list[i+1]);
       }
       // Make lists unmodifiable
       foreach(var key in new List<string>(map.Keys)){
     map.Add(key,PeterO.Support.Collections.UnmodifiableList(map[key]));
       }
       return PeterO.Support.Collections.UnmodifiableMap(map);
 }
Ejemplo n.º 7
0
 /**
    * Gets all children of the specified JSON _object
    * that contain the specified key.   The method will
    * not remove matching keys. As an example, consider
    * this _object:
    * <pre>
    * [{"key":"value1","foo":"foovalue"},
    *  {"key":"value2","bar":"barvalue"},
    *  {"baz":"bazvalue"}]
    * </pre>
    * If getPointersToKey is called on this _object
    * with a keyToFind called "key", we get the following
    * Map as the return value:
    * <pre>
    * {
    * "/0" => "value1", // "/0" points to {"foo":"foovalue"}
    * "/1" => "value2" // "/1" points to {"bar":"barvalue"}
    * }
    * </pre>
    * and the JSON _object will change to the following:
    * <pre>
    * [{"foo":"foovalue"},
    *  {"bar":"barvalue"},
    *  {"baz","bazvalue"}]
    * </pre>
    *
    * @param root _object to search
    * @param keyToFind the key to search for.
    * @return a map:<ul>
    *  <li>The keys in the map are JSON Pointers to the objects
    *  within <i>root</i>
    *  that contained a key named <i>keyToFind</i>.
    *  To get the actual JSON _object, call JSONPointer.getObject,
    *  passing <i>root</i> and the pointer as arguments.</li>
    *  <li>The values in the map are the values of each of
    *  those keys named <i>keyToFind</i>.</li>
    *  </ul>
    *  The JSON Pointers are relative to the root
    * _object.
    */
 public static IDictionary<string, Object> getPointersWithKeyAndRemove(Object root, string keyToFind)
 {
     IDictionary<string,Object> list=new PeterO.Support.LenientDictionary<string,Object>();
     getPointersWithKey(root,keyToFind,"",list,true);
     return list;
 }
Ejemplo n.º 8
0
 /**
    * Construct an empty JSONObject.
    */
 public JSONObject()
 {
     myHashMap = new PeterO.Support.LenientDictionary<string, Object>();
 }
Ejemplo n.º 9
0
 private static IDictionary<string, BEncodeObject> readDictionary(PeterO.Support.InputStream stream)
 {
     IDictionary<string,BEncodeObject> map=new PeterO.Support.LenientDictionary<string,BEncodeObject>();
     while(true){
       stream.mark(2);
       int c=stream.ReadByte();
       if(c=='e') {
     break;
       }
       stream.reset();
       string s=readString(stream);
       Object o=readObject(stream);
       map.Add(s,new BEncodeObject(o));
     }
     return map;
 }