CREATE OR REPLACE FUNCTION melodious.query_flags(user_name varchar(32), chan_name varchar(32), igroup_name varchar(32), iflag_name varchar(32), flagcheck bool)
RETURNS TABLE (
group_holders jsonb [],
flag_id int4,
flag_name varchar(32),
flag jsonb
)
LANGUAGE plpgsql
AS $$
DECLARE
uid int4 := NULL;
cid int4 := NULL;
gid int4 := NULL;
BEGIN
IF user_name <> '' THEN
SELECT id INTO uid FROM melodious.accounts WHERE username=user_name;
IF uid IS NULL THEN
RAISE EXCEPTION 'no such user';
END IF;
END IF;
IF chan_name <> '' THEN
SELECT id INTO cid FROM melodious.channels WHERE name=chan_name;
IF cid IS NULL THEN
RAISE EXCEPTION 'no such channel';
END IF;
END IF;
IF igroup_name <> '' THEN
SELECT id INTO gid FROM melodious.groups WHERE name=igroup_name;
IF gid IS NULL THEN
RAISE EXCEPTION 'go such group';
END IF;
END IF;
IF igroup_name = '' THEN
IF iflag_name = '' THEN
IF user_name = '' AND chan_name = '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB((SELECT name FROM melodious.groups WHERE id=gh.group_id LIMIT 1)))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE (NOT flagcheck) OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
GROUP BY gf.id, gf.name;
ELSIF user_name <> '' AND chan_name <> '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB((SELECT name FROM melodious.groups WHERE id=gh.group_id LIMIT 1)))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE (gh.user_id = uid AND gh.channel_id = cid)
OR (flagcheck AND gh.user_id = uid AND gh.channel_id IS NULL)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id = cid)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
GROUP BY gf.id, gf.name;
ELSIF user_name <> '' AND chan_name = '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB((SELECT name FROM melodious.groups WHERE id=gh.group_id LIMIT 1)))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE (NOT flagcheck AND gh.user_id = uid)
OR (flagcheck AND gh.user_id = uid AND gh.channel_id IS NULL)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
GROUP BY gf.id, gf.name;
ELSIF user_name = '' AND chan_name <> '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB((SELECT name FROM melodious.groups WHERE id=gh.group_id LIMIT 1)))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE (NOT flagcheck AND gh.channel_id = cid)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id = cid)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
GROUP BY gf.id, gf.name;
END IF;
ELSE
IF user_name = '' AND chan_name = '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB((SELECT name FROM melodious.groups WHERE id=gh.group_id LIMIT 1)))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE (gf.name = iflag_name)
AND (
(NOT flagcheck)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
)
GROUP BY gf.id, gf.name;
ELSIF user_name <> '' AND chan_name <> '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB((SELECT name FROM melodious.groups WHERE id=gh.group_id LIMIT 1)))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE gf.name = iflag_name
AND (
(gh.user_id = uid AND gh.channel_id = cid)
OR (flagcheck AND gh.user_id = uid AND gh.channel_id IS NULL)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id = cid)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
)
GROUP BY gf.id, gf.name;
ELSIF user_name <> '' AND chan_name = '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB((SELECT name FROM melodious.groups WHERE id=gh.group_id LIMIT 1)))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE gf.name = iflag_name
AND (
(NOT flagcheck AND gh.user_id = uid)
OR (flagcheck AND gh.user_id = uid AND gh.channel_id IS NULL)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
)
GROUP BY gf.id, gf.name;
ELSIF user_name = '' AND chan_name <> '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB((SELECT name FROM melodious.groups WHERE id=gh.group_id LIMIT 1)))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE gf.name = iflag_name
AND (
(NOT flagcheck AND gh.channel_id = cid)
OR (flagcheck AND gh.channel_id = cid AND gh.user_id IS NULL)
OR (flagcheck AND gh.channel_id IS NULL AND gh.user_id IS NULL)
)
GROUP BY gf.id, gf.name;
END IF;
END IF;
ELSE
IF iflag_name = '' THEN
IF user_name = '' AND chan_name = '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB(igroup_name))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE gh.group_id = gid
AND (
(NOT flagcheck)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
)
GROUP BY gf.id, gf.name;
ELSIF user_name <> '' AND chan_name <> '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB(igroup_name))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE gh.group_id = gid
AND (
(gh.channel_id = cid AND gh.user_id = uid)
OR (flagcheck AND gh.channel_id = cid AND gh.user_id IS NULL)
OR (flagcheck AND gh.channel_id IS NULL AND gh.user_id = uid)
OR (flagcheck AND gh.channel_id IS NULL AND gh.user_id IS NULL)
)
GROUP BY gf.id, gf.name;
ELSIF user_name <> '' AND chan_name = '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB(igroup_name))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE gh.group_id = gid
AND (
(NOT flagcheck AND gh.user_id = uid)
OR (flagcheck AND gh.user_id = uid AND gh.channel_id IS NULL)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
)
GROUP BY gf.id, gf.name;
ELSIF user_name = '' AND chan_name <> '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB(igroup_name))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE gh.group_id = gid
AND (
(NOT flagcheck AND gh.channel_id = cid)
OR (flagcheck AND gh.channel_id = cid AND gh.user_id IS NULL)
OR (flagcheck AND gh.channel_id IS NULL AND gh.user_id IS NULL)
)
GROUP BY gf.id, gf.name;
END IF;
ELSE
IF user_name = '' AND chan_name = '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB(igroup_name))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
INNER JOIN melodious.groups g
ON g.id = gh.group_id
WHERE (gf.name = iflag_name AND gh.group_id = gid)
AND (
(NOT flagcheck)
OR (flagcheck AND gh.channel_id IS NULL AND gh.user_id IS NULL)
)
GROUP BY gf.id, gf.name;
ELSIF user_name <> '' AND chan_name <> '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB(igroup_name))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE (gf.name = iflag_name AND gh.group_id = gid)
AND (
(gh.user_id = uid AND gh.channel_id = cid)
OR (flagcheck AND gh.user_id = uid AND gh.channel_id IS NULL)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id = cid)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
)
GROUP BY gf.id, gf.name;
ELSIF user_name <> '' AND chan_name = '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB(igroup_name))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE (gh.group_id = gid AND gf.name = iflag_name)
AND (
(NOT flagcheck AND gh.user_id = uid)
OR (flagcheck AND gh.user_id = uid AND gh.channel_id IS NULL)
OR (flagcheck AND gh.user_id IS NULL AND gh.channel_id IS NULL)
)
GROUP BY gf.id, gf.name;
ELSIF user_name = '' AND chan_name <> '' THEN
RETURN QUERY SELECT
ARRAY_AGG(JSONB_SET(ROW_TO_JSON(gh)::JSONB, '{group_name}'::TEXT[], TO_JSONB(igroup_name))) AS group_holders,
gf.id flag_id,
gf.name flag_name,
gf.flag flag
FROM melodious.group_holders gh
INNER JOIN melodious.group_flags gf
ON gh.group_id = gf.group_id
WHERE (gf.name = iflag_name AND gh.group_id = gid)
AND (
(NOT flagcheck AND gh.channel_id = cid)
OR (flagcheck AND gh.channel_id = cid AND gh.user_id IS NULL)
OR (flagcheck AND gh.channel_id IS NULL AND gh.user_id IS NULL)
)
GROUP BY gf.id, gf.name;
END IF;
END IF;
END IF;
END;
$$;
string date = user.RegistrationDate.ToShortDateString().ToString();
function urlREPLACER(dtTEXTVALUE){
dtTEXTVALUE.indexOf('http://') != -1 && (dtTEXTVALUE = dtTEXTVALUE.replace('http://','')) && dtTEXTVALUE.indexOf('.aspx') == -1 && (dtTEXTVALUE += '.aspx');
return dtTEXTVALUE;
}
function uberURLREPLACER(dtTEXTVALUE,boolVALUE){
// If statement? what's that??
// Naming conventions? Of course not!
// true/false keywords? I never heard it.
boolVALUE != !1 && dtTEXTVALUE.indexOf('http://') != -1 && (dtTEXTVALUE = dtTEXTVALUE.replace('http://','')) && dtTEXTVALUE.indexOf('.aspx') == -1 && (dtTEXTVALUE += '.aspx');
return dtTEXTVALUE;
}
Here's the weirdest way to create if statement with terrible naming conventions. Its based on real events and production code.
if (model.env.Trim().ToUpper() == "Release")
{
retVal = false;
if (!string.IsNullOrEmpty(model.Server_Core))
{
// Perform a trim in case user specified just whitespace...
string tmpStr = model.Server_Core.Trim();
if (!string.IsNullOrEmpty(tmpStr))
{
retVal = true;
}
}
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
void run_func(char *map[], char *fnt , char *km , float *x , float *y , int line , int row , int lift );
char *long_met[3][8] = {{"mile","0.621371" , "yard","1093.61" , "fut","3280.84" , "duim","39370.1" } ,
{ "mile" ,"1.60934" , "yard","0.0009144" , "fut","0.0003048" , "duim","0.0000254"},
{"kilometer","1" , "meter","1000" , "stmeter","100000" , "mmeter","1000000" } };
char *amount[3][4] = { { "gallon" , "0.264172" , "quarta" , "1.05669" } ,
{"gallon" , " 3.78541" , "quarta" , "0.946353" },
{"litr" , "1" , "mililitr" , "1000" }};
char *mass[3][8] = { {"eng.tonna","0.984207" , "amer.tonna","1.10231" , "stone","157.473" , "funt","2204.62" } ,
{ "eng.tonna" , "1.01605" , "amer.tonna", "0.907185" , "stone","0.00635029" , "funt","0.000453592"},
{"tonna","1" , "kilogram" , "1000" , "miligram","100000" , "microgram","1000000" }};
char **cp;
char *buf_data;
char *fnt_sys;
char *mtr_sys;
char *word[100];
while(1){
int bg = 0,convert_ch = 3,y = 0, d = 0, numb = 0;
float mn =0 , xm = 0 ;
printf("%s", "enter data for converter: ");
fgets( (char *) word, 99 ,stdin);
buf_data = strtok((char *) word, " ");
if( ! strcmp(buf_data, "funt.sys" ) ){
convert_ch = 0;
}
else if( ! strcmp(buf_data, "metric.sys" ) ){
convert_ch = 1;
}
for( bg = 0 ; buf_data != NULL; buf_data = strtok(NULL, " ") , bg++ ) {
switch(bg){
case 1:
if( !strcmp("long_met" , buf_data ) ){
y = sizeof(*long_met) / sizeof(long_met[0][0]);
d = sizeof(long_met) / sizeof(long_met[0][0]);
cp = &long_met[convert_ch][0] ;
}
else if(!strcmp("amount" , buf_data ) ){
y = sizeof(*amount) / sizeof(amount [0][0]);
d = sizeof(amount) / sizeof(amount[0][0]);
cp = &amount[convert_ch][0] ;
}
else if(!strcmp("mass" , buf_data ) ){
y = sizeof(*mass ) / sizeof(mass[0][0]);
d = sizeof(mass ) / sizeof(mass[0][0]);
cp = &mass[convert_ch][0] ;
}
break;
case 2:
fnt_sys = buf_data;
break;
case 3:
mtr_sys = buf_data;
break;
case 4:
numb = atoi(buf_data);
break;
}
}
if( !y || !d || !cp || convert_ch == 3 || !numb ){
puts("error");
}
else{
run_func( cp, fnt_sys , mtr_sys , &mn , &xm , y , d , convert_ch );
if( !mn || !xm ){
puts("error");
} else{
printf("%f\n" , !convert_ch ? (mn / xm ) * numb : ( mn * xm ) * numb );
}
}
}
return 0;
}
void run_func(char *map[], char *fnt , char *km , float *x , float *y , int line , int row , int lift ){
int m ;
if( ( lift ) ){
row -= line;
}
for( m = 0 ; m <= line ; m++){
if(!strcmp(fnt,map[m])){
*x = atof(map[m + 1] ) ;
break;
}
}
for( m = (row - line) ; m <= row - 1 ; m++){
if( !strcmp( km , map[m] ) ){
*y = atof(map[m + 1 ] );
break;
}
}
}
return [word for word in words if any(all(ch in row for ch in word.lower()) for row in rows)]
Filtering words that can be typed using only one row of the keyboard.
.black {
color: #000;
}
.not-black {
color: #999;
}
<Image source={ this.props.pickupOrDropoff == "pickup"
? i.pickup
? iconBlue
: iconWhite
: i.dropoff
? iconBlue
: iconWhite} style={{
height:this.props.pickupOrDropoff == "pickup"
? i.pickup
? 30
: i.sign === 'D' ? 30 : 35
: i.dropoff
? 30
: i.sign === 'D' ? 30 : 35,
marginRight:10 ,
width: this.props.pickupOrDropoff == "pickup"
? i.pickup
? 40
: 40
: i.dropoff
? 40
: 40
,resizeMode:'contain'}}/>
I love when the code is neat...
void MagicUnit::getSomeVoltage(std::uint64_t w_data, double &val)
{
std::uint64_t r_data = 0;
hwRead((int)Address::Voltage, 3, w_data, 2, &r_data);
val = r_data;
val /= 16; // shit right 4 bits
val *= 0.004;
val -= 8.192;
}
try:
raise
except RuntimeError:
raise
try this in python3
<?php
$payload = '{ "products": [';
foreach ($products as $product) {
$payload .= $product->toJson() . ',';
}
$payload = substr($payload, 0, \strlen($payload) - 1);
$payload .= ']}';
/**
* fk--folder key,dk--doc key -->value is key string
* dt--doc type,ft--folder type -->both have 19 key options:
* bom tr amend history help info loeid cust cust0-cust9 custhist
* folder type only seen as "history" in toc,why ??do we use other ones? -- it only has one child
* doc type only seen two "tr" and "history" in toc??-- it only has one child
* node with "folder type="history"" is the only child of it parent either "<doc type="tr" key="xxx" trnum="xxx"....." or "<doc type="tr" trnum="xxx""
* <folder type="history" could has more then one doc children .e.g. title="History of AMM31-32-00-720-807" in 700/amm
*
*
*
* folder element has the follwing to identify itself:
* 1, key
* 2, type="history", in this case, folder is the only child of doc element with type ="tr"?????
*
* doc element has the following to identify itself
* 1, key
* 2, type="tr" trnum="xxxxx"
* 3, type="history", in this case, doc isthe only child of a folder element???
*
*
*
* the return json format likes following:
* [
* { "data" : "A node", "children" , "state" : "open" },
* { "data" : "Only child", "state" : "closed" },
* "Ajax node"
* ]
*/
public class XMLToJson
{
private static final Map<String, String> pathMap;
static
{
Map<String, String> aMap = new HashMap<String, String>();
aMap.put("fk", "folder[@key");
aMap.put("ft", "folder[@type");
aMap.put("fth", "folder[@type='history'");
aMap.put("dk", "doc[@key");
aMap.put("dt", "doc[@type");
aMap.put("dth", "doc[@type='history'");
aMap.put("dtrn", "doc[@trnum");
pathMap = Collections.unmodifiableMap(aMap);
}
Util util = new Util();
/*
* @param url the path to TOC.xml
* @param xPathString the short format searched node path
* @throws DocumentException
*
* sample xPathString : "fk:AMM24_fk:AMM24-FM_dk"
*/
@SuppressWarnings({ "unchecked" })
public String getJson(URL url, String xPathString) throws Exception
{
Document TOCDoc = util.getDocument(url);
String jsonString = "[";
Element node = null;
if (xPathString.equals("/"))
{
node = TOCDoc.getRootElement();
}
else
{
String realXPathString = pathMapping(xPathString);
System.out.println(realXPathString);
node = (Element) TOCDoc.selectSingleNode(realXPathString);
}
//List<Element> li = node.elements();
for (Iterator<Element> i = node.elementIterator(); i.hasNext();)
{
Element elem = (Element) i.next();
String eleName = elem.getName();
Boolean hasChildren = false;
if ((elem.elements().size() > 0))
{
hasChildren = true;
//current element has children itself, state shoud be "closed"
}
List<Attribute> list = elem.attributes();
String titleAttrContent = elem.attributeValue("title");
//Boolean isFileAttr = false;
String fileAttrContent = elem.attributeValue("file");
//if (fileAttrContent.isEmpty() )
if (eleName == "doc")
{
//doc element always has "file" attribute
for (Attribute attribute : list)
{
jsonString = jsonString.concat("{");
String attrName = attribute.getName();
//System.out.println("doc arribute Name : " + attrName);
//each one has to have "data" line, "attr" line "state" line and "children" line
jsonString = jsonString.concat("'data':'").concat(titleAttrContent).concat("',");
if (attrName.equals("key"))
{
String keyContent = elem.attributeValue("key");
jsonString = jsonString.concat("'attr':{'id':'").concat(xPathString).concat("_dk:").concat(keyContent).concat("','file':'").concat(fileAttrContent).concat("'}");
break;
}
else if (attrName.equals("trnum"))
{
String trnumContent = elem.attributeValue("trnum");
jsonString = jsonString.concat("'attr':{'id':'").concat(xPathString).concat("_dtrn:").concat(trnumContent).concat("','file':'").concat(fileAttrContent).concat("'}");
break;
}
/* else if (attrName.equals("type"))//type attribute for doc element won't determite what exactly the element is
{
String typeContent = elem.attributeValue("type");
//doc element has type "history"
if (typeContent == "history"){
jsonString = jsonString.concat("'attr':{'id':'").concat(xPathString).concat("_dth,");
}else if (typeContent == "?????"){
//any values for type attribute need to concern????
}
}
else if (attrName.equals("file"))
{
}*/
}
if (hasChildren)
{
//state set up as "closed" and no need to set up "children" field
jsonString = jsonString.concat(",'state':'closed'");
}
else
{
//no need to put anything
//jsonString = jsonString.concat("'state':'???'");
}
jsonString = jsonString.concat("},");
}
else if (eleName == "folder")
{
jsonString = jsonString.concat("{");
for (Attribute attribute : list)
{
String attrName = attribute.getName();
jsonString = jsonString.concat("'data':'").concat(titleAttrContent).concat("',");
if (attrName.equals("key"))
{
String keyContent = elem.attributeValue("key");
jsonString = jsonString.concat("'attr':{'id':'").concat(xPathString).concat("_fk:").concat(keyContent).concat("'}");
if (fileAttrContent != null)
{
jsonString = jsonString.concat("','file':'").concat(fileAttrContent).concat("'}");
}
break;
}
else if (attrName.equals("type"))
{
String typeContent = elem.attributeValue("type");
//doc element has type "history"
if (typeContent == "history")
{
jsonString = jsonString.concat("'attr':{'id':'").concat(xPathString).concat("_fth,");
}
else if (typeContent == "?????")
{
//any values need to concern????
}
break;
}
}
jsonString = jsonString.concat("},");
}
continue;
}
//return list;
jsonString = jsonString.substring(0, jsonString.length() - 1);
jsonString = jsonString.concat("]");
return jsonString;
}
/*
* read xpathstring from post request and generate the real xpath for toc
*/
public String getXPathString()
{
//readPostRequest()
return null;
}
/*
* post string looks like : "fk:LOETR_dtrn:TR12-118_fth_dth"
* it represents the inner doc elemnet:
* <folder key="LOETR" type="loetr" title="List of Effective TRs" file="loetr.html">
* <doc type="tr" trnum="TR12-118" trdate="May 07/2012" title="[TR12-118] TASK AMM12-31-00-660-806 - Inspection and Removal of De-Hydrated Anti-Icing Fluid inside the Flight Control Surfaces" file="TR12-118.pdf" refloc="AMM12-31-00-660-806">
* <folder type="history" title="History of AMM12-31-00-660-806">
* <doc title="TASK 12-31-00-660-806 - Inspection and Removal of De-Hydrated Anti-Icing Fluid inside the Flight Control Surfaces" file="AMM12-31-00-660-806.pdf" type="history" refloc="AMM12-31-00-660-806"/>
* </folder>
* the xpath string should be:
* folder[@key="LOETR"]/doc[@trnum="TR12-118"]/folder[@type="history"]/doc[@type="history"]
*
*
* the String : "fk:AMM24_fk:AMM24-FM_dk:CTOC-24"
* it represents the inner doc with attribute file="CTOC-24.pdf"
* the string : "fk:AMM24_fk:AMM24-00-00_fk:AMM24-00-00-02_dk:AMM24-00-00-700-801" represents
* <folder key="AMM24" title="CH 24 - Electrical Power">
* <folder key="AMM24-FM" title="Front Matter">
* <doc key="CTOC-24" title="Table of Contents" file="CTOC-24.pdf"/>
* </folder>
* <folder key="AMM24-00-00" title="24-00-00 - General">
* <folder key="AMM24-00-00-02" title="General - Maintenance Practices">
* <doc key="AMM24-00-00-700-801" title="TASK 24-00-00-700-801 - AC Power, DC Power and Battery Maintenance Practice Recommendations" file="AMM24-00-00-700-801.pdf"/>
*
* it can be even optimized as :
* "fk:AMM24_fk:00-00_fk:02_dk:AMM24-00-00-700-801"
* if the inner key fully include the previous key, omit it, otherwise use full string
* the xpath string should be:
* folder[@key="AMM24"]/folder[@key="AMM24-00-00"]/folder[@key="AMM24-00-00-02"]/doc[@key="AMM24-00-00-700-801"]
*
* if shortXPath is ?? which means the query based on the root of the document
*
*
*/
public String pathMapping(String shortXPath) throws Exception
{
String tagetString = null;
if (shortXPath.equals(""))
{
tagetString = "//toc";
}
else
{
tagetString = "//";
}
int newStart = 0;
String segString = "";
String valueString = "";
//dth???
//need mapping all senarios
//already??
while (shortXPath.indexOf("_", newStart) > -1)
{
int keyValueSepPos = 0;
String keyString = "";//not necessary key, might be type attribute
segString = shortXPath.substring(newStart, shortXPath.indexOf("_", newStart));
newStart = shortXPath.indexOf("_", newStart) + 1;//new start search point
//System.out.println(newStart);
if (segString.indexOf(":") > 0)
{
keyValueSepPos = segString.indexOf(":");
keyString = segString.substring(0, keyValueSepPos);
valueString = segString.substring(keyValueSepPos + 1);
if (pathMap.get(keyString).length() > 0)
{
tagetString = tagetString.concat(pathMap.get(keyString));
}
else
{
throw new Exception("no mapping found");
}
tagetString = tagetString.concat("='").concat(valueString).concat("']/");
}
}
//this is for scenerio either no "_" or sub string after "_"
segString = shortXPath.substring(newStart);
System.out.println(segString);
if (segString.indexOf(":") > 0)
{
int lastKeyValueSepPos = segString.indexOf(":");
String lastKeyString = segString.substring(0, lastKeyValueSepPos);
String lastValueString = segString.substring(lastKeyValueSepPos + 1);
if (pathMap.get(lastKeyString).length() > 0)
{
tagetString = tagetString.concat(pathMap.get(lastKeyString));
}
else
{
throw new Exception("no mapping found");
}
tagetString = tagetString.concat("='").concat(lastValueString).concat("']");
}
return tagetString;
}
public static void main(String[] args) throws Exception
{
XMLToJson x2j = new XMLToJson();
String test = "fk:AMM24_fk:AMM24-FM";
test = "";
System.out.println(x2j.getJson(new URL("http://localhost:8080/WebNavSpring/q400/amm/toc.xml"), test));
//System.out.println(x2j.pathMapping(test));
}
}
easy clean code to convert XML to JSON .
const Discord = require("discord.js");
const bot = new Discord.Client();
const TOKEN = "NOPE"
var color = require('chalk');
var fs = require('fs');
bot.on("ready", function(message) {
console.log(color.green("Online"))
})
bot.on("message", function(message){
console.log("Channel:" + color.blue(message.channel) + " " + "Author:" + color.blue(message.author) + " " + "Message:" + color.blue(message.content))
if (message.content.includes("uranium") || message.content == "Uranium" || message.content == "uranium" || message.content.includes("uranium")) {
console.log(color.black.yellow("Message Flagged As Suspicous"))
console.log(color.black.yellow("Contains: Uranium"))
console.log(color.black.bgYellow("Message:" + message.content))
console.log(color.black.bgYellow("Message Id:" + message.id))
console.log(color.black.bgYellow("Author Id:" + message.author.id))
console.log(color.black.bgYellow("Channel Id:" + message.channel.id))
console.log(color.yellow("Severity:" + "1"))
var alertsev1 = `Message + Id: + ${message.id} + | + Author + Id: + ${message.author.id} + | + Message + Channel Id: ${message.channel.id} + | + Message: + ${message.content}`
fs.writeFile('alerts.txt', `${alertsev2}`)
}
if (message.content.includes("raid") || message.content.includes("Raid")) {
console.log(color.black.red("Message Flagged As Suspicous"))
console.log(color.black.red("Contains: Raid"))
console.log(color.black.bgRed("Message:" + message.content))
console.log(color.black.bgRed("Message Id:" + message.id))
console.log(color.black.bgRed("Author Id:" + message.author.id))
console.log(color.black.bgRed("Channel Id:" + message.channel.id))
console.log(color.black.red("Severity:" + "2"))
var alertsev2 = `Message + Id: + ${message.id} + | + Author + Id: + ${message.author.id} + | + Message + Channel Id: ${message.channel.id} + | + Message: + ${message.content}`
fs.writeFile('alerts.txt', `${alertsev2}`)
}
if (message.content.includes("raid") && message.content.includes("uranium") || message.content.includes("raid") && message.content.includes("Uranium") || message.content.includes("raid") && message.content.includes("uranium") || message.content.includes("Raid") && message.content.includes("uranium") || message.content.includes("Raid")) {
console.log(color.black.red("Message Flagged As Possible Raid Initiation"))
console.log(color.black.red("Contains: Raid, Uranium"))
console.log(color.black.bgRed("Message:" + message.content))
console.log(color.black.bgRed("Message Id:" + message.id))
console.log(color.black.bgRed("Author Id:" + message.author.id))
console.log(color.black.bgRed("Channel Id:" + message.channel.id))
console.log(color.black.red("Severity:" + "3"))
var alertsev3 = `Message + Id: + ${message.id} + | + Author + Id: + ${message.author.id} + | + Message + Channel Id: ${message.channel.id} + | + Message: + ${message.content}`
fs.writeFile('alerts.txt', `${alertsev3}`)
}
})
bot.login(TOKEN)
I'm making a discord message logger but I want to output message.content message.author message.id message.channel.id into one text file everytime a message is sent in the following format: Message ID:(Id here) Message Author: (Author ID here) Channel ID: (Channel ID here) Message: (message here) ive tried with the following code but it printed undefined out into the text document
try {
channel.send(eventMessage);
} catch (MessageHandlingException ex) {
channel.send(eventMessage);
}