forked from jbowtie/gokogiri
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhigang Chen
committed
Feb 27, 2012
1 parent
167b158
commit c3b5f88
Showing
18 changed files
with
898 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ build/* | |
_* | ||
*.6 | ||
*.o | ||
libxml/test/ctest/test | ||
libxml/test/ctest/test | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Hampton Catlin | ||
Zhigang Chen | ||
Hampton Catlin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +0,0 @@ | ||
Copyright (C) 2011 by Hampton Catlin | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,4 @@ | ||
Gokogiri | ||
by Hampton Catlin and Zhigang Chen | ||
|
||
A pretty straightforward and simple library for using | ||
LibXML in native Go. | ||
|
||
First, install Go and libxml on your system. You can google that | ||
one yourself. You probably have libxml2 and since you are looking | ||
for a Go lib, assumedly you have that too. | ||
|
||
Then, we are using the build tool called "gb". You can get install | ||
instructions from here: http://code.google.com/p/go-gb/ | ||
|
||
So, to build the project, just go to the root directory and type | ||
|
||
$> gb | ||
|
||
Then, to run the tests: | ||
|
||
$> gb -t | ||
|
||
And finally, to install it... | ||
|
||
$> sudo -E gb -i | ||
|
||
Note: The -E flag is needed if your GO_PATH is not exported automatically to the root user. So, setting -E makes sure it is! | ||
|
||
To check memory leaks by libxml2, please add "--with-mem-debug" to the configure options when compiling libxml2. If you are on Mac and use brew, you can do | ||
|
||
brew edit libxml2 | ||
|
||
Then add " --with-mem-debug " into "args", after "--prefix=#{prefix}", and do | ||
|
||
brew install libxml2; brew link libxml2 | ||
|
||
On linux, you just need to rebuild libxml2 with ( --with-mem-debug).Just do | ||
|
||
./configure --with-mem-debug; make install | ||
|
||
You should run the tests again. If there are memory leaks, you should see errors if there are memory leaks by libxml2. | ||
by Zhigang Chen and Hampton Catlin | ||
|
||
A pretty straightforward and simple library for using LibXML in native Go. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0 | ||
1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package help | ||
|
||
/* | ||
#cgo pkg-config: libxml-2.0 | ||
#include <libxml/tree.h> | ||
#include <libxml/parser.h> | ||
#include <libxml/HTMLtree.h> | ||
#include <libxml/HTMLparser.h> | ||
#include <libxml/xmlsave.h> | ||
void printMemoryLeak() { xmlMemDisplay(stdout); } | ||
*/ | ||
import "C" | ||
|
||
func LibxmlInitParser() { | ||
C.xmlInitParser() | ||
} | ||
|
||
func LibxmlCleanUpParser() { | ||
C.xmlCleanupParser() | ||
} | ||
|
||
func LibxmlGetMemoryAllocation() int { | ||
return (int)(C.xmlMemBlocks()) | ||
} | ||
|
||
func LibxmlCheckMemoryLeak() bool { | ||
return (C.xmlMemBlocks() == 0) | ||
} | ||
|
||
func LibxmlReportMemoryLeak() { | ||
C.printMemoryLeak() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package xml | ||
|
||
type AttributeNode struct { | ||
*XmlNode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package xml | ||
|
||
type CDataNode struct { | ||
*XmlNode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include <string.h> | ||
#include "chelper.h" | ||
|
||
//internal callback functions | ||
int document_write_callback(void *document, char *buffer, int len) { | ||
documentWriteCallback(document, buffer, len); | ||
return len; | ||
} | ||
|
||
int close_callback(void * ctx) { | ||
return 0; | ||
} | ||
|
||
xmlDoc* newEmptyXmlDoc() { return xmlNewDoc(BAD_CAST XML_DEFAULT_VERSION); }; | ||
xmlElementType getNodeType(xmlNode *node) { return node->type; } | ||
|
||
void xmlFreeChars(char *buffer) { | ||
if (buffer) { | ||
xmlFree((xmlChar*)buffer); | ||
} | ||
} | ||
|
||
void dumpXmlError(xmlErrorPtr error, void *error_buffer, int error_buffer_len) { | ||
char *c_error_buffer = (char*)error_buffer; | ||
|
||
if(error != NULL && error_buffer != NULL && error->level >= XML_ERR_ERROR) { | ||
if (error->message != NULL) { | ||
strncpy(c_error_buffer, error->message, error_buffer_len-1); | ||
c_error_buffer[error_buffer_len-1] = '\0'; | ||
} | ||
else { | ||
snprintf(c_error_buffer, error_buffer_len, "xml parsing error:%d", error->code); | ||
} | ||
} | ||
} | ||
|
||
char *xmlDocDumpToString(xmlDoc *doc, void *encoding, int format) { | ||
xmlChar *buff; | ||
int buffersize; | ||
xmlDocDumpFormatMemoryEnc(doc, &buff, &buffersize, (char*)encoding, format); | ||
return (char*)buff; | ||
} | ||
|
||
char *htmlDocDumpToString(htmlDocPtr doc, int format) { | ||
xmlChar *buff; | ||
int buffersize; | ||
htmlDocDumpMemoryFormat(doc, &buff, &buffersize, format); | ||
return (char*)buff; | ||
} | ||
|
||
xmlDoc* native_parse(void *buffer, int buffer_len, void *url, void *encoding, int options, void *error_buffer, int error_buffer_len) { | ||
const char *c_buffer = (char*)buffer; | ||
const char *c_url = (char*)url; | ||
const char *c_encoding = (char*)encoding; | ||
xmlDoc *doc = NULL; | ||
|
||
xmlResetLastError(); | ||
doc = xmlReadMemory(c_buffer, buffer_len, c_url, c_encoding, options); | ||
|
||
if(doc == NULL) { | ||
xmlErrorPtr error; | ||
xmlFreeDoc(doc); | ||
error = xmlGetLastError(); | ||
dumpXmlError(error, error_buffer, error_buffer_len); | ||
} | ||
return doc; | ||
} | ||
|
||
xmlNode* native_parse_fragment(xmlDoc* doc, void *buffer, int buffer_len, void *url, int options, void *error_buffer, int error_buffer_len) { | ||
xmlNodePtr root_element = NULL; | ||
xmlParserErrors errCode; | ||
|
||
errCode = xmlParseInNodeContext((xmlNodePtr)doc, buffer, buffer_len, options, &root_element); | ||
if (errCode != XML_ERR_OK) { | ||
char *c_error_buffer = (char*)error_buffer; | ||
snprintf(c_error_buffer, error_buffer_len, "xml fragemnt parsing error (xmlParserErrors):%d", errCode); | ||
return NULL; | ||
} | ||
return root_element; | ||
} | ||
|
||
void native_save_document(void *obj, void *node, void *encoding, int options) { | ||
xmlSaveCtxtPtr savectx; | ||
const char *c_encoding = (char*)encoding; | ||
|
||
savectx = xmlSaveToIO( | ||
(xmlOutputWriteCallback)document_write_callback, | ||
(xmlOutputCloseCallback)close_callback, | ||
(void *)obj, | ||
encoding, | ||
options | ||
); | ||
xmlSaveTree(savectx, (xmlNode*)node); | ||
xmlSaveClose(savectx); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef __PARSE_H__ | ||
#define __PARSE_H__ | ||
|
||
#include <libxml/tree.h> | ||
#include <libxml/parser.h> | ||
#include <libxml/HTMLtree.h> | ||
#include <libxml/HTMLparser.h> | ||
#include <libxml/xmlsave.h> | ||
|
||
xmlDoc* native_parse(void *buffer, int buffer_len, void *url, void *encoding, int options, void *error_buffer, int errror_buffer_len); | ||
xmlNode* native_parse_fragment(xmlDoc* doc, void *buffer, int buffer_len, void *url, int options, void *error_buffer, int error_buffer_len); | ||
void native_save_document(void *obj, void *node, void *encoding, int options); | ||
|
||
xmlDoc* newEmptyXmlDoc(); | ||
xmlElementType getNodeType(xmlNode *node); | ||
char *xmlDocDumpToString(xmlDoc *doc, void *encoding, int format); | ||
char *htmlDocDumpToString(xmlDoc *doc, int format); | ||
void xmlFreeChars(char *buffer); | ||
|
||
#endif //__PARSE_H__ |
Oops, something went wrong.